添加依赖
<dependency>
    <groupId>com.github.whvcse</groupId>
    <artifactId>easy-captcha</artifactId>
    <version>1.6.2</version>
</dependency>
开源源码地址
github地址:
使用方式
//http://localhost:2020/test/captcha
@RequestMapping("/captcha")
public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
    // 设置请求头为输出图片类型
    response.setContentType("image/gif");
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    // 三个参数分别为宽、高、位数
    SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
    // 设置字体
    // specCaptcha.setFont(new Font("Verdana", Font.PLAIN, 32));  // 有默认字体,可以不用设置
    specCaptcha.setFont(Captcha.FONT_1);
    // 设置类型,纯数字、纯字母、字母数字混合
    //specCaptcha.setCharType(Captcha.TYPE_ONLY_NUMBER);
    specCaptcha.setCharType(Captcha.TYPE_DEFAULT);
    String content = specCaptcha.text().toLowerCase();
    log.info("captcha content == > " + content);
    // 验证码存入session
    request.getSession().setAttribute("captcha", content);
    // 输出图片流
    specCaptcha.out(response.getOutputStream());
}
相关说明
图灵验证码的使用
- 前端请求图灵验证码
- 后台生成图灵验证码,并且保存到session里。
==========================================
- 用户提交注册信息(携带了用户所输入的图灵验证码内容)
- 从session中拿出存储的验证码内容跟用户提交的验证码内容进行比较
- 返回结果
session:会话,可以在属性中存储简单的数据
前后端分离:
- 前端生成随机数以参数的形式添加到请求图灵验证码的url上
- 后台生成图灵验证码,返回结果,并且保存到redis里 key-value,并且设置有效期为10分钟
===============================================
- 用户提交注册信息(携带了用户所输入的图灵验证码内容+key)
- 根据key从redis里拿出生成的验证码内容,与用户输入提交上来的进行对比
- 如果正确,删除redis里的记录。进行注册/do sth. 如果不正确,返回结果给前端



























