update 优化验证码配置 使用泛型 防止错误输入

This commit is contained in:
疯狂的狮子li
2021-09-02 15:19:18 +08:00
parent 76710e453e
commit 8b2f7f0e64
7 changed files with 101 additions and 66 deletions

View File

@ -1,85 +0,0 @@
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 com.ruoyi.common.utils.StringUtils;
/**
* 无符号计算生成器
*
* @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 = StringUtils.rightPad(number1, this.numberLength, CharUtil.SPACE);
number2 = StringUtils.rightPad(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" + StringUtils.repeat('0', this.numberLength));
}
}

View File

@ -1,10 +1,14 @@
package com.ruoyi.framework.config;
import java.awt.*;
import cn.hutool.captcha.*;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.ShearCaptcha;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import java.awt.*;
/**
* 验证码配置
@ -22,8 +26,9 @@ public class CaptchaConfig {
/**
* 圆圈干扰验证码
*/
@Bean(name = "CircleCaptcha")
public CircleCaptcha getCircleCaptcha() {
@Lazy
@Bean
public CircleCaptcha circleCaptcha() {
CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(width, height);
captcha.setBackground(background);
captcha.setFont(font);
@ -33,8 +38,9 @@ public class CaptchaConfig {
/**
* 线段干扰的验证码
*/
@Bean(name = "LineCaptcha")
public LineCaptcha getLineCaptcha() {
@Lazy
@Bean
public LineCaptcha lineCaptcha() {
LineCaptcha captcha = CaptchaUtil.createLineCaptcha(width, height);
captcha.setBackground(background);
captcha.setFont(font);
@ -44,8 +50,9 @@ public class CaptchaConfig {
/**
* 扭曲干扰验证码
*/
@Bean(name = "ShearCaptcha")
public ShearCaptcha getShearCaptcha() {
@Lazy
@Bean
public ShearCaptcha shearCaptcha() {
ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(width, height);
captcha.setBackground(background);
captcha.setFont(font);

View File

@ -1,5 +1,7 @@
package com.ruoyi.framework.config.properties;
import com.ruoyi.common.enums.CaptchaCategory;
import com.ruoyi.common.enums.CaptchaType;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ -17,12 +19,12 @@ public class CaptchaProperties {
/**
* 验证码类型
*/
private String type;
private CaptchaType type;
/**
* 验证码类别
*/
private String category;
private CaptchaCategory category;
/**
* 数字验证码位数