码迷,mamicode.com
首页 > 其他好文 > 详细

使用kaptcha插件生成加法计算验证码

时间:2015-07-15 22:56:32      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:kaptcha   加法验证码   反编译   插件   

1.从官网https://code.google.com/p/kaptcha/下载kaptcha压缩文件,解压文件后里面有一个war文件,打开Eclipse/MyEclipse将其import进去,然后部署到服务器,在浏览器输入url即可看到kaptcha官方提供的基本demo的运行情况,现在将其改为加法计算验证。
2.首先查看web.xml文件发现用来生成验证码的servlet为KaptchaServlet
技术分享
3.找到KaptchaServlet.class文件,然后进行反编译。
4.新建一个自己的验证码MyKaptchaServlet,将反编译得到的源码拷贝进来。
5.对MyKaptchaServlet做如下修改。
技术分享
修改后MyKaptchaServlet.java代码如下:
package com.xhc.kaptchaServlet;

import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.util.Config;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import javax.imageio.ImageIO;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MyKaptchaServlet extends HttpServlet implements Servlet {
 private Properties props;
 private Producer kaptchaProducer;
 private String sessionKeyValue;
 private String sessionKeyDateValue;

 public MyKaptchaServlet() {
  this.props = new Properties();

  this.kaptchaProducer = null;

  this.sessionKeyValue = null;

  this.sessionKeyDateValue = null;
 }

 public void init(ServletConfig conf) throws ServletException {
  super.init(conf);

  ImageIO.setUseCache(false);

  Enumeration initParams = conf.getInitParameterNames();
  while (initParams.hasMoreElements()) {
   String key = (String) initParams.nextElement();
   String value = conf.getInitParameter(key);
   this.props.put(key, value);
  }

  Config config = new Config(this.props);
  this.kaptchaProducer = config.getProducerImpl();
  this.sessionKeyValue = config.getSessionKey();
  this.sessionKeyDateValue = config.getSessionDate();
 }

 public void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  resp.setDateHeader("Expires", 0L);

  resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");

  resp.addHeader("Cache-Control", "post-check=0, pre-check=0");

  resp.setHeader("Pragma", "no-cache");

  resp.setContentType("image/jpeg");

  String capText = this.kaptchaProducer.createText();

  String str1 = capText.substring(0, 2);
  String str2 = capText.substring(2, 4);
  int result = Integer.valueOf(str1) + Integer.valueOf(str2);
  req.getSession().setAttribute(this.sessionKeyValue, result+"");

  req.getSession().setAttribute(this.sessionKeyDateValue, new Date());

  BufferedImage bi = this.kaptchaProducer.createImage(str1 + "+" + str2
    + "=?");

  ServletOutputStream out = resp.getOutputStream();

  ImageIO.write(bi, "jpg", out);
 }
}


6.修改web.xml配置文件,将servlet-class指定到MyKaptchaServlet
技术分享

参考官方文档https://code.google.com/p/kaptcha/wiki/ConfigParameters将其他属性也配置进去,修改后web.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<servlet>
<servlet-name>Kaptcha</servlet-name>
<!-- <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class> -->
<servlet-class>com.xhc.kaptchaServlet.MyKaptchaServlet</servlet-class>
<!-- For a complete list of Init Parameters, please see: http://code.google.com/p/kaptcha/wiki/ConfigParameters -->
<!-- 验证码边界,合法值yes or no -->
<init-param>
<param-name>kaptcha.border</param-name>
<param-value>yes</param-value>
</init-param>
<!-- 边界颜色,合法值 r,g,b (and optional alpha) or white,black,blue -->
<init-param>
<param-name>kaptcha.border.color</param-name>
<param-value>black</param-value>
</init-param>
<!-- 边界厚度,合法值 大于0 -->
<init-param>
<param-name>kaptcha.border.thickness</param-name>
<param-value>1</param-value>
</init-param>
<!-- 验证码图片宽度 -->
<init-param>
<param-name>kaptcha.image.width</param-name>
<param-value>200</param-value>
</init-param>
<!-- 验证码图片高度 -->
<init-param>
<param-name>kaptcha.image.height</param-name>
<param-value>50</param-value>
</init-param>
<!-- 验证码图片生成类,默认由DefaultKaptcha类生成,可以自己写一个图片生成类,需要实现Producer接口并继承Configurable 
类 -->
<init-param>
<param-name>kaptcha.producer.impl</param-name>
<param-value>com.google.code.kaptcha.impl.DefaultKaptcha</param-value>
</init-param>
<!-- 验证码生成类,默认由DefaultTextCreator类生成,可以自己写一个验证码生成类,需要实现TextProducer接口并继承Configurable 
类 -->
<init-param>
<param-name>kaptcha.textproducer.impl</param-name>
<param-value>com.google.code.kaptcha.text.impl.DefaultTextCreator</param-value>
</init-param>
<!-- 用来生成验证码的字符集 -->
<init-param>
<param-name>kaptcha.textproducer.char.string</param-name>
<param-value>01234565789</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>Arial,Courier</param-value>
</init-param>
<!-- 字体大小 -->
<init-param>
<param-name>kaptcha.textproducer.font.size</param-name>
<param-value>40</param-value>
</init-param>
<!-- 字体颜色 -->
<init-param>
<param-name>kaptcha.textproducer.font.color</param-name>
<param-value>blue</param-value>
</init-param>
<!-- 验证码字符间隔 -->
<init-param>
<param-name>kaptcha.textproducer.char.space</param-name>
<param-value>5</param-value>
</init-param>
<!-- 图片干扰生成类,默认由DefaultNoise类生成,可以自己写一个图片干扰类,需要实现NoiseProducer接口并继承Configurable 
类 -->
<init-param>
<param-name>kaptcha.noise.impl</param-name>
<param-value>com.google.code.kaptcha.impl.DefaultNoise</param-value>
</init-param>
<!-- 干扰颜色 -->
<init-param>
<param-name>kaptcha.noise.color</param-name>
<param-value>yellow</param-value>
</init-param>
<!--获取扭曲的不同风格的验证码,官方实现的有ShadowGimpy,FishEyeGimpy,WaterRipple,其中用来FishEyeGimpy之后上面设定的干扰线不起作用-->
<init-param>
<param-name>kaptcha.obscurificator.impl</param-name>
<param-value>com.google.code.kaptcha.impl.FishEyeGimpy</param-value>
</init-param>
<!--渐变背景色,初始颜色 -->
<init-param>
<param-name>kaptcha.background.clear.from</param-name>
<param-value>gray</param-value>
</init-param>
<!-- 渐变北京市,最终颜色 -->
<init-param>
<param-name>kaptcha.background.clear.to</param-name>
<param-value>white</param-value>
</init-param>
<!-- 文字渲染器 -->
<init-param>
<param-name>kaptcha.word.impl</param-name>
<param-value>com.google.code.kaptcha.text.impl.DefaultWordRenderer</param-value>
</init-param>
<init-param>
<param-name>kaptcha.session.key</param-name>
<param-value>KAPTCHA_SESSION_KEY</param-value>
</init-param>
<init-param>
<param-name>kaptcha.session.date</param-name>
<param-value>KAPTCHA_SESSION_DATE</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>KaptchaExample.jsp</welcome-file>
</welcome-file-list>
</web-app>


7.效果图
技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

使用kaptcha插件生成加法计算验证码

标签:kaptcha   加法验证码   反编译   插件   

原文地址:http://blog.csdn.net/sinat_23128227/article/details/46897341

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!