当前位置:首页 » Web前端技术

简单Maven的Web项目之验证码(Kaptcha篇)

2015-09-07 09:05 本站整理 浏览(338)

一:Kaptcha介绍

简单介绍:
kaptcha 是一个扩展自 simplecaptcha 的验证码库,在 Java 编程中 是一个非常实用的验证码生成工具。我们可以利用这个工具生成各种样式的验证码,因为它是可配置的,我们可以根据需求定制。
工作原理:
kaptcha 的工作原理是调用 com.google.code.kaptcha.servlet.KaptchaServlet ,生成一个验证码图片,响应到客户端,同时将生成的真是的验证码字符串放到 HttpSession 中。
使用kaptcha可以方便的配置:
验证码的字体
验证码字体的大小
验证码字体的字体颜色
验证码内容的范围(数字,字母,中文汉字!)
验证码图片的大小,边框,边框粗细,边框颜色
验证码的干扰线(可以自己继承com.google.code.kaptcha.NoiseProducer写一个自定义的干扰线)
验证码的样式(鱼眼样式、3D、普通模糊……当然也可以继承com.google.code.kaptcha.GimpyEngine自定义样式)

二:使用Kaptcha生成验证码

首先下载jar包
下载地址:http://code.google.com/p/kaptcha/downloads/list
然后添加到本地Maven库中命令如下可根据自己情况修改对应路径
mvn install:install-file -DgroupId=com.google.code -DartifactId=kaptcha -Dversion=2.3.2 -Dfile=D:\kaptcha-2.3.jar -Dpackaging=jar -DgeneratePom=true

pom.xml中的配置如下,注意要和上面的groupId、artifactId和version保持一致
<dependency>  
        <groupId>com.google.code</groupId>  
        <artifactId>kaptcha</artifactId>  
        <version>2.3.2</version>  
    </dependency>

下面看代码
ChineseText.java
package com.verify.kaptcha;

import java.util.Random;

import com.google.code.kaptcha.text.TextProducer;
import com.google.code.kaptcha.util.Configurable;

public class ChineseText extends Configurable implements TextProducer {

    public String getText() {
        int length = getConfig().getTextProducerCharLength();
        //char[] charS = getConfig().getTextProducerCharString();

        String[] s = new String[]{"验","证","码","测","试"};

        Random rand = new Random();
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < length; i++){
            int ind = rand.nextInt(s.length);
            sb.append(s[ind]);
        }
        return sb.toString();
    }
    /**
     * 中午实例
     * @return
     */
    public String getText1() {
        int length = getConfig().getTextProducerCharLength();
        String finalWord = "", firstWord = "";
        int tempInt = 0;
        String[] array = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
                "a", "b", "c", "d", "e", "f","g","h","i","j","k","l","m","n",
                "o","p","q","r","s","t","u","v","w","x","y","z" };

        Random rand = new Random();

        for (int i = 0; i < length; i++) {
            switch (rand.nextInt(array.length)) {
            case 1:
                tempInt = rand.nextInt(26) + 65;
                firstWord = String.valueOf((char) tempInt);
                break;
            case 2:
                int r1,
                r2,
                r3,
                r4;
                String strH,
                strL;// high&low
                r1 = rand.nextInt(3) + 11; // 前闭后开[11,14)
                if (r1 == 13) {
                    r2 = rand.nextInt(7);
                } else {
                    r2 = rand.nextInt(16);
                }

                r3 = rand.nextInt(6) + 10;
                if (r3 == 10) {
                    r4 = rand.nextInt(15) + 1;
                } else if (r3 == 15) {
                    r4 = rand.nextInt(15);
                } else {
                    r4 = rand.nextInt(16);
                }

                strH = array[r1] + array[r2];
                strL = array[r3] + array[r4];

                byte[] bytes = new byte[2];
                bytes[0] = (byte) (Integer.parseInt(strH, 16));
                bytes[1] = (byte) (Integer.parseInt(strL, 16));

                firstWord = new String(bytes);
                break;
            default:
                tempInt = rand.nextInt(10) + 48;
                firstWord = String.valueOf((char) tempInt);
                break;
            }
            finalWord += firstWord;
        }
        return finalWord;
    }
}

生成验证码的servlet
package com.verify.kaptcha;

import java.awt.image.BufferedImage;
import java.io.IOException;
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 com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.util.Config;

public class KaptchaServlet extends HttpServlet implements Servlet {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Properties props;
    private Producer kaptchaProducer;
    private String sessionKeyValue;

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

        this.kaptchaProducer = null;

        this.sessionKeyValue = null;
    }

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

        ImageIO.setUseCache(false);

        @SuppressWarnings("rawtypes")
        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();
    }

    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 s1 = capText.substring(0, 1);
        String s2 = capText.substring(1, 2);
        int r = Integer.valueOf(s1).intValue() + Integer.valueOf(s2).intValue();

        req.getSession().setAttribute(this.sessionKeyValue, String.valueOf(r));

        BufferedImage bi = this.kaptchaProducer.createImage(s1+"+"+s2+"=?");

        ServletOutputStream out = resp.getOutputStream();

        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
    }
}

web.xml中的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 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"> 
    <!-- 登陆验证码Kaptcha 2-->
    <servlet>
        <servlet-name>Kaptcha</servlet-name>
        <servlet-class>
            com.verify.kaptcha.KaptchaServlet
        </servlet-class>
        <init-param>
            <description>图片边框,合法值:yes , no</description>
            <param-name>kaptcha.border</param-name>
            <param-value>yes</param-value>
        </init-param>
        <init-param>
            <description>
                边框颜色,合法值: r,g,b (and optional alpha) 或者
                white,black,blue.
            </description>
            <param-name>kaptcha.border.color</param-name>
            <param-value>black</param-value>
        </init-param>
        <init-param>
            <description>边框厚度,合法值:>0</description>
            <param-name>kaptcha.border.thickness</param-name>
            <param-value>1</param-value>
        </init-param>
        <init-param>
            <description>图片宽 200</description>
            <param-name>kaptcha.image.width</param-name>
            <param-value>200</param-value>
        </init-param>
        <init-param>
            <description>图片高 50</description>
            <param-name>kaptcha.image.height</param-name>
            <param-value>50</param-value>
        </init-param>
        <init-param>
            <description>图片实现类</description>
            <param-name>kaptcha.producer.impl</param-name>
            <param-value>
                com.google.code.kaptcha.impl.DefaultKaptcha
            </param-value>
        </init-param>
        <init-param>
            <description>文本实现类</description>
            <param-name>kaptcha.textproducer.impl</param-name>
            <param-value>
                com.google.code.kaptcha.text.impl.DefaultTextCreator
            </param-value>
        </init-param>
        <init-param>
            <description>文本集合,验证码值从此集合中获取</description>
            <param-name>kaptcha.textproducer.char.string</param-name>
            <param-value>1234567890</param-value>
        </init-param>
        <init-param>
            <description>验证码长度 5</description>
            <param-name>kaptcha.textproducer.char.length</param-name>
            <param-value>2</param-value>
        </init-param>
        <init-param>
            <description>字体 Arial, Courier</description>
            <param-name>kaptcha.textproducer.font.names</param-name>
            <param-value>Arial, Courier</param-value>
        </init-param>
        <init-param>
            <description>字体大小 40px.</description>
            <param-name>kaptcha.textproducer.font.size</param-name>
            <param-value>40</param-value>
        </init-param>
        <init-param>
            <description>
                字体颜色,合法值: r,g,b 或者 white,black,blue.
            </description>
            <param-name>kaptcha.textproducer.font.color</param-name>
            <param-value>black</param-value>
        </init-param>
        <init-param>
            <description>文字间隔 2</description>
            <param-name>kaptcha.textproducer.char.space</param-name>
            <param-value>2</param-value>
        </init-param>
        <init-param>
            <description>干扰实现类</description>
            <param-name>kaptcha.noise.impl</param-name>
            <param-value>
                <!-- com.google.code.kaptcha.impl.NoNoise -->
                com.google.code.kaptcha.impl.DefaultNoise
            </param-value>
        </init-param>
        <init-param>
            <description>
                干扰颜色,合法值: r,g,b 或者 white,black,blue.
            </description>
            <param-name>kaptcha.noise.color</param-name>
            <param-value>black</param-value>
        </init-param>
        <init-param>
            <description>
                图片样式: 水纹com.google.code.kaptcha.impl.WaterRipple
                鱼眼com.google.code.kaptcha.impl.FishEyeGimpy
                阴影com.google.code.kaptcha.impl.ShadowGimpy
            </description>
            <param-name>kaptcha.obscurificator.impl</param-name>
            <param-value>
                com.google.code.kaptcha.impl.WaterRipple
            </param-value>
        </init-param>
        <init-param>
            <description>背景实现类</description>
            <param-name>kaptcha.background.impl</param-name>
            <param-value>
                com.google.code.kaptcha.impl.DefaultBackground
            </param-value>
        </init-param>
        <init-param>
            <description>背景颜色渐变,开始颜色</description>
            <param-name>kaptcha.background.clear.from</param-name>
            <param-value>green</param-value>
        </init-param>
        <init-param>
            <description>背景颜色渐变,结束颜色</description>
            <param-name>kaptcha.background.clear.to</param-name>
            <param-value>white</param-value>
        </init-param>
        <init-param>
            <description>文字渲染器</description>
            <param-name>kaptcha.word.impl</param-name>
            <param-value>
                com.google.code.kaptcha.text.impl.DefaultWordRenderer
            </param-value>
        </init-param>
        <init-param>
            <description>
                session中存放验证码的key键
            </description>
            <param-name>kaptcha.session.key</param-name>
            <param-value>KAPTCHA_SESSION_KEY</param-value>
        </init-param>
        <init-param>
            <description>
                The date the kaptcha is generated is put into the
                HttpSession. This is the key value for that item in the
                session.
            </description>
            <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>/randomcode.jpg</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

验证码页面index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>kaptcha生成验证码</title>
<script type="text/javascript">
    function changeR(node){
        // 用于点击时产生不同的验证码
        node.src = "randomcode.jpg?time="+new Date().getTime() ;    
    }
</script>
</head>
<body>
<div style="text-align: center;">
<h2>验证码之kaptcha</h2>
<img alt="random" src="randomcode.jpg" onclick="changeR(this)" style="cursor: pointer;"><br/><br/>
    <form action="checkCode.jsp">
        <input type="text" name="checkCode">
        <input type="submit" value="提交">
    </form> 
</div>
</body>
</html>

验证的页面checkCode.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Kaptcha验证码验证</title>
</head>
<body>
    <%
        //从session中取服务器中的验证码
        String kaptchaServer = (String)request.getSession().getAttribute
        (com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
        String kaptchaClient = request.getParameter("checkCode");
        out.print("<div style='text-align: center;'>");
        if (kaptchaServer.equals(kaptchaClient))
            out.print("<h2>verify success</h2>");
        else
            out.println("<h2>verify fail</h2>");

        out.print("<h4>kaptchaServer===><span style='color:red;'>"+kaptchaServer+"</span>\tkaptchaClient===><span style='color:red;'>"+kaptchaClient+"</span></h4>");
        out.print("</div>");
    %>
</body>
</html>

3.下面来看看效果图




参考文档:
http://www.open-open.com/lib/view/open1395238908947.html
http://blog.sina.com.cn/s/blog_6b85e9c00100uvoh.html
http://stone02111.iteye.com/blog/1688195