本文实例为大家分享了springboot集成kaptcha验证码的具体代码,供大家参考,具体内容如下
1.kaptcha相关介绍
kaptcha是一个基于simplecaptcha的验证码开源项目。
2.集成方案
①pom.xml中配置依赖
?
|
1
2
3
4
5
6
|
<!-- 验证码-->
<dependency>
<groupid>com.github.penggle</groupid>
<artifactid>kaptcha</artifactid>
<version>2.3.2</version>
</dependency>
|
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@configuration
public class kaptchaconfig {
@bean(name="captchaproducer")
public defaultkaptcha getkaptchabean(){
defaultkaptcha defaultkaptcha=new defaultkaptcha();
properties properties=new properties();
properties.setproperty("kaptcha.border", "yes");
properties.setproperty("kaptcha.border.color", "105,179,90");
properties.setproperty("kaptcha.textproducer.font.color", "blue");
properties.setproperty("kaptcha.image.width", "125");
properties.setproperty("kaptcha.image.height", "45");
properties.setproperty("kaptcha.session.key", "code");
properties.setproperty("kaptcha.textproducer.char.length", "4");
properties.setproperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
config config=new config(properties);
defaultkaptcha.setconfig(config);
return defaultkaptcha;
}
}
|
或者
在resources下创建mykaptcher.xml文件
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="captchaproducer" class="com.google.code.kaptcha.impl.defaultkaptcha">
<property name="config">
<bean class="com.google.code.kaptcha.util.config">
<constructor-arg type="java.util.properties">
<props>
<prop key = "kaptcha.border ">yes</prop>
<prop key="kaptcha.border.color">105,179,90</prop>
<prop key="kaptcha.textproducer.font.color">blue</prop>
<prop key="kaptcha.image.width">100</prop>
<prop key="kaptcha.image.height">50</prop>
<prop key="kaptcha.textproducer.font.size">27</prop>
<prop key="kaptcha.session.key">code</prop>
<prop key="kaptcha.textproducer.char.length">4</prop>
<prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑</prop>
<prop key="kaptcha.textproducer.char.string">23456789abcefghjkmnopqrstuvwxyz</prop>
<prop key="kaptcha.obscurificator.impl">com.google.code.kaptcha.impl.waterripple</prop>
<prop key="kaptcha.noise.color">black</prop>
<prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.nonoise</prop>
<!--<prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.defaultnoise</prop>-->
<prop key="kaptcha.background.clear.from">185,56,213</prop>
<prop key="kaptcha.background.clear.to">white</prop>
<prop key="kaptcha.textproducer.char.space">3</prop>
</props>
</constructor-arg>
</bean>
</property>
</bean>
</beans>
|
然后在启动类application中加载配置
?
|
1
2
3
4
5
6
7
8
9
10
11
12
|
@enabletransactionmanagement// 启动注解事务管理,等同于xml配置方式的 <tx:annotation-driven />
@springbootapplication
@enablescheduling//启动注解定时任务
@mapperscan(basepackages = "com.shawn.mapper")
@importresource(locations={"classpath:mykaptcha.xml"})
public class application extends springbootservletinitializer {
public static void main(string[] args) throws exception {
springapplication.run(application.class, args);
}
}
|
两种配置方式在springboot中均可;
③kaptchacontroller
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
@commonslog
@controller
public class kaptchacontroller extends basecontroller {
@autowired
private producer captchaproducer;
@getmapping("/getkaptchaimage")
public void getkaptchaimage() throws exception {
response.setdateheader("expires", 0);
// set standard http/1.1 no-cache headers.
response.setheader("cache-control", "no-store, no-cache, must-revalidate");
// set ie extended http/1.1 no-cache headers (use addheader).
response.addheader("cache-control", "post-check=0, pre-check=0");
// set standard http/1.0 no-cache header.
response.setheader("pragma", "no-cache");
// return a jpeg
response.setcontenttype("image/jpeg");
// create the text for the image
string captext = captchaproducer.createtext();
// store the text in the session
//request.getsession().setattribute(constants.kaptcha_session_key, captext);
//将验证码存到session
session.setattribute(constants.kaptcha_session_key, captext);
log.info(captext);
// create the image with the text
bufferedimage bi = captchaproducer.createimage(captext);
servletoutputstream out = response.getoutputstream();
// write the data out
imageio.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
}
}
|
3.测试效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://blog.csdn.net/zhangxing52077/article/details/75270259
相关文章
猜你喜欢
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 97
-
2025-05-29 66
-
2025-05-29 95
-
2025-06-04 99
-
Debian 8或Debian 9(64 位)安装 .NET Core
2025-05-29 109
热门评论


