springboot集成kaptcha验证码
简介
在开发中,验证码功能是一个常见且重要的功能,kaptcha 是大名鼎鼎的谷歌公司提供的一款用于生成验证码的插件,支持高度可配置;
本章将通过一个简单的示例展示如何实现验证码功能
实现步骤
1. 在 pom.xml 配置文件中
添加如下配置:
由于国内限制了谷歌网络的访问,推荐使用下面的依赖下载
<dependency>
<groupid>com.github.penggle</groupid>
<artifactid>kaptcha</artifactid>
<version>2.3.2</version>
</dependency>2. 在系统公共配置类中添加如下代码
当然关于 kaptcha 的配置也可以添加到 application.properties 配置文件中
@configuration
public class appconfigure implements webmvcconfigurer {
/**
* 验证码配置
*/
@bean
public defaultkaptcha kaptcha() {
defaultkaptcha kaptcha = new defaultkaptcha();
properties properties = new properties();
properties.put("kaptcha.border", "yes");
properties.put("kaptcha.image.width", "100");
properties.put("kaptcha.image.height", "33");
properties.put("kaptcha.session.key", "code");
properties.put("kaptcha.border.color", "105,179,90");
properties.put("kaptcha.textproducer.font.size", "30");
properties.put("kaptcha.textproducer.char.length", "4");
properties.put("kaptcha.textproducer.font.color", "blue");
properties.put("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
kaptcha.setconfig(new config(properties));
return kaptcha;
}
}3. 在 kaptchcontroller.class 中添加提供验证码生成的方法
@controller
@requestmapping("/kaptcha")
@slf4j
public class kaptchacontroller {
@resource
private defaultkaptcha kaptcha;
/**
* 申请验证码
*/
@getmapping("/kaptcha")
public void getkaptcha(httpservletrequest request, httpservletresponse response) {
response.setdateheader("expires", 0);
response.setheader("cache-control", "no-store, no-cache, must-revalidate");
response.addheader("cache-control", "post-check=0, pre-check=0");
response.setheader("pragma", "no-cache");
response.setcontenttype("image/jpeg");
httpsession session = request.getsession();
servletoutputstream outputstream = null;
try {
//生成验证码
string kaptchatext = kaptcha.createtext();
// 将验证码保存 5 分钟
commonutils.setsession(session, properties.kaptcha.desc(), kaptchatext,
properties.expiretime_kaptcha.value());
log.info("captcha code: " + kaptchatext);
//向客户端输出
bufferedimage bufferedimage = kaptcha.createimage(kaptchatext);
outputstream = response.getoutputstream();
imageio.write(bufferedimage, "jpg", outputstream);
outputstream.flush();
} catch (ioexception e) {
throw new businessexception(errorcode.close_io_exception);
} finally {
commonutils.closeio(outputstream);
}
}
......
}4. 前端页面直接使用 img 标签引用即可
<img src="/kaptcha/kaptcha" id="kaptcha-img" title="点击刷新">
补充:kaptcha 更多配置
| 属性(常量) | 描述 | 默认值 |
|---|---|---|
| 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 |
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论