kaptcha 验证码在spring mvc 中的使用

kaptcha 是一个非常实用的验证码生成工具。有了它,你可以生成各种样式的验证码,因为它是可配置的。kaptcha工作的原理是调用 com.google.code.kaptcha.servlet.KaptchaServlet,生成一个图片。同时将生成的验证码字符串放到 HttpSession中。

使用kaptcha可以方便的配置:

 

  • 验证码的字体
  • 验证码字体的大小
  • 验证码字体的字体颜色
  • 验证码内容的范围(数字,字母,中文汉字!)
  • 验证码图片的大小,边框,边框粗细,边框颜色
  • 验证码的干扰线(可以自己继承com.google.code.kaptcha.NoiseProducer写一个自定义的干扰线)
  • 验证码的样式(鱼眼样式、3D、普通模糊……当然也可以继承com.google.code.kaptcha.GimpyEngine自定义样式)

……

详细信息请看下面的web.xml文件

下面介绍一下用法:

1.首先去官网下载jar:http://code.google.com/p/kaptcha/

2.建立一个web项目,导入kaptcha-2.3.jar到环境变量中。

3.配置web.xml文件

Xml代码   收藏代码
  1. <!--Kaptcha 验证码  --><!--  
  2.     < servlet >   
  3.         < servlet-name > kaptcha</ servlet-name >   
  4.         < servlet-class > com.google.code.kaptcha.servlet.KaptchaServlet</ servlet-class >   
  5.         < init-param >   
  6.             < param-name > kaptcha.border</ param-name >   
  7.             < param-value > no</ param-value >   
  8.         </ init-param >   
  9.         < init-param >   
  10.             < param-name > kaptcha.border.color</ param-name >   
  11.             < param-value > 105,179,90</ param-value >   
  12.         </ init-param >        
  13.         < init-param >   
  14.             < param-name > kaptcha.textproducer.font.color</ param-name >   
  15.             < param-value > red</ param-value >   
  16.         </ init-param >   
  17.         < init-param >   
  18.             < param-name > kaptcha.image.width</ param-name >   
  19.             < param-value > 250</ param-value >   
  20.         </ init-param >   
  21.         < init-param >   
  22.             < param-name > kaptcha.image.height</ param-name >   
  23.             < param-value > 90</ param-value >   
  24.         </ init-param >   
  25.         < init-param >   
  26.             < param-name > kaptcha.textproducer.font.size</ param-name >   
  27.             < param-value > 70</ param-value >   
  28.         </ init-param >   
  29.         < init-param >   
  30.             < param-name > kaptcha.session.key</ param-name >   
  31.             < param-value > code</ param-value >   
  32.         </ init-param >   
  33.         < init-param >   
  34.             < param-name > kaptcha.textproducer.char.length</ param-name >   
  35.             < param-value > 4</ param-value >   
  36.         </ init-param >   
  37.         < init-param >   
  38.             < param-name > kaptcha.textproducer.font.names</ param-name >   
  39.             < param-value > 宋体,楷体,微软雅黑</ param-value >   
  40.         </ init-param >        
  41.     </ servlet >   
Xml代码
Xml代码   收藏代码
  1.     < servlet-mapping >   
  2. < servlet-name > kaptcha</ servlet-name >   
  3. < url-pattern > /ClinicCountManager/kaptcha.jpg</ url-pattern >   
  4. lt;/servlet-mapping>   

 

jsp 页面使用

Java代码   收藏代码
  1. <table>  
  2.         <tr>  
  3.             <td><img src="/ClinicCountManager/kaptcha.jpg"></td>  
  4.             <td valign="top">  
  5.           
  6.                 <form method="POST">  
  7.                     <br>sec code:<input type="text" name="kaptchafield"><br />  
  8.                     <input type="submit" name="submit">  
  9.                 </form>  
  10.             </td>  
  11.         </tr>  
  12.     </table>    
  13.   
  14.     <br /><br /><br /><br />  
  15.       
  16.     <%  
  17.         String c = (String)session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);  
  18.         String parm = (String) request.getParameter("kaptchafield");  
  19.           
  20.         out.println("Parameter: " + parm + " ? Session Key: " + c + " : ");  
  21.           
  22.         if  (c != null  && parm != null ) {  
  23.             if  (c.equals(parm)) {  
  24.                 out.println("<b>true</b>");  
  25.             } else  {  
  26.                 out.println("<b>false</b>");  
  27.             }  
  28.           
  29.     %>  

 

上面的配置在普通jsp环境下面是有效的,如果在spring mvc环境下,则取不到session值,对于sping mvc环境验证码配置如下:

1.不用在web.xml进行相关配置,在applicationContext.xml中配置

 

Xml代码 
Xml代码   收藏代码
  1. < bean  id ="captchaProducer"  class ="com.google.code.kaptcha.impl.DefaultKaptcha" >   
  2.         < property  name ="config" >   
  3.             < bean  class ="com.google.code.kaptcha.util.Config" >   
  4.                 < constructor-arg >   
  5.                     < props >   
  6.                         < prop  key ="kaptcha.border" > no</ prop >   
  7.                         < prop  key ="kaptcha.border.color" > 105,179,90</ prop >   
  8.                         < prop  key ="kaptcha.textproducer.font.color" > red</ prop >   
  9.                         < prop  key ="kaptcha.image.width" > 250</ prop >   
  10.                         < prop  key ="kaptcha.textproducer.font.size" > 90</ prop >   
  11.                         < prop  key ="kaptcha.image.height" > 90</ prop >   
  12.                         < prop  key ="kaptcha.session.key" > code</ prop >   
  13.                         < prop  key ="kaptcha.textproducer.char.length" > 4</ prop >   
  14.                         < prop  key ="kaptcha.textproducer.font.names" > 宋体,楷体,微软雅黑</ prop >   
  15.                     </ props >   
  16.                 </ constructor-arg >   
  17.             </ bean >   
  18.         </ property >   
  19.     </ bean >   

 

新建生成图片控制类

Java代码   收藏代码
  1. import  java.awt.image.BufferedImage;  
  2. import  javax.imageio.ImageIO;  
  3. import  javax.servlet.ServletOutputStream;  
  4. import  javax.servlet.http.HttpServletRequest;  
  5. import  javax.servlet.http.HttpServletResponse;  
  6. import  org.springframework.beans.factory.annotation.Autowired;  
  7. import  org.springframework.stereotype.Controller;  
  8. import  org.springframework.web.bind.annotation.RequestMapping;  
  9. import  org.springframework.web.servlet.ModelAndView;  
  10. import  com.google.code.kaptcha.Constants;  
  11. import  com.google.code.kaptcha.Producer;  
  12.   
  13. @Controller   
  14. @RequestMapping ("/")  
  15. public  class  CaptchaImageCreateController {  
  16.       
  17.     private  Producer captchaProducer = null ;  
  18.   
  19.     @Autowired   
  20.     public  void  setCaptchaProducer(Producer captchaProducer) {  
  21.         this .captchaProducer = captchaProducer;  
  22.     }  
  23.   
  24.     @RequestMapping ("/captcha-image")  
  25.     public  ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws  Exception {  
  26.   
  27.         response.setDateHeader("Expires", 0 );  
  28.         // Set standard HTTP/1.1 no-cache headers.  
  29.         response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");  
  30.         // Set IE extended HTTP/1.1 no-cache headers (use addHeader).  
  31.         response.addHeader("Cache-Control", "post-check=0, pre-check=0");  
  32.         // Set standard HTTP/1.0 no-cache header.  
  33.         response.setHeader("Pragma", "no-cache");  
  34.         // return a jpeg  
  35.         response.setContentType("image/jpeg");  
  36.         // create the text for the image  
  37.         String capText = captchaProducer.createText();  
  38.         // store the text in the session  
  39.         request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);  
  40.         // create the image with the text  
  41.         BufferedImage bi = captchaProducer.createImage(capText);  
  42.         ServletOutputStream out = response.getOutputStream();  
  43.         // write the data out  
  44.         ImageIO.write(bi, "jpg", out);  
  45.         try  {  
  46.             out.flush();  
  47.         } finally  {  
  48.             out.close();  
  49.         }  
  50.         return  null ;  
  51.     }  
  52.   
  53. }  

 前台调用方式

 

Java代码
Java代码   收藏代码
  1. <div class ="chknumber">  
  2.        <label>验证码:          
  3.        <input name="kaptcha" type="text" id="kaptcha" maxlength="4" class ="chknumber_input" />               
  4.        </label>  
  5.         <img src="/ClinicCountManager/captcha-image.do" width="55" height="20" id="kaptchaImage"  style="margin-bottom: -3px"/>   
  6.        <script type="text/javascript">      
  7.         $(function(){           
  8.             $('#kaptchaImage').click(function () {//生成验证码  
  9.              $(this ).hide().attr('src', '/ClinicCountManager/captcha-image.do?' + Math.floor(Math.random()*100 ) ).fadeIn(); })      
  10.                   });   
  11.         
  12.        </script>   
  13.      </div>  

 取验证码的方式

 

Java代码
Java代码   收藏代码
  1. String code = (String)session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);  
  2.      

你可能感兴趣的:(spring,mvc)