提供接口将生成的验证码存入session或redis,将验证码以图片格式或者base64编码串返回给调用端。
提供接口将收到调用端传过来的校验码,从session或redis取出验证码,两个验证码都全部转小写,进行无大小写区分匹配校验,返回true/flase 。
<dependency>
<groupId>cn.hutoolgroupId>
<artifactId>hutool-allartifactId>
<version>5.7.12version>
dependency>
@Data
public class VerifyCodeResp implements Serializable {
/**
* header头参数:Captcha-Key
*/
private String captchaKey;
/**
* 验证码图片
*/
private String captchaImg;
}
/**
* 验证码工具类
*/
@RestController
public class VerifyCodeController {
@Resource
RedisTemplate<String, String> redisTemplate;
/**
* 生成验证码
*
* 方法一 ShearCaptcha
* 图片格式
* session存储
* 接口需添加白名单放行
*
* @param request HttpServletRequest
*/
@GetMapping("/verify")
public void verify(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("image/jpeg");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
//定义图形验证码的长、宽、验证码字符数、干扰线宽度
ShearCaptcha shearCaptcha = CaptchaUtil.createShearCaptcha(150, 40, 5, 4);
//图形验证码写出,可以写出到文件,也可以写出到流
shearCaptcha.write(response.getOutputStream());
//获取验证码中的文字内容
request.getSession().setAttribute("verifyCode", shearCaptcha.getCode());
}
/**
* 生成验证码
*
* 方法二 LineCaptcha
* 图片格式
* session存储
* 接口需添加白名单放行
*
* @param request HttpServletRequest
*/
@GetMapping("/verifyTwo")
public void verifyTwo(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("image/jpeg");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
//定义图形验证码的长、宽、验证码字符数、干扰线宽度
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(150, 40, 5, 4);
//图形验证码写出,可以写出到文件,也可以写出到流
ImageIO.write(lineCaptcha.getImage(), "JPEG", response.getOutputStream());
//获取验证码中的文字内容
request.getSession().setAttribute("verifyCode", lineCaptcha.getCode());
}
/**
* 方法三 ShearCaptcha
* 图片的base64编码字符串
* session存储
* 接口需添加白名单放行
*
* @param request HttpServletRequest
* @return String
*/
@GetMapping("/getVerify")
public String getVerify(HttpServletRequest request) {
//定义图形验证码的长、宽、验证码字符数、干扰线宽度
ShearCaptcha shearCaptcha = CaptchaUtil.createShearCaptcha(150, 40, 5, 4);
//获取验证码中的文字内容
request.getSession().setAttribute("verifyCode", shearCaptcha.getCode());
String base64String = "";
try {
base64String = "data:image/png;base64," + shearCaptcha.getImageBase64();
} catch (Exception e) {
e.printStackTrace();
}
return base64String;
}
/**
* 方法四 LineCaptcha
* 图片的base64编码字符串
* session存储
* 接口需添加白名单放行
*
* @param request HttpServletRequest
* @return String
*/
@GetMapping("/getVerifyTwo")
public String getVerifyTwo(HttpServletRequest request) {
//定义图形验证码的长、宽、验证码字符数、干扰线宽度
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(150, 40, 5, 4);
//获取验证码中的文字内容
request.getSession().setAttribute("verifyCode", lineCaptcha.getCode());
String base64String = "";
try {
//返回 base64
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(lineCaptcha.getImage(), "JPEG", bos);
byte[] bytes = bos.toByteArray();
Base64.Encoder encoder = Base64.getEncoder();
base64String = "data:image/png;base64," + encoder.encodeToString(bytes);
} catch (Exception e) {
e.printStackTrace();
}
return base64String;
}
/**
* 方法五 ShearCaptcha
* 图片的base64编码字符串
* redis存储
* 接口需添加白名单放行
*
* @return String
*/
@GetMapping("/getVerifyThree")
public VerifyCodeResp getVerifyThree() {
String captchaKey = UUID.randomUUID().toString();
//定义图形验证码的长、宽、验证码字符数、干扰线宽度
ShearCaptcha shearCaptcha = CaptchaUtil.createShearCaptcha(150, 40, 5, 0);
// 存入redis并设置过期时间为30分钟
redisTemplate.opsForValue().set("captcha:" + captchaKey, shearCaptcha.getCode(), 30L, TimeUnit.MINUTES);
String base64String = "";
try {
base64String = "data:image/png;base64," + shearCaptcha.getImageBase64();
} catch (Exception e) {
e.printStackTrace();
}
VerifyCodeResp verifyCodeResp = new VerifyCodeResp();
verifyCodeResp.setCaptchaKey(captchaKey);
verifyCodeResp.setCaptchaImg(base64String);
return verifyCodeResp;
}
/**
* 方法六 LineCaptcha
* 图片的base64编码字符串
* redis存储
* 接口需添加白名单放行
*
* @return String
*/
@GetMapping("/getVerifyFour")
public VerifyCodeResp getVerifyFour() {
String captchaKey = UUID.randomUUID().toString();
//定义图形验证码的长、宽、验证码字符数、干扰线宽度
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(150, 40, 5, 4);
// 存入redis并设置过期时间为30分钟
redisTemplate.opsForValue().set("captcha:" + captchaKey, lineCaptcha.getCode(), 30L, TimeUnit.MINUTES);
String base64String = "";
try {
//返回 base64
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(lineCaptcha.getImage(), "JPEG", bos);
byte[] bytes = bos.toByteArray();
Base64.Encoder encoder = Base64.getEncoder();
base64String = "data:image/png;base64," + encoder.encodeToString(bytes);
} catch (Exception e) {
e.printStackTrace();
}
VerifyCodeResp verifyCodeResp = new VerifyCodeResp();
verifyCodeResp.setCaptchaKey(captchaKey);
verifyCodeResp.setCaptchaImg(base64String);
return verifyCodeResp;
}
}
如果测试验证码存入redis,则需要提前配置和启动redis。