一、实现方式
使用easy-captcha类库实现获取验证码,并调用RedisTemplate的方法进行设置。可自行参考地址easy-captcha: https://github.com/whvcse/EasyCaptcha增强JAVA11
二、具体实现之基础准备
1、redis键的常量类 RedisConstans.java
/**
* redis键的常量
*/
public class RedisConstans {
/**
* 验证码的前缀
*/
public static final String CAPTCHA_KEY="captcha:";
/**
* 验证码的5分钟过期
*/
public static final Integer CAPTCHA_EXPIRED_MINUTE=5;
}
2、统一响应模型 R.java
/**
* redis键的常量
*/
@Getter
@Setter
public class R<T>{
private Integer code;
private String msg;
private T data;
public synchronized static <T> R<T> build(AckCode statusEnum) {
return build(statusEnum, null);
}
public synchronized static <T> R<T> build(AckCode ackCode, T data) {
R<T> res = new R<>();
res.setCode(ackCode.getCode());
res.setMsg(ackCode.getMsg());
res.setData(data);
return res;
}
public synchronized static <T> R<T> build(AckCode ackCode, T data, String msg) {
R<T> res = new R<>();
res.setCode(ackCode.getCode());
res.setMsg(msg);
res.setData(data);
return res;
}
public synchronized static <T> R<T> ok() {
return build(AckCode.SUCCESS, null);
}
public synchronized static <T> R<T> okHasData(T data) {
return build(AckCode.SUCCESS, data);
}
}
三、具体实现
1、Contoller层
注:因为验证码不需要被拦截,所以是匿名访问的。
/**
* 匿名访问图片验证码
*/
@RestController
@RequestMapping(value = "/anon")
@Api(tags = "通用-图形验证码Api")
public class CaptchaController {
@Autowired
private RedisTemplate redisTemplate;
@ApiOperation(value = "gif图形验证码")
@GetMapping(value = "/captcha/gif")
public R gifCaptcha(@RequestParam(value = "图片宽度",defaultValue = "150") Integer width,
@RequestParam(value = "图片高度",defaultValue = "50")Integer height,
@RequestParam(value = "验证码长度",defaultValue = "4")Integer len){
return this.captcha(width,height,len,"gif");
}
@ApiOperation(value = "png图形验证码")
@GetMapping(value = "/captcha/png")
public R pngCaptcha(@RequestParam(value = "图片宽度",defaultValue = "150") Integer width,
@RequestParam(value = "图片高度",defaultValue = "50")Integer height,
@RequestParam(value = "验证码长度",defaultValue = "4")Integer len){
return this.captcha(width,height,len,"pgn");
}
@ApiOperation(value = "算术运算图形验证码")
@GetMapping(value = "/captcha/airth")
public R airthCaptcha(@RequestParam(value = "图片宽度",defaultValue = "150") Integer width,
@RequestParam(value = "图片高度",defaultValue = "50")Integer height,
@RequestParam(value = "验证码长度",defaultValue = "2")Integer len){
return this.captcha(width,height,len,"airth");
}
/**
* 图形验证码
* @param width 图片宽
* @param height 图片高
* @param len 验证码长度
* @param type 验证码类型
* @return
*/
private R captcha(Integer width, Integer height, Integer len, String type){
//定义一个验证码为空
Captcha captcha=null;
//判断传入类型
if ("gif".equals(type)){
captcha=new GifCaptcha(width,height,len);
}else if ("png".equals(type)){
captcha=new SpecCaptcha(width,height,len);
}else {
captcha=new ArithmeticCaptcha(width,height,len);
}
//给验证码赋值
String value=captcha.text();
String image=captcha.toBase64();
String uuid = UUID.randomUUID().toString();
String redisKey= RedisConstans.CAPTCHA_KEY + uuid;
this.redisTemplate.opsForValue().set(redisKey,value,RedisConstans.CAPTCHA_EXPIRED_MINUTE, TimeUnit.MINUTES);
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("uuid",uuid);
dataMap.put("image",image);
return R.okHasData(dataMap);
}
}
四、Swagger调试
1、Swagger调试界面
图4-1-1 swagger之验证码调试
2、验证码页面展示(复制地址到浏览器)
图4-2-1 页面展示的验证码