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

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

参考官方文档https://code.google.com/p/kaptcha/wiki/ConfigParameters将其他属性也配置进去,修改后web.xml文件如下:





Kaptcha

com.xhc.kaptchaServlet.MyKaptchaServlet



kaptcha.border
yes



kaptcha.border.color
black



kaptcha.border.thickness
1



kaptcha.image.width
200



kaptcha.image.height
50



kaptcha.producer.impl
com.google.code.kaptcha.impl.DefaultKaptcha



kaptcha.textproducer.impl
com.google.code.kaptcha.text.impl.DefaultTextCreator



kaptcha.textproducer.char.string
01234565789



kaptcha.textproducer.char.length
4



kaptcha.textproducer.font.names
Arial,Courier



kaptcha.textproducer.font.size
40



kaptcha.textproducer.font.color
blue



kaptcha.textproducer.char.space
5



kaptcha.noise.impl
com.google.code.kaptcha.impl.DefaultNoise



kaptcha.noise.color
yellow



kaptcha.obscurificator.impl
com.google.code.kaptcha.impl.FishEyeGimpy



kaptcha.background.clear.from
gray



kaptcha.background.clear.to
white



kaptcha.word.impl
com.google.code.kaptcha.text.impl.DefaultWordRenderer


kaptcha.session.key
KAPTCHA_SESSION_KEY


kaptcha.session.date
KAPTCHA_SESSION_DATE




Kaptcha
/Kaptcha.jpg



KaptchaExample.jsp



7.效果图
使用kaptcha插件生成加法计算验证码_第2张图片

你可能感兴趣的:(Java,web,验证框架)