- 新建springboot工程
我使用的是idea 直接新建springboot工程
选择 new 新建工程,选择Spring Initlalizr ,点击next
修改自己的Group和artifact,点击next
这里选择需要的jar包模块(路过的大佬请指教一下这个的官方名称),不用选,直接next
基本上不需要改,可以修改一下存放的位置,记得选对文件夹哦,最后点击finish
-
引入kaptcha所需jar包
1
2
3
4
5
6
7 1<dependency>
2 <groupId>com.github.penggle</groupId>
3 <artifactId>kaptcha</artifactId>
4 <version>2.3.2</version>
5</dependency>
6
7
-
将bean 引入IOC容器
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 1import com.google.code.kaptcha.impl.DefaultKaptcha;
2import com.google.code.kaptcha.util.Config;
3import org.springframework.context.annotation.Bean;
4import org.springframework.context.annotation.Configuration;
5import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
6
7import java.util.Arrays;
8import java.util.List;
9import java.util.Properties;
10
11@Configuration
12public class WebConfig implements WebMvcConfigurer {
13 /**
14 * 验证码生成相关
15 */
16 @Bean
17 public DefaultKaptcha kaptcha() {
18 Properties properties = new Properties();
19 properties.put("kaptcha.border", "no");
20 properties.put("kaptcha.border.color", "105,179,90");
21 properties.put("kaptcha.textproducer.font.color", "blue");
22 properties.put("kaptcha.image.width", "125");
23 properties.put("kaptcha.image.height", "45");
24 properties.put("kaptcha.textproducer.font.size", "45");
25 properties.put("kaptcha.session.key", "code");
26 properties.put("kaptcha.textproducer.char.length", "4");
27 properties.put("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
28 Config config = new Config(properties);
29 DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
30 defaultKaptcha.setConfig(config);
31 return defaultKaptcha;
32 }
33}
34
35
36
-
写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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80 1import com.google.code.kaptcha.Constants;
2import com.google.code.kaptcha.Producer;
3import org.springframework.beans.factory.annotation.Autowired;
4import org.springframework.stereotype.Controller;
5import org.springframework.web.bind.annotation.RequestMapping;
6
7import javax.imageio.ImageIO;
8import javax.servlet.ServletOutputStream;
9import javax.servlet.http.HttpServletRequest;
10import javax.servlet.http.HttpServletResponse;
11import javax.servlet.http.HttpSession;
12import java.awt.image.BufferedImage;
13import java.io.IOException;
14
15@Controller
16@RequestMapping("/kaptcha")
17public class KaptchaController {
18 @Autowired
19 private Producer producer;
20
21 /**
22 * 生成验证码
23 */
24 @RequestMapping("")
25 public void index(HttpServletRequest request, HttpServletResponse response) {
26 HttpSession session = request.getSession();
27
28 response.setDateHeader("Expires", 0);
29
30 // Set standard HTTP/1.1 no-cache headers.
31 response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
32
33 // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
34 response.addHeader("Cache-Control", "post-check=0, pre-check=0");
35
36 // Set standard HTTP/1.0 no-cache header.
37 response.setHeader("Pragma", "no-cache");
38
39 // return a jpeg
40 response.setContentType("image/jpeg");
41
42 // create the text for the image
43 String capText = producer.createText();
44
45 // store the text in the session
46 session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
47
48 // create the image with the text
49 BufferedImage bi = producer.createImage(capText);
50 ServletOutputStream out = null;
51 try {
52 out = response.getOutputStream();
53 } catch (IOException e) {
54 e.printStackTrace();
55 }
56
57 // write the data out
58 try {
59 ImageIO.write(bi, "jpg", out);
60 } catch (IOException e) {
61 e.printStackTrace();
62 }
63 try {
64 try {
65 out.flush();
66 } catch (IOException e) {
67 e.printStackTrace();
68 }
69 } finally {
70 try {
71 out.close();
72 } catch (IOException e) {
73 e.printStackTrace();
74 }
75 }
76 }
77}
78
79
80
- 运行工程,访问
访问127.0.0.1:8080/kaptcha
就可以看到验证码了
- 验证码使用
在html 使用
1
2
3 1<img src ='127.0.0.1:8080/kaptcha'>
2
3
查看验证码
后台通过
1
2
3 1((HttpServletRequest)Objects.requireNonNull(HttpContext.getRequest())).getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY)
2
3
获取验证码