转自:http://ttaale.iteye.com/blog/808719

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文件

    <!--Kaptcha 验证码  --><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.border.color</param-name>  <param-value>105,179,90</param-value>  </init-param>       <init-param>  <param-name>kaptcha.textproducer.font.color</param-name>  <param-value>red</param-value>  </init-param>  <init-param>  <param-name>kaptcha.image.width</param-name>  <param-value>250</param-value>  </init-param>  <init-param>  <param-name>kaptcha.image.height</param-name>  <param-value>90</param-value>  </init-param>  <init-param>  <param-name>kaptcha.textproducer.font.size</param-name>  <param-value>70</param-value>  </init-param>  <init-param>  <param-name>kaptcha.session.key</param-name>  <param-value>code</param-value>  </init-param>  <init-param>  <param-name>kaptcha.textproducer.char.length</param-name>  <param-value>4</param-value>  </init-param>  <init-param>  <param-name>kaptcha.textproducer.font.names</param-name>  <param-value>宋体,楷体,微软雅黑</param-value>  </init-param>       </servlet> 

          <servlet-mapping><servlet-name>kaptcha</servlet-name><url-pattern>/ClinicCountManager/kaptcha.jpg</url-pattern></servlet-mapping>

    jsp页面使用:

  •     <table><tr><td><img src="/ClinicCountManager/kaptcha.jpg"></td><td valign="top"><form method="POST"><br>sec code:<input type="text" name="kaptchafield"><br /><input type="submit" name="submit"></form></td></tr></table><br /><br /><br /><br /><%String c = (String)session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);String parm = (String) request.getParameter("kaptchafield");out.println("Parameter: " + parm + " ? Session Key: " + c + " : ");if (c != null && parm != null) {if (c.equals(parm)) {out.println("<b>true</b>");} else {out.println("<b>false</b>");}           }%>

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

    1.  不用在web.xml进行相关配置,在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">250</prop><prop key="kaptcha.textproducer.font.size">90</prop><prop key="kaptcha.image.height">90</prop><prop key="kaptcha.session.key">code</prop><prop key="kaptcha.textproducer.char.length">4</prop><prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑</prop></props></constructor-arg></bean></property></bean>

    新建生成图片控制类:

  • import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    import com.google.code.kaptcha.Constants;
    import com.google.code.kaptcha.Producer;@Controller
    @RequestMapping("/")
    public class CaptchaImageCreateController {private Producer captchaProducer = null;@Autowiredpublic void setCaptchaProducer(Producer captchaProducer) {this.captchaProducer = captchaProducer;}@RequestMapping("/captcha-image")public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {response.setDateHeader("Expires", 0);// Set standard HTTP/1.1 no-cache headers.response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");// Set IE extended HTTP/1.1 no-cache headers (use addHeader).response.addHeader("Cache-Control", "post-check=0, pre-check=0");// Set standard HTTP/1.0 no-cache header.response.setHeader("Pragma", "no-cache");// return a jpegresponse.setContentType("image/jpeg");// create the text for the imageString capText = captchaProducer.createText();// store the text in the session
            request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);// create the image with the textBufferedImage bi = captchaProducer.createImage(capText);ServletOutputStream out = response.getOutputStream();// write the data outImageIO.write(bi, "jpg", out);try {out.flush();} finally {out.close();}return null;}}

    前台调用方式:

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

    取验证码的方式:

  •         String code = (String)session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
    

kaptcha 验证码在spring mvc 中的使用相关推荐

  1. Spring mvc 中使用 kaptcha 验证码

    2019独角兽企业重金招聘Python工程师标准>>> 生成验证码的方式有很多,个人认为较为灵活方便的是Kaptcha ,他是基于SimpleCaptcha的开源项目.使用Kaptc ...

  2. 彻底解决Spring mvc中时间的转换和序列化等问题

    彻底解决Spring mvc中时间的转换和序列化等问题 参考文章: (1)彻底解决Spring mvc中时间的转换和序列化等问题 (2)https://www.cnblogs.com/childkin ...

  3. spring mvc中的@propertysource

    在spring mvc中,在配置文件中的东西,可以在java代码中通过注解进行读取了: @PropertySource  在spring 3.1中开始引入 比如有配置文件 config.propert ...

  4. spring_在Spring MVC中使用多个属性文件

    spring 每个人都听说过将单个Web应用程序组合成一个大型Web应用程序的门户. 门户软件的工作原理类似于mashup -来自多个来源的内容是在单个服务中获取的,大部分都显示在单个网页中. 门户软 ...

  5. Spring MVC中处理Request和Response的策略

    前沿技术早知道,弯道超车有希望 积累超车资本,从关注DD开始 作者:码农小胖哥, 图文编辑:xj 来源:https://mp.weixin.qq.com/s/3eFygsiVl8dC2nRy8_8n5 ...

  6. Spring MVC 中的 forward 和 redirect

    Spring MVC 中,我们在返回逻辑视图时,框架会通过 viewResolver 来解析得到具体的 View,然后向浏览器渲染.假设逻辑视图名为 hello,通过配置,我们配置某个 ViewRes ...

  7. Spring MVC中获取当前项目的路径

    Spring MVC中获取当前项目的路径 在web.xml中加入以下内容 <!--获取项目路径--><context-param><param-name>webAp ...

  8. Spring 2.5:Spring MVC中的新特性

    转载说明:infoQ就是牛人多,看人家去年就把Spring2.5注视驱动的MVC写出来了,还是这么详细,我真是自叹不如,今天偶尔看到这篇文章非常认真的拜读了2遍,简直是茅厕顿开啊....\(^o^)/ ...

  9. Spring MVC中的二三事

    HandlerMapping和HandlerAdapter 这个两个组件应该算是spring mvc中最重要的几个组件之一了,当一个请求到达DispatcherSerlvet后,spring mvc就 ...

最新文章

  1. 如何使用CSS创建巧妙的动画提示框
  2. Git版本回退之 reset 和 revert
  3. [蓝桥杯][2013年第四届真题]带分数
  4. java运行时读取注解_Java自定义注解和运行时靠反射获取注解
  5. 第二:python安装校验报错api-ms-win-crt-process-l1-1-0.dll 丢失的处理
  6. 拉普拉斯变换的本质意义
  7. JQuery blockUI 的使用方法
  8. 第四章 ,数据处理--学习笔记
  9. 最经典的人生定律、法则、效应总结
  10. 集体建设用地审批程序:
  11. 【哈士奇赠书活动 - 22期】-〖ChatGPT时代:ChatGPT全能应用一本通〗
  12. 共轭函数和原函数的关系
  13. 分析少年派2中的Crypto
  14. 路由交换技术实战七 FR 网络中配置 OSPF( 完成版 )
  15. 2023电工杯数学建模AB题思路分析
  16. FastDFS 系统架构和功能原理
  17. 国内外遥感类核心期刊汇总
  18. 小米路由器AC2100写入OpenWrt
  19. 全文检索Lucene
  20. matlab中的将几条曲线画在一个坐标系下的方法,请问怎么将几条线画在同一个坐标轴下?有程序!...

热门文章

  1. MYSQL储存过程和储存函数和变量
  2. shell 登录mysql 然后quit_使用工具Xshell实现在linux上登录mysql和退出mysql的相关操作讲解...
  3. python小技巧及速度提高-python编码时有什么技巧可以提升速度?
  4. linux双屏显示不同内容,LINUX下双屏显示问题
  5. 盐城工学院计算机考研高吗,信息学院计算机班计玮考取中南大学研究生
  6. android 点击edittext全选,Android - 阻止edittext复制/粘贴,然后双击全选
  7. 王艳 201771010127《面向对象程序设计(java)》第十七周学习总结
  8. python 入门DAY1
  9. java-number2
  10. jQuery滚动指定位置