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
package com.dy.pipIrrApp.captcha;
 
import com.dy.pipIrrApp.captcha.dto.CaptchaDomain;
import com.dy.pipIrrGlobal.daoBa.BaCaptchaMapper;
import com.dy.pipIrrGlobal.pojoBa.BaCaptcha;
import com.google.code.kaptcha.Producer;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;
import java.util.Base64;
import java.util.UUID;
 
/**
 * @author ZhuBaoMin
 * @date 2024-09-11 13:46
 * @LastEditTime 2024-09-11 13:46
 * @Description
 */
 
@Slf4j
@Service
public class CaptchaSV {
    @Autowired
    private BaCaptchaMapper baCaptchaMapper;
 
    @Resource(name = "captchaProducer")
    private Producer captchaProducer;
 
    @Resource(name = "captchaProducerMathOne")
    private Producer captchaProducerMathOne;
 
    @Resource(name = "captchaProducerMathTwo")
    private Producer captchaProducerMathTwo;
 
    private static final String TYPE_CHAR = "char";
    private static final String TYPE_MATH_ONE = "math";
    private static final String TYPE_MATH_TWO = "math2";
 
    /**
     * Kaptcha生成验证码实体
     * @param type,char - 字符(缺省) | math - 一位数算式 | math2 - 两位数算式
     * @return
     */
    public CaptchaDomain createGoogleCaptcha(String type) {
        // 定义验证码实体
        CaptchaDomain captchaDomain = new CaptchaDomain();
        // 一位数加减乘除
        if (TYPE_MATH_ONE.equals(type)) {
            // 生成文本
            String producerText = captchaProducerMathOne.createText();
            // 设置验证码字符
            captchaDomain.setText(producerText.substring(0, producerText.indexOf("@")));
            // 设置验证码答案码
            captchaDomain.setCode(producerText.substring(producerText.indexOf("@") + 1));
            // 设置验证码图片
            captchaDomain.setImage(captchaProducerMathOne.createImage(captchaDomain.getText()));
        }
        // 两位数加减乘除
        else if (TYPE_MATH_TWO.equals(type)) {
            String producerText = captchaProducerMathTwo.createText();
            captchaDomain.setText(producerText.substring(0, producerText.indexOf("@")));
            captchaDomain.setCode(producerText.substring(producerText.indexOf("@") + 1));
            captchaDomain.setImage(captchaProducerMathTwo.createImage(captchaDomain.getText()));
        }
        // 缺省情况:字符
        else {
            captchaDomain.setText(captchaProducer.createText());
            captchaDomain.setCode(captchaDomain.getText());
            captchaDomain.setImage(captchaProducer.createImage(captchaDomain.getText()));
        }
        // 生成base64
        try {
            // 定义字节数组输出流
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            // 将图像以 jpg 的形式,写到字节数组输出流中
            ImageIO.write(captchaDomain.getImage(), "jpg", outputStream);
 
            // 写入base64格式
            captchaDomain.setBase64("data:image/jpg;base64," + Base64.getEncoder().encodeToString(outputStream.toByteArray()));
            // 写入唯一Token
            captchaDomain.setToken(UUID.randomUUID().toString());
            // 返回结果
            return captchaDomain;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }
 
    /**
     * 添加图片验证记录
     * @param po
     * @return
     */
    public Long addCaptcha(BaCaptcha po) {
        baCaptchaMapper.insert(po);
        return po.getId();
    }
 
}