commit
123ee3f17d
@ -0,0 +1,85 @@
|
|||||||
|
package com.ruoyi.gateway.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.core.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));
|
||||||
|
}
|
||||||
|
}
|
@ -1,75 +0,0 @@
|
|||||||
package com.ruoyi.gateway.config;
|
|
||||||
|
|
||||||
import java.util.Random;
|
|
||||||
import com.google.code.kaptcha.text.impl.DefaultTextCreator;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 验证码文本生成器
|
|
||||||
*
|
|
||||||
* @author ruoyi
|
|
||||||
*/
|
|
||||||
public class KaptchaTextCreator extends DefaultTextCreator
|
|
||||||
{
|
|
||||||
private static final String[] CNUMBERS = "0,1,2,3,4,5,6,7,8,9,10".split(",");
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getText()
|
|
||||||
{
|
|
||||||
Integer result = 0;
|
|
||||||
Random random = new Random();
|
|
||||||
int x = random.nextInt(10);
|
|
||||||
int y = random.nextInt(10);
|
|
||||||
StringBuilder suChinese = new StringBuilder();
|
|
||||||
int randomoperands = (int) Math.round(Math.random() * 2);
|
|
||||||
if (randomoperands == 0)
|
|
||||||
{
|
|
||||||
result = x * y;
|
|
||||||
suChinese.append(CNUMBERS[x]);
|
|
||||||
suChinese.append("*");
|
|
||||||
suChinese.append(CNUMBERS[y]);
|
|
||||||
}
|
|
||||||
else if (randomoperands == 1)
|
|
||||||
{
|
|
||||||
if (!(x == 0) && y % x == 0)
|
|
||||||
{
|
|
||||||
result = y / x;
|
|
||||||
suChinese.append(CNUMBERS[y]);
|
|
||||||
suChinese.append("/");
|
|
||||||
suChinese.append(CNUMBERS[x]);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
result = x + y;
|
|
||||||
suChinese.append(CNUMBERS[x]);
|
|
||||||
suChinese.append("+");
|
|
||||||
suChinese.append(CNUMBERS[y]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (randomoperands == 2)
|
|
||||||
{
|
|
||||||
if (x >= y)
|
|
||||||
{
|
|
||||||
result = x - y;
|
|
||||||
suChinese.append(CNUMBERS[x]);
|
|
||||||
suChinese.append("-");
|
|
||||||
suChinese.append(CNUMBERS[y]);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
result = y - x;
|
|
||||||
suChinese.append(CNUMBERS[y]);
|
|
||||||
suChinese.append("-");
|
|
||||||
suChinese.append(CNUMBERS[x]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
result = x + y;
|
|
||||||
suChinese.append(CNUMBERS[x]);
|
|
||||||
suChinese.append("+");
|
|
||||||
suChinese.append(CNUMBERS[y]);
|
|
||||||
}
|
|
||||||
suChinese.append("=?@" + result);
|
|
||||||
return suChinese.toString();
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,35 @@
|
|||||||
|
package com.ruoyi.gateway.enums;
|
||||||
|
|
||||||
|
import cn.hutool.captcha.AbstractCaptcha;
|
||||||
|
import cn.hutool.captcha.CircleCaptcha;
|
||||||
|
import cn.hutool.captcha.LineCaptcha;
|
||||||
|
import cn.hutool.captcha.ShearCaptcha;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证码类别
|
||||||
|
*
|
||||||
|
* @author Lion Li
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public enum CaptchaCategory {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 线段干扰
|
||||||
|
*/
|
||||||
|
LINE(LineCaptcha.class),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 圆圈干扰
|
||||||
|
*/
|
||||||
|
CIRCLE(CircleCaptcha.class),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 扭曲干扰
|
||||||
|
*/
|
||||||
|
SHEAR(ShearCaptcha.class);
|
||||||
|
|
||||||
|
private final Class<? extends AbstractCaptcha> clazz;
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.ruoyi.gateway.enums;
|
||||||
|
|
||||||
|
import cn.hutool.captcha.generator.CodeGenerator;
|
||||||
|
import cn.hutool.captcha.generator.RandomGenerator;
|
||||||
|
import com.ruoyi.gateway.captcha.UnsignedMathGenerator;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证码类型
|
||||||
|
*
|
||||||
|
* @author Lion Li
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public enum CaptchaType {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数字
|
||||||
|
*/
|
||||||
|
MATH(UnsignedMathGenerator.class),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字符
|
||||||
|
*/
|
||||||
|
CHAR(RandomGenerator.class);
|
||||||
|
|
||||||
|
private final Class<? extends CodeGenerator> clazz;
|
||||||
|
}
|
Loading…
Reference in New Issue