简单数字验证码
parent
325725b7bb
commit
32dc3e8284
|
|
@ -34,6 +34,9 @@ public class CaptchaController {
|
|||
@Resource(name = "captchaProducerMath")
|
||||
private Producer captchaProducerMath;
|
||||
|
||||
@Resource(name = "captchaProducerNum")
|
||||
private Producer captchaProducerNum;
|
||||
|
||||
@Autowired
|
||||
private RedisCache redisCache;
|
||||
|
||||
|
|
@ -69,6 +72,9 @@ public class CaptchaController {
|
|||
} else if ("char".equals(captchaType)) {
|
||||
capStr = code = captchaProducer.createText();
|
||||
image = captchaProducer.createImage(capStr);
|
||||
} else if ("num".equals(captchaType)) {
|
||||
capStr = code = captchaProducerNum.createText();
|
||||
image = captchaProducerNum.createImage(capStr);
|
||||
}
|
||||
|
||||
redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
|
||||
|
|
|
|||
|
|
@ -10,8 +10,8 @@ ruoyi:
|
|||
profile: D:/ruoyi/uploadPath
|
||||
# 获取ip地址开关
|
||||
addressEnabled: false
|
||||
# 验证码类型 math 数字计算 char 字符验证
|
||||
captchaType: math
|
||||
# 验证码类型 math 数字计算 char 字符验证 num 纯数字
|
||||
captchaType: num
|
||||
oss:
|
||||
endPoint: oss-cn-beijing.aliyuncs.com
|
||||
accessKeyId: LTAI5tFXTfY5Rsiwvrg9gUuk
|
||||
|
|
|
|||
|
|
@ -80,4 +80,42 @@ public class CaptchaConfig
|
|||
defaultKaptcha.setConfig(config);
|
||||
return defaultKaptcha;
|
||||
}
|
||||
|
||||
@Bean(name = "captchaProducerNum")
|
||||
public DefaultKaptcha getKaptchaBeanNum()
|
||||
{
|
||||
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
|
||||
Properties properties = new Properties();
|
||||
// 是否有边框 默认为true 我们可以自己设置yes,no
|
||||
properties.setProperty(KAPTCHA_BORDER, "yes");
|
||||
// 边框颜色 默认为Color.BLACK
|
||||
properties.setProperty(KAPTCHA_BORDER_COLOR, "105,179,90");
|
||||
// 验证码文本字符颜色 默认为Color.BLACK
|
||||
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "blue");
|
||||
// 验证码图片宽度 默认为200
|
||||
properties.setProperty(KAPTCHA_IMAGE_WIDTH, "160");
|
||||
// 验证码图片高度 默认为50
|
||||
properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "60");
|
||||
// 验证码文本字符大小 默认为40
|
||||
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "40");
|
||||
// KAPTCHA_SESSION_KEY
|
||||
properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCodeNum");
|
||||
// 验证码文本生成器
|
||||
properties.setProperty(KAPTCHA_TEXTPRODUCER_IMPL, "com.ruoyi.framework.config.KaptchaNumberCreator");
|
||||
// 验证码文本字符间距 默认为2
|
||||
properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, "8");
|
||||
// 验证码文本字符长度 默认为5
|
||||
properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "5");
|
||||
// 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
|
||||
// properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "宋体");
|
||||
// 验证码噪点颜色 默认为Color.BLACK
|
||||
properties.setProperty(KAPTCHA_NOISE_COLOR, "white");
|
||||
// 干扰实现类
|
||||
properties.setProperty(KAPTCHA_NOISE_IMPL, "com.google.code.kaptcha.impl.NoNoise");
|
||||
// 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy
|
||||
properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.ruoyi.framework.config.NoGimpy");
|
||||
Config config = new Config(properties);
|
||||
defaultKaptcha.setConfig(config);
|
||||
return defaultKaptcha;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
package com.ruoyi.framework.config;
|
||||
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import com.google.code.kaptcha.text.impl.DefaultTextCreator;
|
||||
|
||||
/**
|
||||
* 验证码文本生成器
|
||||
*
|
||||
* @author liangyq
|
||||
*/
|
||||
public class KaptchaNumberCreator extends DefaultTextCreator {
|
||||
|
||||
@Override
|
||||
public String getText() {
|
||||
return RandomUtil.randomNumbers(5);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
package com.ruoyi.framework.config;
|
||||
|
||||
import com.google.code.kaptcha.GimpyEngine;
|
||||
import com.google.code.kaptcha.util.Configurable;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
/**
|
||||
* @author liangyq
|
||||
*/
|
||||
public class NoGimpy extends Configurable implements GimpyEngine {
|
||||
|
||||
@Override
|
||||
public BufferedImage getDistortedImage(BufferedImage baseImage) {
|
||||
return baseImage;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue