liurunyu
2024-12-13 74a3e4680487ad5184bea49d729356053d7fee35
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.dy.pipIrrApp.captcha;
 
import com.dy.common.multiDataSource.DataSourceContext;
import com.dy.common.webUtil.BaseResponse;
import com.dy.common.webUtil.BaseResponseUtils;
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 BaseResponse<CaptchaDomain> getCaptcha(HttpServletRequest request, @RequestParam(value = "type", required = false, defaultValue = "char") String type) {
        // 配置数据源
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String wxDataSourceName = httpRequest.getHeader("tag");
        if(wxDataSourceName != null && wxDataSourceName.trim().length() > 0){
            log.info("微信开发,设置数据源名称为:" + wxDataSourceName);
            //把组织单位标签作为数据源名称
            DataSourceContext.set(wxDataSourceName);
        } else {
            log.info("用户未选择数据源");
        }
 
        // 生成验证码实体
        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 BaseResponseUtils.buildSuccess(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();
            }
        }
    }
 
}