`

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整合kaptcha验证码的实现

    本文将介绍spring整合kaptcha验证码的实现,主要通过讲解kaptcha的简介、开发工具及使用的核心技术、kaptcha两种使用方式、搭骨架、完善配置文件等几个方面,对spring整合kaptcha验证码的实现进行了详细的介绍。...

    谷歌kaptcha验证码jar包

    在实际应用中,Kaptcha还可以与其他框架(如Spring MVC)集成,简化验证码的生成和验证流程。例如,可以创建一个过滤器(Filter)来处理请求,自动在每次需要验证码的页面上生成新的验证码,并在用户提交表单时验证...

    Kaptcha验证码类库及配置

    2. **配置(Configuration)**:在使用Kaptcha时,我们需要在Spring或其他配置文件中设置相关属性,比如验证码长度、字体样式、文本颜色、背景颜色、边框、噪声线等。这可以通过XML配置或Java代码完成。 3. **存储...

    Spring MVC中使用Google kaptcha验证码的方法详解

    在Spring MVC中集成Google kaptcha验证码能够有效地防止恶意登录和批量操作。kaptcha是一个高度可配置的验证码生成库,能够创建各种样式的验证码。以下是如何在Spring MVC项目中使用kaptcha的详细步骤: 1. **Maven...

    spring mvc 使用kaptcha配置生成验证码实例

    总结来说,使用Kaptcha在Spring MVC中生成验证码主要包括以下几个步骤: 1. 添加Kaptcha的Maven依赖。 2. 在`web.xml`中配置Kaptcha bean,自定义验证码的样式。 3. 创建一个Controller,处理验证码生成和验证的请求...

    kaptcha验证码组件使用简介解析

    kaptcha验证码组件使用简介解析 Kaptcha验证码组件是基于SimpleCaptcha的开源项目,提供了强大的验证码生成和验证功能。该组件的使用非常方便,只需添加jar包依赖和简单的配置就可以使用了。 一、添加jar包依赖 ...

    谷歌验证码使用工具——kaptcha-2.3.2

    7. **集成到Web应用**:在Spring MVC、Struts2等常见Web框架中,可以方便地将kaptcha集成到登录或其他需要验证码的表单中,以增强安全性。 8. **安全考虑**:kaptcha的目的是抵御自动化工具,例如自动注册、恶意...

    kaptcha-2.3.jar

    6. **与Spring框架的集成**:在Spring MVC项目中,`kaptcha`可以方便地与Spring的依赖注入机制结合,使验证码服务的实例化和配置更加灵活。 7. **安全性**:`kaptcha`的验证码具有一定的抗机器学习能力,随着机器...

    登陆验证码kaptcha结合spring boot的用法详解

    这个配置类使用Java配置,类似于Spring MVC中的XML配置。下面是`CaptchaConfig`的示例代码: ```java @Configuration public class CaptchaConfig { @Bean(name="captchaProducer") public DefaultKaptcha ...

    springmvc+shiro+kaptcha+excel+hibernate+mail

    4. **Excel**:在Java开发中,处理Excel文件通常需要使用Apache POI库。Apache POI允许开发者读取、写入和修改Microsoft Office格式的文件,包括Excel工作簿。这在数据分析、报表生成或数据导入导出场景中非常有用。...

    kaptcha-2.3.2.jar

    3. **易于集成**:kaptcha库设计简洁,易于与其他Java Web应用集成,如Spring MVC、Struts等框架,只需添加对应的依赖并配置相关代码即可。 4. **响应式设计**:随着移动设备的普及,kaptcha也支持生成适应不同屏幕...

    kaptcha-2.3.2.jar工具类

    6. **兼容性**:kaptcha-2.3.2版本兼容Java Servlet API,因此可以无缝集成到基于Servlet的Web应用中,如Spring MVC或Struts等框架。 7. **国际化支持**:kaptcha允许设置验证码的文字语言,满足不同地区的使用需求...

    kaptcha-2.3.jar包

    在Spring MVC应用中,可以将`KaptchaProducer`实例配置为bean,然后在控制器中注入并使用。这种方式使得验证码的生成和验证过程变得更加简洁和模块化。 除了基本的文本验证码,kaptcha还支持生成基于数学问题的...

    java实现多种验证码

    要使用kaptcha,首先需要在项目中引入依赖。如果你使用的是Maven,可以在pom.xml文件中添加以下依赖: ```xml &lt;groupId&gt;com.google.code.kaptcha&lt;/groupId&gt; &lt;artifactId&gt;kaptcha &lt;version&gt;2.3.2 ``` 接下来,...

    Kaptcha.zip

    10. **最佳实践**:在实际项目中,除了使用Kaptcha生成验证码,还应考虑定期更换验证码样式,防止被破解。同时,结合其他安全措施,如限流、IP黑名单等,增强网站的安全性。 总的来说,Kaptcha是Java Web开发中一个...

    springMVC实现图形验证码(kaptcha)代码实例

    要在 SpringMVC 项目中使用 kaptcha,需要首先在 pom.xml 文件中添加 kaptcha 的依赖项: ```xml &lt;groupId&gt;com.google.code&lt;/groupId&gt; &lt;artifactId&gt;kaptcha &lt;version&gt;2.3.2 ``` 然后,需要在 spring-mvc.xml ...

    kaptcha-2.3.2

    《kaptcha-2.3.2:高效验证码生成器详解》 在网络安全领域,验证码(CAPTCHA)扮演着至关重要的角色,它能...在实际项目中,结合Spring框架或其他MVC框架,可以轻松地将kaptcha集成到应用中,提升网站的整体安全性能。

    如何构建快速的mvc开发环境

    1. **Spring Core 4.0.9 RELEASE** 和 **Spring MVC 4.0.9 RELEASE**:Spring框架提供了依赖注入和面向切面编程,Spring MVC则是Spring框架中的Web MVC模块,用于构建Web应用。 2. **Dubbo 2.5.3**:阿里巴巴出品的...

    【20210611】Springboot开发系统支持与配置文件.pdf

    图形验证码的生成则可以通过`kaptcha`库来实现,这在用户登录验证等场景中非常常见。 此外,文件中还提到了其他一些依赖,可能包括但不限于日志记录、安全控制、邮件发送等功能的组件。每个依赖都有其特定的版本号...

    项目架构设计1

    在数据持久化层,我们使用了Hibernate、MyBatis和Spring Data JPA三种不同的ORM框架。Hibernate提供了对象关系映射,简化了数据库操作;MyBatis则允许更细粒度的SQL控制;Spring Data JPA则是基于JPA的,提供了一种...

Global site tag (gtag.js) - Google Analytics