Просмотр исходного кода

Update 优化 验证码校验逻辑
update hutool 5.8.38 => 5.8.40 默认支持了验证码不生成负数

Yue 6 месяцев назад
Родитель
Сommit
a9a934a26f

+ 1 - 1
SERVER/VberAdminPlusV3/pom.xml

@@ -37,7 +37,7 @@
         <velocity.version>2.3</velocity.version>
         <satoken.version>1.44.0</satoken.version>
         <p6spy.version>3.9.1</p6spy.version>
-        <hutool.version>5.8.38</hutool.version>
+        <hutool.version>5.8.40</hutool.version>
         <lock4j.version>2.2.7</lock4j.version>
         <mapstruct-plus.version>1.4.8</mapstruct-plus.version>
         <mapstruct-plus.lombok.version>0.2.0</mapstruct-plus.lombok.version>

+ 7 - 4
SERVER/VberAdminPlusV3/vber-admin/src/main/java/com/vber/web/controller/CaptchaController.java

@@ -132,15 +132,18 @@ public class CaptchaController {
         String verifyKey = GlobalConstants.CAPTCHA_CODE_KEY + uuid;
         // 生成验证码
         CaptchaType captchaType = captchaProperties.getType();
-        boolean isMath = CaptchaType.MATH == captchaType;
-        Integer length = isMath ? captchaProperties.getNumberLength() : captchaProperties.getCharLength();
-        CodeGenerator codeGenerator = ReflectUtils.newInstance(captchaType.getClazz(), length);
+        CodeGenerator codeGenerator;
+        if (CaptchaType.MATH == captchaType) {
+            codeGenerator = ReflectUtils.newInstance(captchaType.getClazz(), captchaProperties.getNumberLength(), false);
+        } else {
+            codeGenerator = ReflectUtils.newInstance(captchaType.getClazz(), captchaProperties.getCharLength());
+        }
         AbstractCaptcha captcha = SpringUtils.getBean(captchaProperties.getCategory().getClazz());
         captcha.setGenerator(codeGenerator);
         captcha.createCode();
         // 如果是数学验证码,使用SpEL表达式处理验证码结果
         String code = captcha.getCode();
-        if (isMath) {
+        if (CaptchaType.MATH == captchaType) {
             ExpressionParser parser = new SpelExpressionParser();
             Expression exp = parser.parseExpression(StringUtils.remove(code, "="));
             code = exp.getValue(String.class);

+ 1 - 1
SERVER/VberAdminPlusV3/vber-admin/src/main/java/com/vber/web/service/SysRegisterService.java

@@ -82,7 +82,7 @@ public class SysRegisterService {
             recordLogininfor(tenantId, username, MessageUtils.message("user.jcaptcha.expire"));
             throw new CaptchaExpireException();
         }
-        if (!code.equalsIgnoreCase(captcha)) {
+        if (!StringUtils.equalsIgnoreCase(code, captcha)) {
             recordLogininfor(tenantId, username, MessageUtils.message("user.jcaptcha.error"));
             throw new CaptchaException();
         }

+ 1 - 1
SERVER/VberAdminPlusV3/vber-admin/src/main/java/com/vber/web/service/impl/PasswordAuthStrategy.java

@@ -89,7 +89,7 @@ public class PasswordAuthStrategy extends AuthStrategy {
             loginService.recordLogininfor(tenantId, username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"));
             throw new CaptchaExpireException();
         }
-        if (!code.equalsIgnoreCase(captcha)) {
+        if (!StringUtils.equalsIgnoreCase(code, captcha)) {
             loginService.recordLogininfor(tenantId, username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error"));
             throw new CaptchaException();
         }

+ 2 - 2
SERVER/VberAdminPlusV3/vber-common/vber-common-web/src/main/java/com/vber/common/web/enums/CaptchaType.java

@@ -1,8 +1,8 @@
 package com.vber.common.web.enums;
 
 import cn.hutool.captcha.generator.CodeGenerator;
+import cn.hutool.captcha.generator.MathGenerator;
 import cn.hutool.captcha.generator.RandomGenerator;
-import com.vber.common.web.utils.UnsignedMathGenerator;
 import lombok.AllArgsConstructor;
 import lombok.Getter;
 
@@ -18,7 +18,7 @@ public enum CaptchaType {
     /**
      * 数字
      */
-    MATH(UnsignedMathGenerator.class),
+    MATH(MathGenerator.class),
 
     /**
      * 字符

+ 0 - 88
SERVER/VberAdminPlusV3/vber-common/vber-common-web/src/main/java/com/vber/common/web/utils/UnsignedMathGenerator.java

@@ -1,88 +0,0 @@
-package com.vber.common.web.utils;
-
-import cn.hutool.captcha.generator.CodeGenerator;
-import cn.hutool.core.math.Calculator;
-import cn.hutool.core.util.CharUtil;
-import cn.hutool.core.util.RandomUtil;
-import com.vber.common.core.utils.StringUtils;
-
-import java.io.Serial;
-
-/**
- * 无符号计算生成器
- *
- * @author Iwb
- */
-public class UnsignedMathGenerator implements CodeGenerator {
-
-    @Serial
-    private static final long serialVersionUID = -5514819971774091076L;
-
-    private static final String OPERATORS = "+-*";
-
-    /**
-     * 参与计算数字最大长度
-     */
-    private final int numberLength;
-
-    /**
-     * 构造
-     */
-    public UnsignedMathGenerator() {
-        this(2);
-    }
-
-    /**
-     * 构造
-     *
-     * @param numberLength 参与计算最大数字位数
-     */
-    public UnsignedMathGenerator(int numberLength) {
-        this.numberLength = numberLength;
-    }
-
-    @Override
-    public String generate() {
-        final int limit = getLimit();
-        int a = RandomUtil.randomInt(limit);
-        int b = RandomUtil.randomInt(limit);
-        String max = Integer.toString(Math.max(a, b));
-        String min = Integer.toString(Math.min(a, b));
-        max = StringUtils.rightPad(max, this.numberLength, CharUtil.SPACE);
-        min = StringUtils.rightPad(min, this.numberLength, CharUtil.SPACE);
-
-        return max + RandomUtil.randomChar(OPERATORS) + min + '=';
-    }
-
-    @Override
-    public boolean verify(String code, String userInputCode) {
-        int result;
-        try {
-            result = Integer.parseInt(userInputCode);
-        } catch (NumberFormatException e) {
-            // 用户输入非数字
-            return false;
-        }
-
-        final int calculateResult = (int) Calculator.conversion(code);
-        return result == calculateResult;
-    }
-
-    /**
-     * 获取验证码长度
-     *
-     * @return 验证码长度
-     */
-    public int getLength() {
-        return this.numberLength * 2 + 2;
-    }
-
-    /**
-     * 根据长度获取参与计算数字最大值
-     *
-     * @return 最大值
-     */
-    private int getLimit() {
-        return Integer.parseInt("1" + StringUtils.repeat('0', this.numberLength));
-    }
-}