利用Java工具类Hutool实现验证码校验功能
作者:愚生浅末 时间:2022-07-15 02:31:22
第一篇是纯利用现有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中加入以下内容:
<dependency>
Maven相关可参阅:idea创建Maven项目
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.8</version>
</dependency>
生成验证码:
设置验证码长、宽、验证码字符数、干扰元素个数:
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文件:
<servlet>
<servlet-name>ImageGenerate</servlet-name>
<servlet-class>com.kailong.servlet.ImageGenerate</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ImageGenerate</servlet-name>
<url-pattern>/imageGenerate</url-pattern>
</servlet-mapping>
其他样式的验证码
上面展示的验证码是线段干扰样式的验证码,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();
}
2. 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();
}
3. 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/aeLqqgsqGBj3eqLilRtS-w
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
搭建MyBatis-Plus框架并进行数据库增删改查功能
![](https://img.aspxhome.com/file/2023/7/59167_0s.png)
C#二维数组与多维数组的具体使用
![](https://img.aspxhome.com/file/2023/5/82185_0s.jpg)
dubbo服务链路跟踪方式
![](https://img.aspxhome.com/file/2023/6/58296_0s.png)
Java自动生成趋势比对数据的方法分享
![](https://img.aspxhome.com/file/2023/7/61087_0s.jpg)
springboot配置aop切面日志打印过程解析
C# Winform 分页功能的实现
![](https://img.aspxhome.com/file/2023/1/69241_0s.png)
SpringCloud如何使用Eureka实现服务之间的传递数据
C#实现xml文件反序列化读入数据到object的方法
Spring实战之注入集合值操作示例
Spring Boot环境属性占位符解析及类型转换详解
![](https://img.aspxhome.com/file/2023/4/66524_0s.png)
Java常用流程控制语句实现原理解析
图解Java排序算法之快速排序的三数取中法
![](https://img.aspxhome.com/file/2023/9/65159_0s.png)
SpringBoot 集成 Memcached的方法示例
Java中synchronized的几种使用方法
![](https://img.aspxhome.com/file/2023/8/62228_0s.webp)
SpringBoot整合WebService的实现示例
![](https://img.aspxhome.com/file/2023/4/60754_0s.png)
Java使用过滤器防止SQL注入XSS脚本注入的实现
![](https://img.aspxhome.com/file/2023/8/68528_0s.png)
Java synchronized锁升级jol过程详解
![](https://img.aspxhome.com/file/2023/4/61864_0s.jpg)
java spring mvc处理器映射器介绍
使用java实现各种数据统计图(柱形图,饼图,折线图)
![](https://img.aspxhome.com/file/2023/5/81365_0s.png)