国产高清在线精品免费,国产九九精品,亚洲女同性恋黄色视频,日本在线中文

歡迎光臨機(jī)房365,我們竭誠(chéng)為您提供功能全方位提供機(jī)房解決方案!

關(guān)于我們 品牌優(yōu)勢(shì) 服務(wù)支持 聯(lián)系我們

精密空調(diào)|機(jī)房精密空調(diào)|機(jī)房專用空調(diào)-機(jī)房365

旗下欄目: 服務(wù)器資訊 服務(wù)器產(chǎn)品 服務(wù)器售后

服務(wù)器開發(fā)系列--圖形驗(yàn)證碼到底是怎么回事?

發(fā)布時(shí)間:2018-03-19 13:28:11   點(diǎn)擊:
1 什么是驗(yàn)證碼?驗(yàn)證碼是一種區(qū)別用戶是計(jì)算機(jī)仍是人的公共全自動(dòng)程序。短時(shí)間是無(wú)法退出人類舞臺(tái)的,現(xiàn)在僅僅盡量提升用戶體驗(yàn)。效果賬號(hào)

1.什么是驗(yàn)證碼?

驗(yàn)證碼是一種區(qū)別用戶是計(jì)算機(jī)仍是人的公共全自動(dòng)程序。短時(shí)間是無(wú)法退出人類舞臺(tái)的,現(xiàn)在僅僅盡量提升用戶體驗(yàn)。

效果

  • 賬號(hào)安全
  • 反作弊
  • 反爬蟲
  • 防論壇灌水
  • 防歹意注冊(cè)

分類

  • 圖形驗(yàn)證碼
  • Gif動(dòng)畫驗(yàn)證碼
  • 手機(jī)短信驗(yàn)證碼
  • 手機(jī)語(yǔ)音驗(yàn)證碼
  • 視頻驗(yàn)證碼
  • web2.0驗(yàn)證碼

2.kaptcha驗(yàn)證碼組件

kaptcha 是一個(gè)非常有用的驗(yàn)證碼生成東西;有了它,你能夠生成各種款式的驗(yàn)證碼,由于它是可裝備的。

常見裝備

  • 驗(yàn)證碼的字體
  • 驗(yàn)證碼字體的巨細(xì)
  • 驗(yàn)證碼字體的字體色彩
  • 驗(yàn)證碼內(nèi)容的規(guī)模(數(shù)字,字母,中文漢字)
  • 驗(yàn)證碼圖片的巨細(xì),邊框,邊框粗細(xì),邊框色彩
  • 驗(yàn)證碼的攪擾線(能夠自己承繼com.google.code.kaptcha.NoiseProducer寫一個(gè)自定義的攪擾線)
  • 驗(yàn)證碼的款式(魚眼款式、3D、一般含糊……當(dāng)然也能夠承繼com.google.code.kaptcha.GimpyEngine自定義款式)

引進(jìn)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過(guò)濾器來(lái)生成驗(yàn)證碼,具體步驟如下:

1、web.xml

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

闡明:驗(yàn)證碼過(guò)濾器需求放到Shiro之后,由于Shiro將包裝HttpSession.如果不,可能形成兩次的sesisonid不一樣。

2、圖片驗(yàn)證碼類

  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、依據(jù)Kaptcha的驗(yàn)證碼圖片實(shí)現(xiàn)

  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、驗(yàn)證碼東西類

  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、生成驗(yàn)證碼過(guò)濾器

  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、驗(yàn)證碼校驗(yàn)

  1. private boolean doCaptchaValidate(HttpServletRequest request, String code) { 
  2.         //比對(duì) 
  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.         } 

總結(jié)

本文首要講述了kaptcha圖形化驗(yàn)證碼的運(yùn)用和介紹,小伙伴能夠依據(jù)自己的需求進(jìn)行引進(jìn)。

注:文章內(nèi)容和圖片均來(lái)源于網(wǎng)絡(luò),只起到信息的傳遞,不是用于商業(yè),如有侵權(quán)請(qǐng)聯(lián)系刪除!

最火資訊

陈昊宇陈丽君四公帮唱| 封神2将从影院下映| 姚安娜带华为手机参加活动| 梁洁造型师| 林孝埈每天主打一个快乐| 朴成训 来财 | 白敬亭 宋轶| BLACKPINK未公开的物料| 司马南偷税到底该谁担责| 甲亢哥直播打破外网的单向认知 | 少吃水果少生很多病不具有普遍性 | 封神2将从影院下映山姆客服称水果中吃出虫是正常情况 | 白敬亭 宋轶| 男子肝癌晚期只打一针获新生| 甲亢哥成都行直播| 挂面是面食界的苹果| 内蒙古警察枪击案重审宣判故意杀人罪改判故意伤害罪刑期四年 | 中国咖啡98%来自云南| 乘风2025最新排名| 零公摊时代真的要来了吗| 直播卖所谓降糖食品误导消费者停药| 白敬亭 宋轶| 中国石化西北油田分公司原副总经理秦强运被查 | 店主接到近8吨毛肚订单直接报警 一直对月薪三万没概念直到换算成天 | 王艳发了赤脚鬼| 时代少年团 录播| 愚人节文案| 男子肝癌晚期只打一针获新生 | 李昀锐好标准的体育生下楼梯| 姚安娜带华为手机参加活动| 横店变竖店了| 甲亢哥和宽窄巷子网红一起玩抽象| 乘风2025最新排名| 檀健次当评委了| 淘宝推出首个AI机器人带货主播 | 清明档预售前三名| 男子用非遗茶杯吹奏广西民歌| 钟南山提醒剩饭剩菜别强吃| 乘风2025最新排名| 找工作不要限制于招聘app| 缅甸地震已致2056人死亡|