继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

java实现验证码制作(1)

大叔_fighting
关注TA
已关注
手记 81
粉丝 44
获赞 400

kaptcha:kaptcha 是一个非常实用的验证码生成工具
jcaptcha使用默认样式生成的验证码比较难以识别,所以需要自定义验证码的样式,包括,背景色、背景大小、字体、字体大小、生成的字符数等。相对与kaptcha比较复杂。

kaptcha使用
配置web.xml
?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>captcha</display-name>
<servlet>
<servlet-name>Kaptcha</servlet-name>
<servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
<init-param>
<description>图片边框,合法值 yea 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>blue</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>图片宽度</description>
<param-name>kaptcha.image.width</param-name>
<param-value>200</param-value>
</init-param>
<init-param>
<description>图片高度</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>1234560789</param-value>
</init-param>
<init-param>
<description>验证码长度 5</description>
<param-name>kaptcha.textproducer.char.length</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<description>字体</description>
<param-name>kaptcha.textproducer.font.names</param-name>
<param-value>Console</param-value>
</init-param>
<init-param>
<description>字体大小</description>
<param-name>kaptcha.textproducer.font.size</param-name>
<param-value>40</param-value>
</init-param>
<init-param>
<description>字体颜色 合法值 r,g,b black white blue</description>
<param-name>kaptcha.textproducer.font.color</param-name>
<param-value>blue</param-value>
</init-param>
<init-param>
<description>文字间隔 </description>
<param-name>kaptcha.textproducer.char.space</param-name>
<param-value>100</param-value>
</init-param>

    <init-param>
        <description>干扰实现类</description>
        <param-name>kaptcha.noise.impl</param-name>
        <param-value>
            com.google.code.kaptcha.impl.DefaultNoise
        </param-value>
    </init-param>
    <init-param>
        <description>干扰颜色</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.FishEyeGimpy
        </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>/captcha.jpg</url-pattern>
</servlet-mapping>

</web-app>

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" };

    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;
}

}

打开App,阅读手记
3人推荐
发表评论
随时随地看视频慕课网APP

热门评论

你好,我想问下Kaptcha只能用HttpServlet来实现吗,有别的方式来接收和传递吗

查看全部评论