Kaptcha

Jul 12, 2018


简介

可以用来生成验证码
官方效果如下:
水纹效果:
水纹
水纹
鱼眼效果(有无干扰线看不出来😓):
鱼眼
鱼眼
阴影效果:
阴影
阴影

安装

<!-- https://mvnrepository.com/artifact/com.google.code.kaptcha/kaptcha -->
<dependency>
    <groupId>com.google.code.kaptcha</groupId>
    <artifactId>kaptcha</artifactId>
    <version>2.3</version>
</dependency>

jar包下载见这里
源码下载见这里

属性列表

属性 描述 默认值
kaptcha.border 图片边框:yes , no yes
kaptcha.border.color 边框颜色: r,g,b 或者 white,black,blue black
kaptcha.border.thickness 边框厚度:> 0 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 文本集合,验证码可选值 abcde2345678gfynmnpwx
kaptcha.textproducer.char.length 验证码长度 5
kaptcha.textproducer.font.names 字体 Arial, Courier
kaptcha.textproducer.font.size 字体大小 40
kaptcha.textproducer.font.color 字体颜色: r,g,b 或者 white,black,blue black
kaptcha.textproducer.char.space 文字间隔 2
kaptcha.noise.impl 干扰实现类 com.google.code.kaptcha.impl.DefaultNoise
kaptcha.noise.color 干扰颜色: r,g,b 或者 white,black,blue. black
kaptcha.obscurificator.impl 图片样式:
水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy
com.google.code.kaptcha.impl.WaterRipple
kaptcha.background.impl 背景实现类 com.google.code.kaptcha.impl.DefaultBackground
kaptcha.background.clear.from 背景颜色渐变,开始颜色 light grey
kaptcha.background.clear.to 背景颜色渐变,结束颜色 white
kaptcha.word.impl 文字渲染器 com.google.code.kaptcha.text.impl.DefaultWordRenderer
kaptcha.session.key session key KAPTCHA_SESSION_KEY
kaptcha.session.date session date KAPTCHA_SESSION_DATE

特殊配置

#去除干扰线
<prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.NoNoise</prop>
#字体
<prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑</prop>

存储位置

使用默认的com.google.code.kaptcha.servlet.KaptchaServlet产生的验证码值存储于 session 中,在某些环境下是不适用的,比如不使用 session 同步的 Tomcat 集群
可基于源码修改存储位置满足需求

public class KaptchaServlet extends HttpServlet implements Servlet {
    private Properties props = new Properties();
    private Producer kaptchaProducer = null;
    private String sessionKeyValue = null;

    public KaptchaServlet() {
    }

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

    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();
        # 将 capText 的存储位置修改,比如存入 Redis
        req.getSession().setAttribute(this.sessionKeyValue, capText);
        BufferedImage bi = this.kaptchaProducer.createImage(capText);
        ServletOutputStream out = resp.getOutputStream();
        ImageIO.write(bi, "jpg", out);

        try {
            out.flush();
        } finally {
            out.close();
        }

    }
}