From 6ec6f5336755da695378850d9fd960f6498a20e3 Mon Sep 17 00:00:00 2001
From: 无关风月 <443237572@qq.com>
Date: 星期三, 06 十一月 2024 16:02:55 +0800
Subject: [PATCH] Merge branch 'master' of http://120.76.84.145:10101/gitblit/r/java/mx_charging_pile
---
ruoyi-gateway/src/main/java/com/ruoyi/gateway/service/impl/ValidateCodeServiceImpl.java | 168 ++++++++++++++++++++++++++------------------------------
1 files changed, 78 insertions(+), 90 deletions(-)
diff --git a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/service/impl/ValidateCodeServiceImpl.java b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/service/impl/ValidateCodeServiceImpl.java
index 2ae87bd..365cad6 100644
--- a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/service/impl/ValidateCodeServiceImpl.java
+++ b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/service/impl/ValidateCodeServiceImpl.java
@@ -5,6 +5,7 @@
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
+
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.FastByteArrayOutputStream;
@@ -26,94 +27,81 @@
* @author ruoyi
*/
@Service
-public class ValidateCodeServiceImpl implements ValidateCodeService
-{
- @Resource(name = "captchaProducer")
- private Producer captchaProducer;
-
- @Resource(name = "captchaProducerMath")
- private Producer captchaProducerMath;
-
- @Autowired
- private RedisService redisService;
-
- @Autowired
- private CaptchaProperties captchaProperties;
-
- /**
- * 生成验证码
- */
- @Override
- public AjaxResult createCaptcha() throws IOException, CaptchaException
- {
- AjaxResult ajax = AjaxResult.success();
- boolean captchaEnabled = captchaProperties.getEnabled();
- ajax.put("captchaEnabled", captchaEnabled);
- if (!captchaEnabled)
- {
- return ajax;
- }
-
- // 保存验证码信息
- String uuid = IdUtils.simpleUUID();
- String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
-
- String capStr = null, code = null;
- BufferedImage image = null;
-
- String captchaType = captchaProperties.getType();
- // 生成验证码
- if ("math".equals(captchaType))
- {
- String capText = captchaProducerMath.createText();
- capStr = capText.substring(0, capText.lastIndexOf("@"));
- code = capText.substring(capText.lastIndexOf("@") + 1);
- image = captchaProducerMath.createImage(capStr);
- }
- else if ("char".equals(captchaType))
- {
- capStr = code = captchaProducer.createText();
- image = captchaProducer.createImage(capStr);
- }
-
- redisService.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
- // 转换流信息写出
- FastByteArrayOutputStream os = new FastByteArrayOutputStream();
- try
- {
- ImageIO.write(image, "jpg", os);
- }
- catch (IOException e)
- {
- return AjaxResult.error(e.getMessage());
- }
-
- ajax.put("uuid", uuid);
- ajax.put("img", Base64.encode(os.toByteArray()));
- return ajax;
- }
-
- /**
- * 校验验证码
- */
- @Override
- public void checkCaptcha(String code, String uuid) throws CaptchaException
- {
- if (StringUtils.isEmpty(code))
- {
- throw new CaptchaException("验证码不能为空");
- }
- if (StringUtils.isEmpty(uuid))
- {
- throw new CaptchaException("验证码已失效");
- }
- String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
- String captcha = redisService.getCacheObject(verifyKey);
- redisService.deleteObject(verifyKey);
-
- if (!code.equalsIgnoreCase(captcha))
- {
- throw new CaptchaException("验证码错误");
- }
- }
+public class ValidateCodeServiceImpl implements ValidateCodeService {
+ @Resource(name = "captchaProducer")
+ private Producer captchaProducer;
+
+ @Resource(name = "captchaProducerMath")
+ private Producer captchaProducerMath;
+
+ @Autowired
+ private RedisService redisService;
+
+ @Autowired
+ private CaptchaProperties captchaProperties;
+
+ /**
+ * 生成验证码
+ */
+ @Override
+ public AjaxResult createCaptcha() throws IOException, CaptchaException {
+ AjaxResult ajax = AjaxResult.success();
+ boolean captchaEnabled = captchaProperties.getEnabled();
+ ajax.put("captchaEnabled", captchaEnabled);
+ if (!captchaEnabled) {
+ return ajax;
+ }
+
+ // 保存验证码信息
+ String uuid = IdUtils.simpleUUID();
+ String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
+
+ String capStr = null, code = null;
+ BufferedImage image = null;
+
+ String captchaType = captchaProperties.getType();
+ // 生成验证码
+ if ("math".equals(captchaType)) {
+ String capText = captchaProducerMath.createText();
+ capStr = capText.substring(0, capText.lastIndexOf("@"));
+ code = capText.substring(capText.lastIndexOf("@") + 1);
+ image = captchaProducerMath.createImage(capStr);
+ } else if ("char".equals(captchaType)) {
+ capStr = code = captchaProducer.createText();
+ image = captchaProducer.createImage(capStr);
+ }
+
+ redisService.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
+ // 转换流信息写出
+ FastByteArrayOutputStream os = new FastByteArrayOutputStream();
+ try {
+ ImageIO.write(image, "jpg", os);
+ } catch (IOException e) {
+ return AjaxResult.error(e.getMessage());
+ }
+
+ ajax.put("uuid", uuid);
+ ajax.put("img", Base64.encode(os.toByteArray()));
+ return ajax;
+ }
+
+ /**
+ * 校验验证码
+ */
+ @Override
+ public void checkCaptcha(String code, String uuid) throws CaptchaException {
+ if (StringUtils.isEmpty(code)) {
+ throw new CaptchaException("验证码不能为空");
+ }
+ if (StringUtils.isEmpty(uuid)) {
+ throw new CaptchaException("验证码已失效");
+ }
+ String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
+ String captcha = redisService.getCacheObject(verifyKey);
+ redisService.deleteObject(verifyKey);
+
+ if (!code.equalsIgnoreCase(captcha)) {
+ throw new CaptchaException("验证码错误");
+ }
+ }
}
--
Gitblit v1.7.1