Java生成动态版验证码的方法实例
作者:坏蛋 时间:2023-08-26 03:53:08
前言
相对来说呢,jpg格式的相对来说容易破解一点,当然也取决于你的干扰元素,元素越复杂,破解也就难度越高,有的加的多,人都识别不出来了,何况人呢。都是概率问题。
GIF格式 + 干扰元素,那么验证码破解难度又上了一个层次
上代码:
/**
* 获取验证码(Gif版本)
* @param response
*/
@RequestMapping(value="getGifCode",method=RequestMethod.GET)
public void getGifCode(HttpServletResponse response,HttpServletRequest request){
try {
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/gif");
/**
* gif格式动画验证码
* 宽,高,位数。
*/
Captcha captcha = new GifCaptcha(146,33,4);
//输出
captcha.out(response.getOutputStream());
HttpSession session = request.getSession(true);
//存入Session
session.setAttribute("_code",captcha.text().toLowerCase());
} catch (Exception e) {
LoggerUtils.fmtError(getClass(),e, "获取验证码异常:%s",e.getMessage());
}
}
使用挺简单的,但是用了其他人封装的工具类。下面会提供下载链接的。
这些个工具类,还提供了这个气泡版本的jpg格式验证码方式。
代码如下:
/**
* 获取验证码(jpg版本)
* @param response
*/
@RequestMapping(value="getJPGCode",method=RequestMethod.GET)
public void getJPGCode(HttpServletResponse response,HttpServletRequest request){
try {
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpg");
/**
* jgp格式验证码
* 宽,高,位数。
*/
Captcha captcha = new SpecCaptcha(146,33,4);
//输出
captcha.out(response.getOutputStream());
HttpSession session = request.getSession(true);
//存入Session
session.setAttribute("_code",captcha.text().toLowerCase());
} catch (Exception e) {
LoggerUtils.fmtError(getClass(),e, "获取验证码异常:%s",e.getMessage());
}
}
有兴趣的朋友可以下载源码看看。
vcode-sojson.com(jb51.net).rar
总结
标签:java,动态,验证码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android编程基于自定义View实现绚丽的圆形进度条功能示例
2023-07-24 17:13:26
![](https://img.aspxhome.com/file/2023/7/84737_0s.png)
Java数组(Array)最全汇总(中篇)
2022-09-05 06:01:02
![](https://img.aspxhome.com/file/2023/8/63638_0s.jpg)
Java把数字格式化为货币字符串实例代码
2022-10-13 23:17:27
![](https://img.aspxhome.com/file/2023/8/92688_0s.png)
dubbo将异常转换成RuntimeException的原因分析 ExceptionFilter
2023-11-24 14:25:13
![](https://img.aspxhome.com/file/2023/3/64143_0s.jpg)
Java mybatis 开发自定义插件
2022-11-26 03:29:24
![](https://img.aspxhome.com/file/2023/9/75489_0s.png)
Java中构造函数,set/get方法和toString方法使用及注意说明
2021-07-15 13:01:39
![](https://img.aspxhome.com/file/2023/2/116192_0s.png)
基于C# winform实现图片上传功能的方法
2022-09-12 18:18:14
25个最好的免费Eclipse插件
2021-09-21 10:56:24
![](https://img.aspxhome.com/file/2023/2/67642_0s.jpg)
使用Thumbnails实现图片指定大小压缩
2021-11-14 11:07:34
![](https://img.aspxhome.com/file/2023/7/138267_0s.jpg)
MaterialApp Flutter 应用全局配置与主题管理详解
2023-05-03 18:50:48
Java C++ 算法题解leetcode652寻找重复子树
2022-08-17 23:58:09
![](https://img.aspxhome.com/file/2023/0/61120_0s.jpg)
RocketMQ NameServer架构设计启动流程
2023-12-13 07:40:01
![](https://img.aspxhome.com/file/2023/8/80938_0s.png)
SpringCloud让微服务实现指定程序调用
2022-03-05 23:24:15
![](https://img.aspxhome.com/file/2023/2/88552_0s.png)
C#简单实现显示中文格式星期几的方法
2021-09-08 12:27:05
java WebSocket客户端断线重连的实现方法
2023-05-26 22:45:18
struts2拦截器_动力节点Java学院整理
2023-06-11 10:11:36
![](https://img.aspxhome.com/file/2023/2/78242_0s.png)
Maven安装与配置及Idea配置Maven的全过程
2023-05-20 01:37:59
![](https://img.aspxhome.com/file/2023/6/88216_0s.jpg)
c# 通过WinAPI播放PCM声音
2021-10-22 12:35:18
Spring AOP AspectJ使用及配置过程解析
2023-09-29 00:17:45
Java 延迟队列的常用的实现方式
2022-06-30 13:57:00
![](https://img.aspxhome.com/file/2023/1/118901_0s.png)