SPRING MVC Kaptcha 实现验证码功能

Kaptcha是一个基于SimpleCaptcha的验证码开源项目。

maven配置如下

		<dependency>
			<groupId>com.github.axet</groupId>
			<artifactId>kaptcha</artifactId>
			<version>0.0.8</version>
		</dependency>

 

 

在springMVC环境下,使用kaptcha

    web.xml配置代码:

 

[html]  view plain copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.   <display-name></display-name>   
  8.     
  9.   <servlet>  
  10.     <servlet-name>hello</servlet-name>  
  11.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  12.     <load-on-startup>1</load-on-startup>  
  13.   </servlet>  
  14.     
  15.   <servlet-mapping>  
  16.     <servlet-name>hello</servlet-name>  
  17.     <url-pattern>/</url-pattern>  
  18.   </servlet-mapping>  
  19.     
  20.   <servlet>  
  21.     <servlet-name>Kaptcha</servlet-name>  
  22.     <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>  
  23.     <init-param>  
  24.         <param-name>kaptcha.border</param-name>    <!-- 是否有边框 -->  
  25.         <param-value>no</param-value>  
  26.     </init-param>  
  27.     <init-param>  
  28.         <param-name>kaptcha.textproducer.char.space</param-name>   <!--字符之间的间距 -->  
  29.         <param-value>8</param-value>  
  30.     </init-param>  
  31.     <init-param>  
  32.         <param-name>kaptcha.textproducer.char.length</param-name>   <!-- 字符的个数 -->  
  33.         <param-value>4</param-value>  
  34.     </init-param>    
  35.   </servlet>  
  36.     
  37.   <servlet-mapping>  
  38.     <servlet-name>Kaptcha</servlet-name>  
  39.     <url-pattern>/Kaptcha.jpg</url-pattern>  
  40.   </servlet-mapping>    
  41.     
  42.   <welcome-file-list>  
  43.     <welcome-file>index.jsp</welcome-file>  
  44.   </welcome-file-list>  
  45. </web-app>  


     hello-servlet.xml配置代码:

 

 

[html]  view plain copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xsi:schemaLocation="  
  7.         http://www.springframework.org/schema/beans   
  8.         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  9.         http://www.springframework.org/schema/mvc   
  10.         http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd  
  11.         http://www.springframework.org/schema/context   
  12.         http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  13.       
  14.     <context:component-scan base-package="com.lqh.controller" />  
  15.         <mvc:annotation-driven />  
  16.       
  17.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  18.         <property name="prefix" value="/WEB-INF/jsp/" />     <!-- 视图前缀 -->  
  19.         <property name="suffix" value=".jsp" />              <!-- 视图后缀 -->  
  20.     </bean>  
  21. </beans>  

    controller代码:

 

[java]  view plain copy
 
  1. @Controller  
  2. public class HelloController {  
  3.       
  4.     @RequestMapping(value={"/""/hello"})  
  5.     public String hello() {  
  6.         System.out.println("hello");  
  7.         return "hello";  
  8.     }  
  9.       
  10.     @RequestMapping(value="/hello", method=RequestMethod.POST)  
  11.     public String hello(String verifyCode, HttpServletRequest request) {  
  12.           
  13.         String code = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);        //获取生成的验证码  
  14.         System.out.println(verifyCode + "," + code);  
  15.         if(verifyCode.equals(code)) {  
  16.             System.out.println("验证通过 ");  
  17.         }  
  18.         return "redirect:/hello";                                                       //post方式提交,实现客户端跳转  
  19.     }  
  20. }  

       前台页面hello.jsp代码:

 

[html]  view plain copy
 
  1. <html>  
  2.   <head>  
  3.     <base href="<%=basePath%>">      
  4.     <title>My JSP 'index.jsp' starting page</title>   
  5.   </head>  
  6.     
  7.   <body>  
  8.     hello.  
  9.     <form method="post">  
  10.         <input type="text" name="verifyCode" />   
  11.         <img alt="验证码" src="Kaptcha.jpg">        <!-- KaptchaServlet会过滤该请求,返回验证码图片--!>  
  12.           
  13.         <input type="submit" value="提交" />  
  14.     </form>  
  15.   </body>  
  16. </html>  

附录:kaptcha的可配置项

kaptcha.border 是否有边框 默认为true 我们可以自己设置yes,no

kaptcha.border.color 边框颜色 默认为Color.BLACKkaptcha.border.thickness 边框粗细度 默认为1

kaptcha.producer.impl 验证码生成器 默认为DefaultKaptcha

kaptcha.textproducer.impl 验证码文本生成器 默认为DefaultTextCreator

kaptcha.textproducer.char.string 验证码文本字符内容范围 默认为abcde2345678gfynmnpwx

kaptcha.textproducer.char.length 验证码文本字符长度 默认为5

kaptcha.textproducer.font.names 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)

kaptcha.textproducer.font.size 验证码文本字符大小 默认为40

kaptcha.textproducer.font.color 验证码文本字符颜色 默认为Color.BLACK

kaptcha.textproducer.char.space 验证码文本字符间距 默认为2

kaptcha.noise.impl 验证码噪点生成对象 默认为DefaultNoise

kaptcha.noise.color 验证码噪点颜色 默认为Color.BLACK

kaptcha.obscurificator.impl 验证码样式引擎 默认为WaterRipplekaptcha.word.impl 验证码文本字符渲染 默认为DefaultWordRenderer

kaptcha.background.impl 验证码背景生成器 默认为DefaultBackground

kaptcha.background.clear.from 验证码背景颜色渐进 默认为Color.LIGHT_GRAY

kaptcha.background.clear.to 验证码背景颜色渐进 默认为Color.WHITE

kaptcha.image.width 验证码图片宽度 默认为200kaptcha.image.height 验证码图片高度 默认为50

猜你喜欢

转载自lvjun106.iteye.com/blog/2196026