MainController.java
3.8 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.tianbo.warehouse.controller;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import com.tianbo.warehouse.model.USERS;
import com.tianbo.warehouse.service.UserService;
import com.tianbo.warehouse.util.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import java.util.Properties;
@Slf4j
@RestController
public class MainController {
@Autowired
private DefaultKaptcha captchaProducer;
@Autowired
private UserService userService;
@Autowired
private RedisUtils redisUtils;
@GetMapping("/error")
public String error(){
return "error";
}
@GetMapping("/main")
public String main(){
return "main";
}
/**
* 生成验证码
*/
@RequestMapping(value = "/randCode")
public void getRandCode(HttpServletRequest request, HttpServletResponse response){
HttpSession session = request.getSession();
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");
// 设置返回文件类型
response.setContentType("image/jpeg");
// 获取验证码上的文字
String capText = captchaProducer.createText();
// 将验证码上的文字保存在session中
session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
//缓存存储登录验证码信息
redisUtils.set(session.getId(),capText,60);
String code = (String)session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
log.info("验证码为:"+code);
// 将文件渲染到图片上
BufferedImage bi = captchaProducer.createImage(capText);
ServletOutputStream out = null;
try {
out = response.getOutputStream();
ImageIO.write(bi, "jpeg", out);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/*声明验证码生成策略属性 Bean*/
@Bean
public DefaultKaptcha captchaProducer(){
DefaultKaptcha captchaProducer =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","red");
properties.setProperty("kaptcha.image.width","125");
properties.setProperty("kaptcha.image.height","60");
properties.setProperty("kaptcha.textproducer.font.size","36");
properties.setProperty("kaptcha.session.key","code");
properties.setProperty("kaptcha.textproducer.char.length","4");
properties.setProperty("kaptcha.textproducer.font.names","宋体,楷体,微软雅黑");
Config config=new Config(properties);
captchaProducer.setConfig(config);
return captchaProducer;
}
}