天天看点

Kaptcha 验证码框架使用

基于springboot 验证码框架kaptcha使用

一、统一步骤引入maven坐标

<dependency>
            <groupId>com.github.penggle</groupId>
            <artifactId>kaptcha</artifactId>
            <version>2.3.2</version>
        </dependency>
           

二、配置验证码生成规则并将框架实现类导入Spring容器

@Bean
    public DefaultKaptcha getDefaultKaptcha() {
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Properties properties = new Properties();
        // 图片边框,合法值yes,no,默认值yes
        properties.setProperty("kaptcha.border", "no");
        // 边框颜色,合法值rgb(and optional alpha)或者 white,black,blue,默认值black
        properties.setProperty("kaptcha.border.color", "105,179,90");
        // 边框厚度,合法值>0,默认值为1
        properties.setProperty("kaptcha.border.color", "2");
        // 图片宽度,默认值200
        properties.setProperty("kaptcha.image.width", "200");
        // 图片高度,默认值50
        properties.setProperty("kaptcha.image.height", "50");
        // 验证码长度,默认值为5
        properties.setProperty("kaptcha.textproducer.char.length", "4");
        // 字体,默认值Arial, Courier(如果使用中文验证码,则必须使用中文的字体,否则出现乱码)
        properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
        // 字体颜色,合法值: r,g,b 或者 white,black,blue,默认值black
        properties.setProperty("kaptcha.textproducer.font.color", "black");
        // 字体大小,默认值为40px
        properties.setProperty("kaptcha.textproducer.font.size", "40");
        // 文字间隔,默认值为2
        properties.setProperty("kaptcha.textproducer.char.space", "3");
        // 干扰 颜色,合法值: r,g,b 或者 white,black,blue,默认值black
        properties.setProperty("kaptcha.noise.color", "blue");
        // 水纹com.google.code.kaptcha.impl.WaterRipple
        // 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy
        // 阴影com.google.code.kaptcha.impl.ShadowGimpy
        properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.ShadowGimpy");
        // 背景颜色渐变,开始颜色,默认值lightGray/192,193,193
        properties.setProperty("kaptcha.background.clear.from", "255,255,255");
        // 背景颜色渐变, 结束颜色,默认值white
        properties.setProperty("kaptcha.background.clear.to", "white");
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
           

三、编写controller

@Controller
public class VerifyCodeController {

    @Autowired
    DefaultKaptcha defaultKaptcha;

    @RequestMapping("/verifyCode")
    public void verifyCode(HttpServletResponse httpServletResponse) throws IOException {
        // 获取字符串验证码
        String verifyCodeStr = defaultKaptcha.createText();
        // 通过字符串验证码生成图片验证码
        BufferedImage verifyCodeImage = defaultKaptcha.createImage(verifyCodeStr);
        // 设置响应头
        httpServletResponse.setHeader("Cache-Control", "no-store");
        httpServletResponse.setHeader("Pragma", "no-cache");
        httpServletResponse.setContentType("image/jpeg");
        ServletOutputStream ops = httpServletResponse.getOutputStream();
        // 将验证码写出浏览器
        ImageIO.write(verifyCodeImage, "jpg", ops);
    }
}
           

生成的验证码效果:

Kaptcha 验证码框架使用

四、以下是验证码生成规则配置表。

Constant 描述 默认值
kaptcha.border 图片边框,合法值:yes , no yes
kaptcha.border.color 边框颜色,合法值: r,g,b (and optional alpha) 或者 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 字体大小 40px.
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