相对来说呢,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());
}
}