利用Hutool-(Java工具类)实现验证码校验


目录
  • 第一篇是纯利用现有JDK提供的绘图类(ImageIO)类制作,这个过程比较复杂且需要了解ImageIO类。

    这一篇文章是利用Hutool工具类来实现的,该工具类已经封装验证码所需的相关类等,使用起来较为简单和方便。

    Hutool工具类介绍

    Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。

    Hutool中的工具方法来自每个用户的精雕细琢,它涵盖了Java开发底层代码中的方方面面,它既是大型项目开发中解决小问题的利器,也是小型项目中的效率担当;

    • Web开发
    • 与其它框架无耦合
    • 高度可替换

    Hutool官方网站:https://hutool.cn/

    Hutool实现验证码生成

    利用Hutool实现验证码校验,校验的Servlet与今天的第一篇是一样的,唯一就是验证码的生成是不一样的。利用Hutool生成验证码更快捷。

    获取Hutool:

    • jar包下载:https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.8.8/

    • Maven:在项目的pom.xml的dependencies中加入以下内容:

      
          cn.hutool
          hutool-all
          5.8.8
      
      

      Maven相关可参阅:idea创建Maven项目

    生成验证码:

    设置验证码长、宽、验证码字符数、干扰元素个数:

    LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(100,30,4,25);
    

    在页面显示验证码及保存验证码内容到Session:

    	try{
            lineCaptcha.write(response.getOutputStream());
            String code = lineCaptcha.getCode();//获取验证码内容
            request.getSession().setAttribute("piccode",code);
            response.getOutputStream().close();
            }catch (IOException e){
                e.printStackTrace();
            }
    

    这个就实现了验证码的生成,相比于第一篇自己制作简洁了许多。

    完整代码:

    ImageGenerate.java

    public class ImageGenerate extends HttpServlet {
        public void doGet (HttpServletRequest request,HttpServletResponse response) {
            LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(100,30,4,25);
            response.setContentType("image/jpeg");
            response.setHeader("Pragma", "No-cache");
            try{
            lineCaptcha.write(response.getOutputStream());
            String code = lineCaptcha.getCode();
            request.getSession().setAttribute("piccode",code);
            response.getOutputStream().close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
    

    测试验证码生成

    还是需要先配置web.xml文件:

     	
            ImageGenerate
            com.kailong.servlet.ImageGenerate
        
        
            ImageGenerate
            /imageGenerate
        
    

    其他样式的验证码

    上面展示的验证码是线段干扰样式的验证码,Hutool工具类还提供了其他样式的验证码:

    1. CircleCaptcha -圆圈干扰验证码

    例:

    //定义图形验证码的长、宽、验证码字符数、干扰元素个数
    CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
    try{
        lineCaptcha.write(response.getOutputStream());
        String code = lineCaptcha.getCode();//获取验证码内容
        request.getSession().setAttribute("piccode",code);
        response.getOutputStream().close();
    }catch (IOException e){
        e.printStackTrace();
    }
    
    1. ShearCaptcha 扭曲干扰验证码

    例:

    //定义图形验证码的长、宽、验证码字符数、干扰线宽度
    ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 100, 4, 4);
    try{
        lineCaptcha.write(response.getOutputStream());
        String code = lineCaptcha.getCode();//获取验证码内容
        request.getSession().setAttribute("piccode",code);
        response.getOutputStream().close();
    }catch (IOException e){
        e.printStackTrace();
    }
    
    1. Hutool还提供了自定义验证码

    有时候标准的验证码不满足要求,比如我们希望使用纯字母的验证码、纯数字的验证码、加减乘除的验证码,此时我们就要自定义CodeGenerator

    例:

    // 自定义纯数字的验证码(随机4位数字,可重复)
    RandomGenerator randomGenerator = new RandomGenerator("0123456789", 4);
    LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
    lineCaptcha.setGenerator(randomGenerator);
    // 重新生成code
    lineCaptcha.createCode();
    
    ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 45, 4, 4);
    // 自定义验证码内容为四则运算方式
    captcha.setGenerator(new MathGenerator());
    // 重新生成code
    captcha.createCode();
    

    公众号本文地址:https://mp.weixin.qq.com/s/XHucabQ_WwUx2OMDGSTMkw
    头条本文地址:https://www.toutiao.com/item/7154266494900191757/

    欢迎关注公众号:愚生浅末