kaptcha 验证码组件使用
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>
- lt;/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;
- @Autowired
- public 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 jpeg
- response.setContentType("image/jpeg");
- // create the text for the image
- String capText = captchaProducer.createText();
- // store the text in the session
- request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
- // create the image with the text
- BufferedImage bi = captchaProducer.createImage(capText);
- ServletOutputStream out = response.getOutputStream();
- // write the data out
- ImageIO.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);
如果需要全部数字

- <init-param>
- <param-name>kaptcha.textproducer.char.string</param-name>
- <param-value>0123456789</param-value>
- </init-param>
去掉干扰线

- <init-param>
- <param-name>kaptcha.noise.impl</param-name>
- <param-value>com.google.code.kaptcha.impl.NoNoise </param-value>
- </init-param>
kaptcha 验证码组件使用的更多相关文章
- google kaptcha 验证码组件使用简介
kaptcha 是一个非常实用的验证码生成工具.有了它,你可以生成各种样式的验证码,因为它是可配置的.kaptcha工作的原理是调用 com.google.code.kaptcha.servlet.K ...
- kaptcha验证码组件使用简介
Kaptcha是一个基于SimpleCaptcha的验证码开源项目. 官网地址:http://code.google.com/p/kaptcha/ kaptcha的使用比较方便,只需添加jar包依赖之 ...
- 如何使用kaptcha验证码组件
kaptcha是基于SimpleCaptcha的验证码开源项目. kaptcha是纯配置的,使用起来比较友好.如使用了Servlet,所有配置都在web.xml中.如果你在项目中使用了开源框架(比如S ...
- java 实现登录验证码 (kaptcha 验证码组件)
验证码的作用: 1.防止广告机注册和发帖.评论.2.防止暴力破解密码,特别是有管理员权限的密码. 在这里介绍一种非常实用的验证码生成工具:kaptcha 这个工具,可以生成各种样式的验证码,因为它是可 ...
- 使用kaptcha验证码组件操作演示
1.创建一个Maven项目 2.在pom.xml中引入相关依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...
- jcaptcha和kaptcha验证码使用入门【转】
jcaptcha和kaptcha验证码使用入门 一.jcaptcha验证码使用 jcaptcha使用默认样式生成的验证码比较难以识别,所以需要自定义验证码的样式,包括,背景色.背景大小.字体.字体大小 ...
- 【干货】”首个“ .NET Core 验证码组件
前言 众所周知,Dotnet Core目前没有图形API,以前的System.Drawing程序集并没有包含在Dotnet Core 1.0环境中.不过在dotnet core labs项目里可以见到 ...
- Java实现验证码制作之一Kaptcha验证码
Kaptcha验证码 是google提供的验证码插件,使用起来相对简单,设置的干扰线以及字体扭曲不易让其他人读取破解. 这里我们需要 导入一个 kaptcha-2.3.jar 下载地址:http:/ ...
- kaptcha验证码插件的使用
kaptcha 是一个非常实用的验证码生成工具.有了它,你可以生成各种样式的验证码,因为它是可配置的.kaptcha工作的原理是调用 com.google.code.kaptcha.servlet.K ...
随机推荐
- UITextField关闭自动联想功能
在textField输入内容时,如果内容为英文,输入的英文如果不正确的单词就是有红色的线报警,关闭英文自动联想功能 self.autocorrectionType = UITextAutocorrec ...
- C语言socket编程——linux环境
先写一个服务器端的监听程序,功能室从客户端读取字符,接收到后告知客户端“I got your message: ”+收到的消息:server.c #include <stdio.h> #i ...
- BAYESIAN STATISTICS AND CLINICAL TRIAL CONCLUSIONS: WHY THE OPTIMSE STUDY SHOULD BE CONSIDERED POSITIVE(转)
Statistical approaches to randomised controlled trial analysis The statistical approach used in the ...
- CSS小技巧-怎样让每行多余的文字显示文省略号?
1.white-space:nowrap 如果是中文,则需要设置行末不断行 2.overflow:hidden 设置超出控件范围隐藏 3.text-overflow:ellipsis
- flash2print文档在线预览应用(java,.net)
一.背景 前段时间,LZ的boss突然给了出了这样一个需求:将原项目中的所有文章关联的附件TXT.PDF.office相关文件全部以flash的形式在网页上进行展示,便于预览.看似简单的需求,整个研发 ...
- python爬虫从入门到放弃(八)之 Selenium库的使用
一.什么是Selenium selenium 是一套完整的web应用程序测试系统,包含了测试的录制(selenium IDE),编写及运行(Selenium Remote Control)和测试的并行 ...
- ue4打包问题的巧妙解决——二分回退大法!
昨天突然发生了一件非常恐怖的事--我的ue4项目居然不能打包了!! 大概是这么一回事: UATHelper: 打包 (Windows (64位)): UnrealBuildTool: ERROR: ...
- Sqlmap注入Base64编码的注入点
不小心删了,找快照才找到的.补回来... 上次遇到一个Base64的注入点,手工注入太麻烦,于是在网上看了一下sqlmap Base64注入的方法,如下: sqlmap -u http://xxxx. ...
- Page directive must not have multiple occurrences of pageencoding
一个jsp文件中不能同时出现两个 p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #932192 } pageE ...
- Linux_Ununtu 16.04 的下载安装并部署.Net Core 网站
第一次接触Linux也难免有些懵逼,因为公司项目必须用.Net Core 开发一个后端服务应用:第一次用Linux给我的感觉就像在用2000年的手机一样:没用智能的操作:让人崩溃的用户体验.说多了都是 ...