在一个web应用中验证码是一个常见的元素。不管是防止机器人还是爬虫都有一定的作用,我们是自己编写生产验证码的工具类,也可以使用一些比较方便的验证码工具。在网上收集一些资料之后,今天给大家介绍一下kaptcha的和springboot一起使用的简单例子。
准备工作:
1.你要有一个springboot的hello world的工程,并能正常运行。
2.导入kaptcha的maven:
|
1
|
|
2
3
4
5
6
|
<!-- https://mvnrepository.com/artifact/com.github.penggle/kaptcha -->
<dependency>
<groupid>com.github.penggle</groupid>
<artifactid>kaptcha</artifactid>
<version>2.3.2</version>
</dependency>
|
开始实验:
我们有两种方式在springboot中使用kaptcha
第一种使用.xml的配置方式配置生成kaptcha的bean对象,在启动类上@importresource这个xml文件;在controller中注入其对象并使用
第二种是把kaptcha作为工程的一个类,加上@component注解在返回kaptcha的方法中加上@bean注解,再在controller中注入其对象。
第一种方法:
在resources中创建一个xxx.xml文件 如:
mykaptcha.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
|
<?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">0123456789abcefghijklmnopqrstuvwxyz</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.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>
|
在springboot启动类上引入这个文件
|
1
|
|
2
3
4
5
6
7
|
@springbootapplication
@importresource(locations={"classpath:mykaptcha.xml"})
public class application {
public static void main(string[] args) {
springapplication.run(application.class, args);
}
}
|
在controller中使用:
|
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
|
@autowired
defaultkaptcha defaultkaptcha;
......
@requestmapping("/defaultkaptcha")
public void defaultkaptcha(httpservletrequest httpservletrequest,httpservletresponse httpservletresponse) throws exception{
byte[] captchachallengeasjpeg = null;
bytearrayoutputstream jpegoutputstream = new bytearrayoutputstream();
try {
//生产验证码字符串并保存到session中
string createtext = defaultkaptcha.createtext();
httpservletrequest.getsession().setattribute("vrifycode", createtext);
//使用生产的验证码字符串返回一个bufferedimage对象并转为byte写入到byte数组中
bufferedimage challenge = defaultkaptcha.createimage(createtext);
imageio.write(challenge, "jpg", jpegoutputstream);
} catch (illegalargumentexception e) {
httpservletresponse.senderror(httpservletresponse.sc_not_found);
return;
}
//定义response输出类型为image/jpeg类型,使用response输出流输出图片的byte数组
captchachallengeasjpeg = jpegoutputstream.tobytearray();
httpservletresponse.setheader("cache-control", "no-store");
httpservletresponse.setheader("pragma", "no-cache");
httpservletresponse.setdateheader("expires", 0);
httpservletresponse.setcontenttype("image/jpeg");
servletoutputstream responseoutputstream =
httpservletresponse.getoutputstream();
responseoutputstream.write(captchachallengeasjpeg);
responseoutputstream.flush();
responseoutputstream.close();
}
|
验证的方法:
|
1
|
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@requestmapping("/imgvrifycontrollerdefaultkaptcha")
public modelandview imgvrifycontrollerdefaultkaptcha(httpservletrequest httpservletrequest,httpservletresponse httpservletresponse){
modelandview andview = new modelandview();
string captchaid = (string) httpservletrequest.getsession().getattribute("vrifycode");
string parameter = httpservletrequest.getparameter("vrifycode");
system.out.println("session vrifycode "+captchaid+" form vrifycode "+parameter);
if (!captchaid.equals(parameter)) {
andview.addobject("info", "错误的验证码");
andview.setviewname("index");
} else {
andview.addobject("info", "登录成功");
andview.setviewname("succeed");
}
return andview;
}
|
模板html:
- <!doctypehtml>
- <html>
- <headlang="en">
- <metacharset="utf-8"/>
- <title>hello</title>
- </head>
- <body>
- <h1th:text="${info}"/>
- <div>
- <!–<imgalt="这是图片"src="/img/001.png"/>–>
- <imgalt="验证码"onclick="this.src='/defaultkaptcha?d='+newdate()*1"src="/defaultkaptcha"/>
- </div>
- <formaction="imgvrifycontrollerdefaultkaptcha">
- <inputtype="text"name="vrifycode"/>
- <inputtype="submit"value="提交"></input>
- </form>
- </body>
- </html>
启动并访问:
提交:
第二中方发:
这种方法把.xml文件换成使用代码来配置:
kaptchaconfig.java:
|
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
|
import java.util.properties;
import org.springframework.context.annotation.bean;
import org.springframework.stereotype.component;
import com.google.code.kaptcha.impl.defaultkaptcha;
import com.google.code.kaptcha.util.config;
@component
public class kaptchaconfig {
@bean
public defaultkaptcha getdefaultkaptcha(){
com.google.code.kaptcha.impl.defaultkaptcha defaultkaptcha = new com.google.code.kaptcha.impl.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", "110");
properties.setproperty("kaptcha.image.height", "40");
properties.setproperty("kaptcha.textproducer.font.size", "30");
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;
}
}
|
注意要去掉启动类中引入的.xml文件,不然会有两个相同的对象,而你没有指明要注入哪一个的话启动会失败。
启动并测试:
到这里就算成功了。(也有使用jcaptcha的,只是他们最好不要再一个工程中使用,使用到了相同的类,有时候会导致异常。)
补充:对于kaptcha的配置属性大家可以找找,根据属性就可以配置了。
总结
以上所述是小编给大家介绍的springboot 集成kaptcha实现验证码功能实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!
原文链接:http://blog.csdn.net/u014104286/article/details/70515004
相关文章
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-25 83
-
2025-05-25 66
-
2025-05-26 79
-
关于安装linux redhat后无法使用yum命令安装gcc-c++问题的解决过程
2025-05-27 54 -
2025-05-29 88






