kaptcha 是一个非常实用的验证码生成工具。有了它,你可以生成各种样式的验证码,因为它是可配置的。kaptcha工作的原理是调用 com.google.code.kaptcha.servlet.KaptchaServlet,生成一个图片。同时将生成的验证码字符串放到 HttpSession中。
1.在applicationContext.xml中配置,切记启动时别忘了加载。
<bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha"> <property name="config"> <bean class="com.google.code.kaptcha.util.Config"> <constructor-arg> <props> <prop key="kaptcha.border">no</prop> <prop key="kaptcha.border.color">105,179,90</prop> <prop key="kaptcha.textproducer.font.color">red</prop> <prop key="kaptcha.image.width">80</prop> <prop key="kaptcha.textproducer.font.size">30</prop> <prop key="kaptcha.image.height">30</prop> <prop key="kaptcha.session.key">code</prop> <prop key="kaptcha.textproducer.char.length">4</prop> <prop key="kaptcha.textproducer.font.names">Arial, Courier</prop> <prop key="kaptcha.GimpyEngine">WaterRipple</prop> <prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.NoNoise</prop> </props> </constructor-arg> </bean> </property> </bean>
2.新建图片控制类CaptchaController.java
package com.ly.controller; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.google.code.kaptcha.Constants; import com.google.code.kaptcha.Producer; @Controller @RequestMapping("/captchaController") public class CaptchaController extends BaseController { @Autowired private Producer captchaProducer = null; @RequestMapping("/image") public String getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception { HttpSession session = request.getSession(); String code = (String) session.getAttribute(Constants.KAPTCHA_SESSION_KEY); System.out.println("******************验证码是: " + code); response.setDateHeader("Expires", 0); response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate"); response.addHeader("Cache-Control", "post-check=0, pre-check=0"); response.setHeader("Pragma", "no-cache"); response.setContentType("image/jpeg"); String capText = captchaProducer.createText(); session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText); BufferedImage bi = captchaProducer.createImage(capText); ServletOutputStream out = response.getOutputStream(); ImageIO.write(bi, "jpg", out); try { out.flush(); } finally { out.close(); } return null; } }
3.前台登录页面.jsp
<div id="ck"> 验证码 <input type="text" id="kaptcha" name="kaptcha" style="width: 60px;" class="easyui-validatebox" maxlength="4" data-options="required:true"/> <img id="kaptchaImage" src="captchaController/image" style="margin-bottom: -10px"/> </div> <script type="text/javascript"> $('#kaptchaImage').click(function () {//生成验证码 $(this).hide().attr('src', 'captchaController/image?' + Math.floor(Math.random()*100) ).fadeIn(); event.cancelBubble=true; }); </script>
4.验证登录什么的功能,自己发挥吧!
以下另送配置属性参考值:
<servlet> <servlet-name>Kaptcha</servlet-name> <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class> <init-param> <description> Border around kaptcha. Legal values are yes or no. </description> <param-name>kaptcha.border</param-name> <param-value>no</param-value> </init-param> <init-param> <description>Color of the border. Legal values are r,g,b (and optional alpha) or white,black,blue. </description> <param-name>kaptcha.border.color</param-name> <param-value>red</param-value> </init-param> <init-param> <description>Thickness of the border around kaptcha. Legal values are > 0. </description> <param-name>kaptcha.border.thickness</param-name> <param-value>5</param-value> </init-param> <init-param> <description>Width in pixels of the kaptcha image. </description> <param-name>kaptcha.image.width</param-name> <param-value>80</param-value> </init-param> <init-param> <description>Height in pixels of the kaptcha image. </description> <param-name>kaptcha.image.height</param-name> <param-value>40</param-value> </init-param> <init-param> <description>The image producer. </description> <param-name>kaptcha.producer.impl</param-name> <param-value>com.google.code.kaptcha.impl.DefaultKaptcha </param-value> </init-param> <init-param> <description>The text producer. </description> <param-name>kaptcha.textproducer.impl</param-name> <param-value>com.google.code.kaptcha.text.impl.DefaultTextCreator</param-value> </init-param> <init-param> <description>The characters that will create the kaptcha. </description> <param-name>kaptcha.textproducer.char.string</param-name> <param-value>abcde2345678gfynmnpwx </param-value> </init-param> <init-param> <description>The number of characters to display. </description> <param-name>kaptcha.textproducer.char.length</param-name> <param-value>5</param-value> </init-param> <init-param> <description>A list of comma separated font names.</description> <param-name>kaptcha.textproducer.font.names</param-name> <param-value>Arial, Courier</param-value> </init-param> <init-param> <description>The size of the font to use. </description> <param-name>kaptcha.textproducer.font.size</param-name> <param-value>23</param-value> </init-param> <init-param> <description>The color to use for the font. Legal values are r,g,b. </description> <param-name>kaptcha.textproducer.font.color</param-name> <param-value>black</param-value> </init-param> <init-param> <description>The noise producer. </description> <param-name>kaptcha.noise.impl</param-name> <param-value>com.google.code.kaptcha.impl.NoNoise </param-value> </init-param> <init-param> <description>The noise color. Legal values are r,g,b. </description> <param-name>kaptcha.noise.color</param-name> <param-value>black</param-value> </init-param> <init-param> <description>The obscurificator implementation. </description> <param-name>kaptcha.obscurificator.impl</param-name> <param-value>com.google.code.kaptcha.impl.ShadowGimpy</param-value> </init-param> <init-param> <description>The background implementation. </description> <param-name>kaptcha.background.impl</param-name> <param-value>com.google.code.kaptcha.impl.DefaultBackground</param-value> </init-param> <init-param> <description>Ending background color. Legal values are r,g,b. </description> <param-name>kaptcha.background.clear.to</param-name> <param-value>white</param-value> </init-param> <init-param> <description>The word renderer implementation. </description> <param-name>kaptcha.word.impl</param-name> <param-value>com.google.code.kaptcha.text.impl.DefaultWordRenderer</param-value> </init-param> <init-param> <description>The value for the kaptcha is generated and is put into the HttpSession. This is the key value for that item in the session. </description> <param-name>kaptcha.session.key</param-name> <param-value>KAPTCHA_SESSION_KEY</param-value> </init-param> <init-param> <description>The date the kaptcha is generated is put into the HttpSession. This is the key value for that item in the session. </description> <param-name>kaptcha.session.date</param-name> <param-value>KAPTCHA_SESSION_DATE</param-value> </init-param> </servlet>
相关推荐
kaptcha 图片验证码 springMVC project demo
第一个springMVC示例+源码,仅供入门.
SpringMVC核心配置文件示例和文档
MAVEN添加依赖包 无需数据库 都是静态代码
springMVC的简单示例,同时讲到了如何在servlet中获取springBean 是一个很好的学习springmvc 的开始资料
kaptcha 是一个非常实用的验证码生成工具。有了它,你可以生成各种样式的验证码,因为它是可配置的,下面这篇文章主要给大家介绍了关于Spring MVC中使用Google kaptcha验证码的方法,需要的朋友可以参考借鉴,下面来...
springmvc 示例项目goodDay 详情参考博客http://blog.csdn.net/niaoer2010/article/details/78732865
本篇文章主要介绍了spring mvc 使用kaptcha生成验证码实例,详细的介绍了使用Kaptcha 生成验证码的步骤,有兴趣的可以了解一下
传智播客Java进阶Spring和springMVC详细示例精通教程资料 传智播客Java进阶Spring和springMVC详细示例精通教程资料 传智播客Java进阶Spring和springMVC详细示例精通教程资料
springmvc 基于注解的入门实例,仅适合初学者参考!
比较完整的SpringMVC的demo,里面包含了对数据增删改查,上传下载文件的实现,异常处理,拦截器,国际化等等功能的实现
cas 结合 springmvc shiro 做项目单点登录包括cas服务端,cas客户端。shiro-cas集成项目
使用SpringMVC框架,用Maven作为包依赖管理工具,此示例下载于慕课网的SpringMVC起步章节。
本篇文章主要介绍了springmvc下实现登录验证码功能示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
activeMQ+spring+springmvc整合示例
springmvc简单样例代码,可以参考一下
花了一天写的,把这几个都整合了一起,包括事务SpringMVC4.0 + MyBatis3.2 + 验证码 + 邮箱 + Log4j.rar