一个强大且易于使用的 CAPTCHA 库,可用于 Java 应用程序,支持各种类型的 CAPTCHA,包括 GIF、中文、算术运算等。
一个强大且易于使用的 CAPTCHA 库,可用于 Java 应用程序,支持各种类型的 CAPTCHA,包括 GIF、中文、算术运算等。
算术类型:
中文类型:
内置字体:
dependencies {
implementation 'com.pig4cloud.plugin:easy-captcha:2.2.5'
}
com.pig4cloud.plugin
captcha-spring-boot-starter
2.2.5
注意:
1. 使用 Jakarta 时用
CaptchaJakartaUtil;使用 Javax 时使用CaptchaUtil;
2. 使用CaptchaUtil时需要根据使用情况引入对应的servlet依赖。
@Controller
public class CaptchaController {
@RequestMapping("/captcha")
public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 使用 Jakarta 时用 CaptchaJakartaUtil
CaptchaUtil.out(request, response);
}
}前端html代码:
不要忘了把
/captcha路径排除登录拦截,比如shiro的拦截。
web.xml 中配置 Javax servlet:
CaptchaServlet
com.pig4cloud.captcha.servlet.CaptchaServlet
CaptchaServlet
/captcha
web.xml 中配置 Jakarta servlet:
CaptchaServlet
com.pig4cloud.captcha.servlet.CaptchaJakartaServlet
CaptchaServlet
/captcha
前端html代码:
@Controller
public class LoginController {
@PostMapping("/login")
public JsonResult login(String username, String password, String verCode) {
// 使用 Jakarta 时用 CaptchaJakartaUtil
if (!CaptchaUtil.ver(verCode, request)) {
CaptchaUtil.clear(request); // 清除session中的验证码
return JsonResult.error("验证码不正确");
}
}
}@Controller
public class CaptchaController {
@RequestMapping("/captcha")
public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 设置位数
CaptchaUtil.out(5, request, response);
// 设置宽、高、位数
CaptchaUtil.out(130, 48, 5, request, response);
// 使用gif验证码
GifCaptcha gifCaptcha = new GifCaptcha(130,48,4);
CaptchaUtil.out(gifCaptcha, request, response);
}
}CaptchaUtil封装了输出验证码、存session、判断验证码等功能,也可以不使用此工具类:
……注意:
1. 算术验证码的len表示是几位数运算,而其他验证码的len表示验证码的位数,算术验证码的text()表示的是公式的结果, 对于算术验证码,你应该把公式的结果存储session,而不是公式。
2. 由于部分字体库的问题,除号可能无法显示
| 值 | 描述 |
|---|---|
| 2 | 加法 |
| 3 | 加减 |
| 4 | 加减乘 |
| 5 | 加减乘除 |
使用方法:
ArithmeticCaptcha arithmeticCaptcha=new ArithmeticCaptcha();
arithmeticCaptcha.supportAlgorithmSign(5);| 类型 | 描述 |
|---|---|
| TYPE_DEFAULT | 数字和字母混合 |
| TYPE_ONLY_NUMBER | 纯数字 |
| TYPE_ONLY_CHAR | 纯字母 |
| TYPE_ONLY_UPPER | 纯大写字母 |
| TYPE_ONLY_LOWER | 纯小写字母 |
| TYPE_NUM_AND_UPPER | 数字和大写字母 |
使用方法:
SpecCaptcha captcha=new SpecCaptcha(130,48,5);
captcha.setCharType(Captcha.TYPE_ONLY_NUMBER);只有
SpecCaptcha和GifCaptcha设置才有效果。
内置字体:
| 字体 | 效果 |
|---|---|
| Captcha.FONT_1 | |
| Captcha.FONT_2 | |
| Captcha.FONT_3 | |
| Captcha.FONT_4 | |
| Captcha.FONT_5 | |
| Captcha.FONT_6 | |
| Captcha.FONT_7 | |
| Captcha.FONT_8 | |
| Captcha.FONT_9 | |
| Captcha.FONT_10 |
使用方法:
SpecCaptcha captcha = new SpecCaptcha(130, 48, 5);
// 设置内置字体
captcha.setFont(Captcha.FONT_1);
// 设置系统字体
captcha.setFont(new Font("楷体", Font.PLAIN, 28)); SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
specCaptcha.toBase64();
// 如果不想要base64的头部data:image/png;base64,
specCaptcha.toBase64(""); // 加一个空的参数即可FileOutputStream outputStream = new FileOutputStream(new File("C:/captcha.png"))
SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
specCaptcha.out(outputStream);前后端分离项目建议不要存储在session中,存储在redis中,redis存储需要一个key,key一同返回给前端用于验证输入:
…前端使用ajax获取验证码:
RedisUtil到这里获取https://gitee.com/whvse/RedisUtil
继承Captcha实现out方法,中文验证码可继承ChineseCaptchaAbstract,算术验证码可继承ArithmeticCaptchaAbstract。
本项目 fork 源至EasyCaptcha , ele-admin/EasyCaptcha
暂无开放 Issues,或尚未同步最近议题。