转自:http://www.souvc.com/?p=2673
SpringBoot–集成验证码kaptcha实现验证码功能
SpringBoot–集成验证码kaptcha实现验证码功能
配置文件详解
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
|
spring.kaptcha.properties.kaptcha.textproducer.char.string=0123456789ABCDEFGHIJKLMNPQRSTUVWXYZ
spring.kaptcha.properties.kaptcha.textproducer.char.length=4
spring.kaptcha.properties.kaptcha.border=no
spring.kaptcha.properties.kaptcha.image.width=80
spring.kaptcha.properties.kaptcha.image.height=35
spring.kaptcha.properties.kaptcha.border.color=105,179,90
spring.kaptcha.properties.kaptcha.textproducer.font.color=blue
spring.kaptcha.properties.kaptcha.textproducer.font.size=30
spring.kaptcha.properties.kaptcha.textproducer.font.names=宋体,宋体,宋体,宋体
spring.kaptcha.properties.kaptcha.obscurificator.impl=com.google.code.kaptcha.impl.WaterRipple
spring.kaptcha.properties.kaptcha.session.key=code
spring.kaptcha.properties.kaptcha.noise.color=white
spring.kaptcha.properties.kaptcha.background.clear.from=white
spring.kaptcha.properties.kaptcha.background.clear.to=white
#kaptcha.border 是否有边框 默认为true 我们可以自己设置yes,no
#kaptcha.border.color 边框颜色 默认为Color.BLACK
#kaptcha.border.thickness 边框粗细度 默认为1
#kaptcha.producer.impl 验证码生成器 默认为DefaultKaptcha
#kaptcha.textproducer.impl 验证码文本生成器 默认为DefaultTextCreator
#kaptcha.textproducer.char.string 验证码文本字符内容范围 默认为abcde2345678gfynmnpwx
#kaptcha.textproducer.char.length 验证码文本字符长度 默认为5
#kaptcha.textproducer.font.names 验证码文本字体样式 默认为newFont("Arial",1,fontSize),newFont("Courier",1,fontSize)
#kaptcha.textproducer.font.size 验证码文本字符大小 默认为40
#kaptcha.textproducer.font.color 验证码文本字符颜色 默认为Color.BLACK
#kaptcha.textproducer.char.space 验证码文本字符间距 默认为2
#kaptcha.noise.impl 验证码噪点生成对象 默认为DefaultNoise
#kaptcha.noise.color 验证码噪点颜色 默认为Color.BLACK
#kaptcha.obscurificator.impl 验证码样式引擎 默认为WaterRipple
#kaptcha.word.impl 验证码文本字符渲染 默认为DefaultWordRenderer
#kaptcha.background.impl 验证码背景生成器 默认为DefaultBackground
#kaptcha.background.clear.from 验证码背景颜色渐进 默认为Color.LIGHT_GRAY
#kaptcha.background.clear.to 验证码背景颜色渐进 默认为Color.WHITE
#kaptcha.image.width 验证码图片宽度 默认为200
#kaptcha.image.height 验证码图片高度 默认为50
|
问题:如何通过SpringBoot集成验证码kaptcha实现验证码功能?
新建一个Java的Maven项目,项目名字为:springboot-sample-kaptcha
引入依赖的Jar包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 验证码 -->
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>kaptcha</artifactId>
<version>0.0.9</version>
</dependency>
</dependencies>
|
生成验证码样式的配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@Bean
publicDefaultKaptcha captchaProducer(){
DefaultKaptcha captchaProducer=newDefaultKaptcha();
Properties properties=newProperties();
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.textproducer.font.size","45");
properties.setProperty("kaptcha.session.key","code");
properties.setProperty("kaptcha.textproducer.char.length","4");
properties.setProperty("kaptcha.textproducer.font.names","宋体,楷体,微软雅黑");
Config config=newConfig(properties);
captchaProducer.setConfig(config);
return captchaProducer;
}
|
生成验证码的控制类:
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
packagecom.souvc;
importjava.awt.image.BufferedImage;
importjavax.imageio.ImageIO;
importjavax.servlet.ServletOutputStream;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.servlet.ModelAndView;
importcom.google.code.kaptcha.Constants;
importcom.google.code.kaptcha.Producer;
@Controller
@RequestMapping("/kaptcha/*")
publicclassCaptchaController{
@Autowired
privateProducer captchaProducer;
@RequestMapping
publicModelAndView getKaptchaImage(HttpServletRequest request,HttpServletResponse response)throwsException{
HttpSession session=request.getSession();
Stringcode=(String)session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
System.out.println("******************验证码是: "+code+"******************");
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
StringcapText=captchaProducer.createText();
// store the text in the session
session.setAttribute(Constants.KAPTCHA_SESSION_KEY,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();
}
returnnull;
}
}
|
访问链接:http://127.0.0.1:8080/kaptcha/getKaptchaImage 可以进行测试。刷新浏览器重新获取验证码。
如果其他地方需要获取验证码进行判断,可以通过获取session中验证码的key值。
1
2
3
|
HttpSession session=request.getSession();
Stringcode=(String)session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
System.out.println("******************验证码是: "+code+"******************");
|
当然也可以写在配置文件中,不过得看修改的频度了。