疯狂的狮子li
2021-05-19 db18050b869f71efe35dc4cea36ed485d78f17f8
update 验证码生成更新为无符号整数计算
已修改1个文件
已添加1个文件
89 ■■■■■ 文件已修改
ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-framework/src/main/java/com/ruoyi/framework/captcha/UnsignedMathGenerator.java 85 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java
@@ -5,7 +5,6 @@
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.captcha.generator.CodeGenerator;
import cn.hutool.captcha.generator.MathGenerator;
import cn.hutool.captcha.generator.RandomGenerator;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.IdUtil;
@@ -13,6 +12,7 @@
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.framework.captcha.UnsignedMathGenerator;
import com.ruoyi.framework.config.properties.CaptchaProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
@@ -59,7 +59,7 @@
        AbstractCaptcha captcha;
        switch (captchaProperties.getType()) {
            case "math":
                codeGenerator = new MathGenerator(captchaProperties.getNumberLength());
                codeGenerator = new UnsignedMathGenerator(captchaProperties.getNumberLength());
                break;
            case "char":
                codeGenerator = new RandomGenerator(captchaProperties.getCharLength());
ruoyi-framework/src/main/java/com/ruoyi/framework/captcha/UnsignedMathGenerator.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,85 @@
package com.ruoyi.framework.captcha;
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 cn.hutool.core.util.StrUtil;
/**
 * æ— ç¬¦å·è®¡ç®—生成器
 *
 * @author Lion Li
 */
public class UnsignedMathGenerator implements CodeGenerator {
    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 min = RandomUtil.randomInt(limit);
        int max = RandomUtil.randomInt(min, limit);
        String number1 = Integer.toString(max);
        String number2 = Integer.toString(min);
        number1 = StrUtil.padAfter(number1, this.numberLength, CharUtil.SPACE);
        number2 = StrUtil.padAfter(number2, this.numberLength, CharUtil.SPACE);
        return number1 + RandomUtil.randomChar(operators) + number2 + '=';
    }
    @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" + StrUtil.repeat('0', this.numberLength));
    }
}