2021与蓝度共同重构项目,服务端
liuhaonan
2022-04-25 aac1000906c788aef8a14ca865f067f2b9673126
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
package com.sandu.ximon.admin.security.authcode;
 
import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;
 
/**
 * @Author Nan
 * @Date 2022/04/25
 * @Version 1.0
 */
@RestController
@AllArgsConstructor
@RequestMapping("/authcode")
public class VerifyCodeController {
 
 
    @RequestMapping("/getImg")
    @ResponseBody
    public void getVerifiCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        /*
             1.生成验证码
             2.把验证码上的文本存在session中
             3.把验证码图片发送给客户端
             */
        ImageVerificationCode ivc = new ImageVerificationCode();     //用我们的验证码类,生成验证码类对象
        BufferedImage image = ivc.getImage();  //获取验证码
        request.getSession().setAttribute("text", ivc.getText()); //将验证码的文本存在session中
        ImageVerificationCode.output(image, response.getOutputStream());//将验证码图片响应给客户端
    }
 
    @RequestMapping("Login_authentication")
    @ResponseBody
    public String Login_authentication(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
//        String vcode = (String)map.get("session_vcode");
//        if(vcode.isEmpty()||vcode==null){
//            return "验证码不能为空";
//        }
        String session_vcode = (String) request.getSession().getAttribute("text");    //从session中获取真正的验证码
        //比较输入的验证码和真正的验证码
//        if(StrUtil.equalsIgnoreCase(session_vcode, vcode)){
//            return "true";
//        }
        return session_vcode;
    }
}