zhubaomin
2024-09-11 660e9a56d2ed2ed794de90c2fcf74a0bf4161402
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
120
121
122
123
124
125
126
127
128
129
package com.dy.pipIrrApp.captcha;
 
import com.dy.pipIrrApp.captcha.dto.CaptchaDomain;
import com.dy.pipIrrGlobal.pojoBa.BaCaptcha;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.imageio.ImageIO;
import java.io.IOException;
import java.util.Calendar;
 
/**
 * @author ZhuBaoMin
 * @date 2024-09-11 13:45
 * @LastEditTime 2024-09-11 13:45
 * @Description
 */
 
@Slf4j
@RestController
@RequestMapping(path="captcha")
public class CaptchaCtrl {
    @Autowired
    private CaptchaSV captchaSV;
 
    /**
     * 获取验证码
     * @param type,图片类型:char-文本,math-一位数算式,math2-两位数算式
     * @return
     */
    @GetMapping("/get")
    @ResponseBody
    public CaptchaDomain getCaptcha(HttpServletRequest request, @RequestParam(value = "type", required = false, defaultValue = "char") String type) {
        // 生成验证码实体
        CaptchaDomain captchaDomain = captchaSV.createGoogleCaptcha(type);
        if (null != captchaDomain) {
 
            // 验证码保存至redis
            // redisUtils.set(captchaDomain.getToken(), captchaDomain.getCode(), 300L);
 
            // 验证码保存至session
            //HttpSession session = (HttpSession) request.getSession();
            //session.setAttribute(captchaDomain.getToken(), captchaDomain.getCode());
 
            /**
             * 验证保存到数据库
             * 获取当前时间戳并延后3分钟
             */
            Long timestamp = System.currentTimeMillis();
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(timestamp);
            calendar.add(Calendar.SECOND, 180);
 
            BaCaptcha baCaptcha = new BaCaptcha();
            baCaptcha.setToken(captchaDomain.getToken());
            baCaptcha.setCode(captchaDomain.getCode());
            baCaptcha.setExpiration(calendar.getTimeInMillis());
            Long rec = captchaSV.addCaptcha(baCaptcha);
            System.out.println("token: " + captchaDomain.getToken() + "; code: " + captchaDomain.getCode());
 
            // 无用信息设空
            captchaDomain.setText(null);
            captchaDomain.setCode(null);
            // 返回前端信息
            return captchaDomain;
        } else {
            return null;
        }
    }
 
    /**
     * 直接输出图片
     * @param type,图片类型:char-文本,math-一位数算式,math2-两位数算式
     */
    @GetMapping("/get/image")
    public void getCaptchaImage(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "type", required = false, defaultValue = "char") String type) {
        CaptchaDomain captchaDomain = null;
 
        // 生成谷歌验证码实体
        captchaDomain = captchaSV.createGoogleCaptcha(type);
 
        // 验证码保存至redis
        // redisUtils.set(captchaDomain.getToken(), captchaDomain.getCode(), 300L);
 
        // 验证码保存至session
        //HttpSession session = (HttpSession) request.getSession();
        //session.setAttribute(captchaDomain.getToken(), captchaDomain.getCode());
 
        /**
         * 验证保存到数据库
         * 获取当前时间戳并延后3分钟
         */
        Long timestamp = System.currentTimeMillis();
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timestamp);
        calendar.add(Calendar.SECOND, 180);
 
        BaCaptcha baCaptcha = new BaCaptcha();
        baCaptcha.setToken(captchaDomain.getToken());
        baCaptcha.setCode(captchaDomain.getCode());
        baCaptcha.setExpiration(calendar.getTimeInMillis());
        Long rec = captchaSV.addCaptcha(baCaptcha);
        System.out.println("token: " + captchaDomain.getToken() + "; code: " + captchaDomain.getCode());
 
        // 以文件流的形式,输出验证码图片
        ServletOutputStream out = null;
        try {
            response.setContentType("image/jpeg");
            out = response.getOutputStream();
            ImageIO.write(captchaDomain.getImage(), "jpg", out);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
}