使用
1、导入依赖
在maven仓库中查找,发现只有这个依赖,所以直接复制这个即可
<!--easy-captcha 验证码-->
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
2、使用
总共有这么多种验证类型

3、测试
package com.walker.third.easyCaptcha;
import com.wf.captcha.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@RestController
public class CaptchaController {
/**
* 数字验证码
*/
@GetMapping("/arithmeticCaptcha")
public void captcha(HttpServletResponse response) throws IOException {
ArithmeticCaptcha captcha = new ArithmeticCaptcha(138, 54);
captcha.setLen(3);
String arithmeticString = captcha.getArithmeticString();
//获取算数过程的字符串
System.out.println(arithmeticString);
//获取算数结果
System.out.println(captcha.text());
response.setContentType("image/gif");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
captcha.out(response.getOutputStream());
}
/**
* 中文验证码
*/
@GetMapping("chineseCaptcha")
public void chineseCaptcha(HttpServletResponse response) throws IOException {
ChineseCaptcha chineseCaptcha = new ChineseCaptcha();
chineseCaptcha.setLen(5);
chineseCaptcha.out(response.getOutputStream());
}
/**
* 字母闪烁图片
*/
@GetMapping("gifCaptcha")
public void gifCaptcha(HttpServletResponse response) throws IOException {
GifCaptcha gifCaptcha = new GifCaptcha();
gifCaptcha.out(response.getOutputStream());
}
/**
* 中文闪烁图片
*/
@GetMapping("chineseGifCaptcha")
public void chineseGifCaptcha(HttpServletResponse response) throws IOException {
ChineseGifCaptcha chineseGifCaptcha = new ChineseGifCaptcha();
chineseGifCaptcha.out(response.getOutputStream());
}
/**
* 字母图片
*/
@GetMapping("specCaptcha")
public void spec(HttpServletResponse response) throws IOException {
SpecCaptcha specCaptcha = new SpecCaptcha();
specCaptcha.out(response.getOutputStream());
}
}
具体结果如下:
算数ArithmeticCaptcha:

中文ChineseCaptcha:

GifCaptcha
不断闪烁

SpecCaptcha:英文字母

中文gif:中文不断闪烁

4、最佳实践
可以将其与uuid和验证码结果保存在缓存中,如果是本地,可以保存在guava中,也可以保存在redis中,或者mysql数据库也可以(在数据量相对较小的情况下)
本文介绍了如何在Spring Boot应用中集成Easy-Captcha库,提供了数字、中文、字母闪烁及静态验证码的使用示例,并探讨了缓存验证码结果的最佳实践,包括与Guava、Redis或数据库存储的结合。
4448

被折叠的 条评论
为什么被折叠?



