maven方式使用
<dependencies>
<!--图形验证码-->
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
redis配置
获取验证码图片
@GetMapping("/get******")
public Map get******() {
/*// png类型
SpecCaptcha captcha = new SpecCaptcha(130, 48);
// gif类型
GifCaptcha captcha = new GifCaptcha(130, 48);
// 中文类型
ChineseCaptcha captcha = new ChineseCaptcha(130, 48);
// 中文gif类型
ChineseGifCaptcha captcha = new ChineseGifCaptcha(130, 48);*/
//算术类型
ArithmeticCaptcha specCaptcha = new ArithmeticCaptcha(130, 48);
// 几位数运算,默认是两位
specCaptcha.setLen(2);
// 运算的结果
String verCode = specCaptcha.text();
//随机生产一个key 传到前台 用于登录是获取值
String key = OrderNumUtils.getOrderCode();
redisUtil.set("captcha:" + key, verCode, 1000L);
// 将key和base64返回给前端
Map<String, Object> map = new HashMap<>();
map.put("key", key);
map.put("image", specCaptcha.toBase64());
return map;
}
登陆时验证
@GetMapping("/******")
public String *****(String verCode, String verKey) {
// 获取redis中的验证码的值
String redisCode = redisUtil.get("captcha:" + verKey);
redisUtil.delete("captcha:" + verKey);
// 判断验证码
if (verCode == null || !redisCode.equals(verCode.trim().toLowerCase())) {
return "验证码不正确";
}
return "成功";
}
redisutil下载地址: https://gitee.com/nmwork/RedisUtil
详情请看: https://gitee.com/ele-admin/EasyCaptcha