验证码的制作和验证

Posted candywu-blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了验证码的制作和验证相关的知识,希望对你有一定的参考价值。

三、Kaptcha的扩展----算式验证码

  1.将原来的KaptchaServlet.class反编译为KaptchaServlet.java,在其基础上修改,得新的KaptchaServlet.java

  【反编译软件:java Decompiler】【代码有注释部分为修改部分】

 1 package com.kaptcha;
 2 
 3 import java.awt.image.BufferedImage;
 4 import java.io.IOException;
 5 import java.util.Date;
 6 import java.util.Enumeration;
 7 import java.util.Properties;
 8 
 9 import javax.imageio.ImageIO;
10 import javax.servlet.Servlet;
11 import javax.servlet.ServletConfig;
12 import javax.servlet.ServletException;
13 import javax.servlet.ServletOutputStream;
14 import javax.servlet.http.HttpServlet;
15 import javax.servlet.http.HttpServletRequest;
16 import javax.servlet.http.HttpServletResponse;
17 
18 import com.google.code.kaptcha.Producer;
19 import com.google.code.kaptcha.util.Config;
20 
21 public class KaptchaServlet extends HttpServlet
22   implements Servlet
23 {
24   private Properties props;
25   private Producer kaptchaProducer;
26   private String sessionKeyValue;
27   private String sessionKeyDateValue;
28 
29   public KaptchaServlet()
30   {
31     this.props = new Properties();
32 
33     this.kaptchaProducer = null;
34 
35     this.sessionKeyValue = null;
36 
37     this.sessionKeyDateValue = null;
38   }
39 
40   public void init(ServletConfig conf)
41     throws ServletException
42   {
43     super.init(conf);
44 
45     ImageIO.setUseCache(false);
46 
47     Enumeration initParams = conf.getInitParameterNames();
48     while (initParams.hasMoreElements())
49     {
50       String key = (String)initParams.nextElement();
51       String value = conf.getInitParameter(key);
52       this.props.put(key, value);
53     }
54 
55     Config config = new Config(this.props);
56     this.kaptchaProducer = config.getProducerImpl();
57     this.sessionKeyValue = config.getSessionKey();
58     this.sessionKeyDateValue = config.getSessionDate();
59   }
60 
61   public void doGet(HttpServletRequest req, HttpServletResponse resp)
62     throws ServletException, IOException
63   {
64     resp.setDateHeader("Expires", 0L);
65 
66     resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
67 
68     resp.addHeader("Cache-Control", "post-check=0, pre-check=0");
69 
70     resp.setHeader("Pragma", "no-cache");
71 
72     resp.setContentType("image/jpeg");
73 
74     String capText = this.kaptchaProducer.createText();
75     //分离出数字1
76     String s1=capText.substring(0,1);
77     //分离出数字2
78     String s2=capText.substring(1,2);
79     //计算算式和
80     int r=Integer.valueOf(s1).intValue()+Integer.valueOf(s2).intValue();
81     //将算式和加入session中
82     req.getSession().setAttribute(this.sessionKeyValue, String.valueOf(r));
83     
84     req.getSession().setAttribute(this.sessionKeyDateValue, new Date());
85     //显示s1+s2=?图片
86     BufferedImage bi = this.kaptchaProducer.createImage(s1+"+"+s2+"=?");
87 
88     ServletOutputStream out = resp.getOutputStream();
89 
90     ImageIO.write(bi, "jpg", out);
91   }
92 }

  2.修改web.xml

  (1)验证码取值范围:

1       <init-param>
2           <description>文本集合,验证码值从此集合中获取</description>
3           <param-name>kaptcha.textproducer.char.string</param-name>
4           <param-value>1234567890</param-value>
5       </init-param>

  (2)验证码长度,两个值:

1       <init-param>
2           <description>验证码长度,默认:5</description>
3           <param-name>kaptcha.textproducer.char.length</param-name>
4           <param-value>2</param-value>
5       </init-param>

  (3)修改servlet-class

1 <servlet-class>com.kaptcha.KaptchaServlet</servlet-class>

  4.效果图

     技术分享图片

**注:学习自慕课网《Java实现验证码制作》,附链接https://www.imooc.com/learn/283

以上是关于验证码的制作和验证的主要内容,如果未能解决你的问题,请参考以下文章

Java验证码的jar制作

php验证码的制作

动态验证码的制作

PHP验证码的制作教程

GD图片(画布)的制作及验证码的应用

php制作验证码