Kaptcha是一个基于SimpleCaptcha的验证码开源项目。在springMVC环境下,使用kaptcha
web.xml配置代码:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <display-name></display-name>
- <servlet>
- <servlet-name>hello</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>hello</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- <servlet>
- <servlet-name>Kaptcha</servlet-name>
- <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
- <init-param>
- <param-name>kaptcha.border</param-name> <!-- 是否有边框 -->
- <param-value>no</param-value>
- </init-param>
- <init-param>
- <param-name>kaptcha.textproducer.char.space</param-name> <!--字符之间的间距 -->
- <param-value>8</param-value>
- </init-param>
- <init-param>
- <param-name>kaptcha.textproducer.char.length</param-name> <!-- 字符的个数 -->
- <param-value>4</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>Kaptcha</servlet-name>
- <url-pattern>/Kaptcha.jpg</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
hello-servlet.xml配置代码:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <context:component-scan base-package="com.lqh.controller" />
- <mvc:annotation-driven />
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/jsp/" /> <!-- 视图前缀 -->
- <property name="suffix" value=".jsp" /> <!-- 视图后缀 -->
- </bean>
- </beans>
controller代码:
- @Controller
- public class HelloController {
- @RequestMapping(value={"/", "/hello"})
- public String hello() {
- System.out.println("hello");
- return "hello";
- }
- @RequestMapping(value="/hello", method=RequestMethod.POST)
- public String hello(String verifyCode, HttpServletRequest request) {
- String code = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY); //获取生成的验证码
- System.out.println(verifyCode + "," + code);
- if(verifyCode.equals(code)) {
- System.out.println("验证通过 ");
- }
- return "redirect:/hello"; //post方式提交,实现客户端跳转
- }
- }
前台页面hello.jsp代码:
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'index.jsp' starting page</title>
- </head>
- <body>
- hello.
- <form method="post">
- <input type="text" name="verifyCode" />
- <img alt="验证码" src="Kaptcha.jpg"> <!-- KaptchaServlet会过滤该请求,返回验证码图片--!>
- <input type="submit" value="提交" />
- </form>
- </body>
- </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
版权声明:本文为博主原创文章,未经博主允许不得转载。