Kaptcha(验证码图片)
1、Kapcha是什么?
Kapcha是一个工具类,用于生成验证码图片。验证码主要作用是为了防止恶意程序对项目的大量访问。
2、Kapcha的基本使用(SpringBoot整合)
maven坐标
在pom.xml中添加依赖
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
配置类
| Constant | 描述| 默认值| | --- | --- || --- | | kaptcha.border| 图片边框,合法值:yes , no | yes | | kaptcha.border.color | 边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue. | black | | 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 |
@Configuration
public class KaptchaConfig {
@Bean
public Producer kaptchaProducer(){
Properties properties=new Properties();
//图片的宽度
properties.setProperty("kaptcha.image.width","100");
//图片的高度
properties.setProperty("kaptcha.image.height","40");
//字体大小
properties.setProperty("kaptcha.textproducer.font.size","32");
//字体颜色(RGB)
properties.setProperty("kaptcha.textproducer.font.color","0,0,0");
//验证码字符的集合
properties.setProperty("kaptcha.textproducer.char.string","123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
//验证码长度(即在上面集合中随机选取几位作为验证码)
properties.setProperty("kaptcha.textproducer.char.length","4");
//图片的干扰样式
properties.setProperty("kaptcha.noise.impl","com.google.code.kaptcha.impl.NoNoise");
DefaultKaptcha Kaptcha = new DefaultKaptcha();
Config config=new Config(properties);
Kaptcha.setConfig(config);
return Kaptcha;
}
}
基本使用
//获取验证码图片
@GetMapping("/kaptcha")
public void getKaptcha(HttpServletResponse response, HttpSession session){
//生成验证码
String text = kaptchaProducer.createText();
//生成图片
BufferedImage image = kaptchaProducer.createImage(text);
//将验证码存入session
session.setAttribute("kaptcha",text);
//将图片输出给浏览器
response.setContentType("image/png");
try {
ServletOutputStream outputStream = response.getOutputStream();
ImageIO.write(image,"png",outputStream);
}catch (Exception e){
LOGGER.error("验证码获取失败:"+e.getMessage());
}
}