Java生成验证码合集(二)GIF版,Java生成动图验证码

相对来说呢,jpg格式的相对来说容易破解一点,当然也取决于你的干扰元素,元素越复杂,破解也就难度越高,有的加的多,人都识别不出来了,何况人呢。都是概率问题。

GIF格式 + 干扰元素,那么就 验证码  破解难度又上了一个层次。

上代码吧。

 
  1. /**
  2. * 获取验证码(Gif版本)
  3. * @param response
  4. */
  5. @RequestMapping(value="getGifCode",method=RequestMethod.GET)
  6. public void getGifCode(HttpServletResponse response,HttpServletRequest request){
  7. try {
  8. response.setHeader("Pragma", "No-cache");
  9. response.setHeader("Cache-Control", "no-cache");
  10. response.setDateHeader("Expires", 0);
  11. response.setContentType("image/gif");
  12. /**
  13. * gif格式动画验证码
  14. * 宽,高,位数。
  15. */
  16. Captcha captcha = new GifCaptcha(146,33,4);
  17. //输出
  18. captcha.out(response.getOutputStream());
  19. HttpSession session = request.getSession(true);
  20. //存入Session
  21. session.setAttribute("_code",captcha.text().toLowerCase());
  22. } catch (Exception e) {
  23. LoggerUtils.fmtError(getClass(),e, "获取验证码异常:%s",e.getMessage());
  24. }
  25. }

使用挺简单的,但是用了其他人封装的工具类。下面会提供下载链接的。

这些个工具类,还提供了这个气泡版本的jpg格式 验证码  方式。

代码如下:

 
  1. /**
  2. * 获取验证码(jpg版本)
  3. * @param response
  4. */
  5. @RequestMapping(value="getJPGCode",method=RequestMethod.GET)
  6. public void getJPGCode(HttpServletResponse response,HttpServletRequest request){
  7. try {
  8. response.setHeader("Pragma", "No-cache");
  9. response.setHeader("Cache-Control", "no-cache");
  10. response.setDateHeader("Expires", 0);
  11. response.setContentType("image/jpg");
  12. /**
  13. * jgp格式验证码
  14. * 宽,高,位数。
  15. */
  16. Captcha captcha = new SpecCaptcha(146,33,4);
  17. //输出
  18. captcha.out(response.getOutputStream());
  19. HttpSession session = request.getSession(true);
  20. //存入Session
  21. session.setAttribute("_code",captcha.text().toLowerCase());
  22. } catch (Exception e) {
  23. LoggerUtils.fmtError(getClass(),e, "获取验证码异常:%s",e.getMessage());
  24. }
  25. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值