欢迎光临机房365,我们竭诚为您提供功能全方位提供机房解决方案!

关于我们 品牌优势 服务支持 联系我们

精密空调|机房精密空调|机房专用空调-机房365

旗下栏目: 服务器资讯 服务器产品 服务器售后

服务器开发系列--图形验证码到底是怎么回事?

发布时间:2018-03-19 13:28:11   点击:
1 什么是验证码?验证码是一种区别用户是计算机仍是人的公共全自动程序。短时间是无法退出人类舞台的,现在仅仅尽量提升用户体验。效果账号

1.什么是验证码?

验证码是一种区别用户是计算机仍是人的公共全自动程序。短时间是无法退出人类舞台的,现在仅仅尽量提升用户体验。

效果

  • 账号安全
  • 反作弊
  • 反爬虫
  • 防论坛灌水
  • 防歹意注册

分类

  • 图形验证码
  • Gif动画验证码
  • 手机短信验证码
  • 手机语音验证码
  • 视频验证码
  • web2.0验证码

2.kaptcha验证码组件

kaptcha 是一个非常有用的验证码生成东西;有了它,你能够生成各种款式的验证码,由于它是可装备的。

常见装备

  • 验证码的字体
  • 验证码字体的巨细
  • 验证码字体的字体色彩
  • 验证码内容的规模(数字,字母,中文汉字)
  • 验证码图片的巨细,边框,边框粗细,边框色彩
  • 验证码的搅扰线(能够自己承继com.google.code.kaptcha.NoiseProducer写一个自定义的搅扰线)
  • 验证码的款式(鱼眼款式、3D、一般含糊……当然也能够承继com.google.code.kaptcha.GimpyEngine自定义款式)

引进maven装备

  1.  
  2.     com.github.axet 
  3.     kaptcha 
  4.     0.0.9 
  5.  

kaptcha.properties

  1. kaptcha.textproducer.font.color=red  
  2. kaptcha.image.width=130  
  3. kaptcha.image.height=44  
  4. kaptcha.textproducer.font.size=35  
  5. kaptcha.textproducer.char.length=4  
  6. kaptcha.textproducer.font.names=\\u5B8B\\u4F53,\\u6977\\u4F53,\\u5FAE\\u8F6F\\u96C5\\u9ED1  
  7. kaptcha.noise.color=gray 
  8. kaptcha.obscurificator.impl=com.google.code.kaptcha.impl.WaterRipple 

用filter过滤器来生成验证码,具体步骤如下:

1、web.xml

  1.  
  2.     KaptchaFilter 
  3.     com.xxoo.admin.ui.filter.KaptchaFilter 
  4.  
  5.  
  6.     KaptchaFilter 
  7.     /kaptcha.jpg 
  8.  

阐明:验证码过滤器需求放到Shiro之后,由于Shiro将包装HttpSession.如果不,可能形成两次的sesisonid不一样。

2、图片验证码类

  1. public class CaptchaService { 
  2.     private static ImageCaptchaService instance = null;  
  3.     static { 
  4.         instance = new KaptchaImageCaptchaService(); 
  5.     }  
  6.     public synchronized static ImageCaptchaService getInstance() { 
  7.         return instance; 
  8.     } 
  9.     public synchronized static boolean validate(HttpServletRequest httpServletRequest, String input) throws Exception { 
  10.         String text = instance.getText(httpServletRequest); 
  11.         boolean result = text.equalsIgnoreCase(input); 
  12.         instance.removeKaptcha(httpServletRequest); 
  13.         return result; 
  14.     } 

3、依据Kaptcha的验证码图片实现

  1. public class KaptchaImageCaptchaService implements ImageCaptchaService { 
  2.  
  3.     private Logger logger = LoggerFactory.getLogger(getClass()); 
  4.  
  5.     public KaptchaImageCaptchaService() { 
  6.     } 
  7.  
  8.     public static Config getConfig() throws IOException { 
  9.         Properties p = new Properties(); 
  10.         p.load(new DefaultResourceLoader().getResource("kaptcha.properties").getInputStream()); 
  11.         Config config = new Config(p); 
  12.         return config; 
  13.     } 
  14.  
  15.     @Override 
  16.     public void create(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { 
  17.         httpServletResponse.setDateHeader("Expires", 0L); 
  18.         httpServletResponse.setHeader("Cache-Control", "no-store, no-cache, must-revalidate"); 
  19.         httpServletResponse.addHeader("Cache-Control", "post-check=0, pre-check=0"); 
  20.         httpServletResponse.setHeader("Pragma", "no-cache"); 
  21.         httpServletResponse.setContentType("image/jpeg"); 
  22.         Config config = getConfig(); 
  23.         Producer producer = config.getProducerImpl(); 
  24.         String capText = producer.createText(); 
  25.         if(logger.isDebugEnabled()){ 
  26.             logger.info("create captcha:" + capText + ":" + config.getSessionKey() ); 
  27.         } 
  28.         httpServletRequest.getSession().setAttribute(config.getSessionKey(), capText); 
  29.         httpServletRequest.getSession().setAttribute(config.getSessionDate(), new Date()); 
  30.         BufferedImage bi = producer.createImage(capText); 
  31.         ServletOutputStream out = httpServletResponse.getOutputStream(); 
  32.         ImageIO.write(bi, "jpg", out); 
  33.         out.flush(); 
  34.         out.close(); 
  35.     } 
  36.  
  37.     @Override 
  38.     public String getText(HttpServletRequest httpServletRequest) throws Exception { 
  39.         return (String)httpServletRequest.getSession().getAttribute(getConfig().getSessionKey()); 
  40.     } 
  41.  
  42.     @Override 
  43.     public void removeKaptcha(HttpServletRequest httpServletRequest) throws Exception { 
  44.         httpServletRequest.getSession().removeAttribute(getConfig().getSessionKey()); 
  45.         httpServletRequest.getSession().removeAttribute(getConfig().getSessionDate()); 
  46.     } 
  47.  

4、验证码东西类

  1. public class CaptchaService { 
  2.  
  3.     private static ImageCaptchaService instance = null; 
  4.  
  5.     static { 
  6.         instance = new KaptchaImageCaptchaService(); 
  7.     } 
  8.  
  9.     public synchronized static ImageCaptchaService getInstance() { 
  10.         return instance; 
  11.     } 
  12.  
  13.     public synchronized static boolean validate(HttpServletRequest httpServletRequest, String input) throws Exception { 
  14.         String text = instance.getText(httpServletRequest); 
  15.         boolean result = text.equalsIgnoreCase(input); 
  16.         instance.removeKaptcha(httpServletRequest); 
  17.         return result; 
  18.     } 

5、生成验证码过滤器

  1. public class KaptchaFilter extends OncePerRequestFilter { 
  2.  
  3.     private Logger logger = LoggerFactory.getLogger(getClass()); 
  4.  
  5.     @Override 
  6.     protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException { 
  7.         try { 
  8.             CaptchaService.getInstance().create(httpServletRequest,httpServletResponse); 
  9.         } catch (Exception e) { 
  10.             logger.info("create captcha error.",e); 
  11.         } 
  12.     } 
  13.  

6、验证码校验

  1. private boolean doCaptchaValidate(HttpServletRequest request, String code) { 
  2.         //比对 
  3.         try { 
  4.             if (code == null || !CaptchaService.validate(request, code)) { 
  5.                 return false; 
  6.             } else { 
  7.                 return true; 
  8.             } 
  9.         } catch (Exception e) { 
  10.             logger.warn("captcha check error!"); 
  11.             return false; 
  12.         } 

总结

本文首要讲述了kaptcha图形化验证码的运用和介绍,小伙伴能够依据自己的需求进行引进。

注:文章内容和图片均来源于网络,只起到信息的传递,不是用于商业,如有侵权请联系删除!

最火资讯