若依改用EasyCaptcha验证码
若依自带的验证码样式比较单一,所以想改用EasyCaptcha验证码,另外EasyCaptcha算术验证码可能会有负数,输入时需要写负号,比较麻烦,所以使用一个简单的方法过滤掉负数结果
原本的验证码依赖和代码可删可不删,注释掉即可
在父模块添加依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14<properties>
<captcha.version>1.6.2</captcha.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- EasyCaptcha验证码依赖 -->
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>${captcha.version}</version>
</dependency>
</dependencies>
</dependencyManagement>ruoyi-admin模块中添加依赖
1
2
3
4
5<!-- 验证码 -->
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
</dependency>找到
CaptchaController,java
,修改getCode
方法1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74public AjaxResult getCode(HttpServletResponse response) throws IOException, FontFormatException {
AjaxResult ajax = AjaxResult.success();
boolean captchaEnabled = configService.selectCaptchaEnabled();
ajax.put("captchaEnabled", captchaEnabled);
if (!captchaEnabled)
{
return ajax;
}
// 保存验证码信息
String uuid = IdUtils.simpleUUID();
String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
//新验证码
//图形验证码
//SpecCaptcha captcha = new SpecCaptcha(130, 48, 4);
//算术验证码
ArithmeticCaptcha captcha;
String code;
captcha = new ArithmeticCaptcha(130, 48);
//得到验证码的值
code = captcha.text();
//若为负数则重新生成
int i = Integer.parseInt(code);
while (i < 0){
System.out.println("code = " + code + ",负数,重新生成!!!!!!!!");
captcha = new ArithmeticCaptcha(130, 48);
code = captcha.text();
i = Integer.parseInt(code);
System.out.println("i = " + i + ",新值!!!");
}
// 设置内置字体
captcha.setFont(Captcha.FONT_1);
//captcha.setLen(2); // 几位数运算,默认是两位
//captcha.getArithmeticString(); // 获取运算的公式
//存入redis
redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
//旧验证码
//String capStr = null, code = null;
//BufferedImage image = null;
//
//// 生成验证码
//String captchaType = RuoYiConfig.getCaptchaType();
//if ("math".equals(captchaType))
//{
// String capText = captchaProducerMath.createText();
// capStr = capText.substring(0, capText.lastIndexOf("@"));
// code = capText.substring(capText.lastIndexOf("@") + 1);
// image = captchaProducerMath.createImage(capStr);
//}
//else if ("char".equals(captchaType))
//{
// capStr = code = captchaProducer.createText();
// image = captchaProducer.createImage(capStr);
//}
//
//redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
//// 转换流信息写出
//FastByteArrayOutputStream os = new FastByteArrayOutputStream();
//try
//{
// ImageIO.write(image, "jpg", os);
//}
//catch (IOException e)
//{
// return AjaxResult.error(e.getMessage());
//}
ajax.put("uuid", uuid);
ajax.put("img", captcha.toBase64());
return ajax;
}
本网站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 火柴人儿的小站!
评论