From 4bb50765f4b46e4f1d35747fc4f7b203feaaa212 Mon Sep 17 00:00:00 2001 From: fengshuonan Date: Mon, 5 Jul 2021 16:28:18 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=907.0.4=E3=80=91=E3=80=90captcha?= =?UTF-8?q?=E3=80=91=E6=9B=B4=E6=96=B0=E5=BC=82=E5=B8=B8=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/exception/enums/SecurityExceptionEnum.java | 7 ++++++- .../kernel/security/captcha/DragCaptchaService.java | 11 ++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/kernel-d-security/security-api/src/main/java/cn/stylefeng/roses/kernel/security/api/exception/enums/SecurityExceptionEnum.java b/kernel-d-security/security-api/src/main/java/cn/stylefeng/roses/kernel/security/api/exception/enums/SecurityExceptionEnum.java index 4d93984dd..62877dcb9 100644 --- a/kernel-d-security/security-api/src/main/java/cn/stylefeng/roses/kernel/security/api/exception/enums/SecurityExceptionEnum.java +++ b/kernel-d-security/security-api/src/main/java/cn/stylefeng/roses/kernel/security/api/exception/enums/SecurityExceptionEnum.java @@ -41,7 +41,12 @@ public enum SecurityExceptionEnum implements AbstractExceptionEnum { /** * 生成验证码错误 */ - CAPTCHA_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + SecurityConstants.SECURITY_EXCEPTION_STEP_CODE + "01", "生成验证码错误"); + CAPTCHA_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + SecurityConstants.SECURITY_EXCEPTION_STEP_CODE + "01", "生成验证码错误"), + + /** + * 验证码过期,请从新生成验证码 + */ + CAPTCHA_INVALID_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + SecurityConstants.SECURITY_EXCEPTION_STEP_CODE + "02", "验证码过期,请从新生成验证码"); /** * 错误编码 diff --git a/kernel-d-security/security-sdk-captcha/src/main/java/cn/stylefeng/roses/kernel/security/captcha/DragCaptchaService.java b/kernel-d-security/security-sdk-captcha/src/main/java/cn/stylefeng/roses/kernel/security/captcha/DragCaptchaService.java index f1bc4ab77..f74c20298 100644 --- a/kernel-d-security/security-sdk-captcha/src/main/java/cn/stylefeng/roses/kernel/security/captcha/DragCaptchaService.java +++ b/kernel-d-security/security-sdk-captcha/src/main/java/cn/stylefeng/roses/kernel/security/captcha/DragCaptchaService.java @@ -35,6 +35,7 @@ import cn.stylefeng.roses.kernel.security.api.exception.SecurityException; import cn.stylefeng.roses.kernel.security.api.exception.enums.SecurityExceptionEnum; import cn.stylefeng.roses.kernel.security.api.pojo.DragCaptchaImageDTO; import cn.stylefeng.roses.kernel.security.captcha.util.DragCaptchaImageUtil; +import lombok.extern.slf4j.Slf4j; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -45,6 +46,7 @@ import java.io.IOException; * @author fengshuonan * @date 2021/7/5 11:34 */ +@Slf4j public class DragCaptchaService implements DragCaptchaApi { private final CacheOperatorApi cacheOperatorApi; @@ -57,6 +59,7 @@ public class DragCaptchaService implements DragCaptchaApi { ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(Base64.decode(DragCaptchaImageUtil.IMAGE_BASE64)); try { DragCaptchaImageDTO dragCaptchaImageDTO = DragCaptchaImageUtil.getVerifyImage(byteArrayInputStream); + // 缓存x轴坐标 String verKey = IdUtil.simpleUUID(); Integer verValue = dragCaptchaImageDTO.getLocationX(); @@ -82,8 +85,14 @@ public class DragCaptchaService implements DragCaptchaApi { return false; } + // 获取缓存中正确的locationX的值 + String locationXString = cacheOperatorApi.get(verKey); + if (StrUtil.isEmpty(locationXString)) { + throw new SecurityException(SecurityExceptionEnum.CAPTCHA_INVALID_ERROR); + } + // 获取缓存中存储的范围 - Integer locationX = Convert.toInt(cacheOperatorApi.get(verKey)); + Integer locationX = Convert.toInt(locationXString); int beginScope = locationX - 5; int endScope = locationX + 5;