kaiyu
2021-09-13 10d4b885a9b2af2a38bb03249d7547283f948197
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
package com.moral.util;
 
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.moral.pojo.VerificationCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import sun.misc.BASE64Encoder;
 
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
 
/**
 * @ClassName KaptchaUtils
 * @Description 验证码工具类
 * @Author 陈凯裕
 * @Date 2021/3/16 15:36
 * @Version TODO
 **/
@Component
@Slf4j
public class KaptchaUtils {
 
    private static DefaultKaptcha defaultKaptcha;
 
    private static RedisTemplate redisTemplate;
 
    //验证码有效期 单位:秒
    private static final int validity_time = 60*60;
 
    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        KaptchaUtils.redisTemplate = redisTemplate;
    }
 
    @Autowired
    @Qualifier(value = "kaptchaProducer")
    public  void setDefaultKaptcha(DefaultKaptcha defaultKaptcha) {
        KaptchaUtils.defaultKaptcha = defaultKaptcha;
    }
 
    /**
     * @Description: 生成验证码
     * @Param: []
     * @return: java.lang.String
     * @Author: 陈凯裕
     * @Date: 2021/3/16
     */
 
    public static VerificationCode createVerificationCode() throws IOException {
        VerificationCode verificationCode = new VerificationCode();
        //生成验证码内容
        String text = defaultKaptcha.createText();
        //生成图片
        BufferedImage image = defaultKaptcha.createImage(text);
        //获取图片的字节数组
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(image,"jpg",outputStream);
        byte[] bytes = outputStream.toByteArray();
        //将字节数组进行编码
        BASE64Encoder encoder = new BASE64Encoder();
        String encode = encoder.encode(bytes);
        //将验证码存入redis
        String key = UUID.randomUUID().toString();
        redisTemplate.opsForValue().set(key,text);
        redisTemplate.expire(key,validity_time, TimeUnit.SECONDS);
        verificationCode.setKey(key);
        verificationCode.setEncode(encode);
        return verificationCode;
    }
    public static boolean verify(VerificationCode code) {
        String key = code.getKey();
        String inputText = code.getInputText();
        String validText = (String) redisTemplate.opsForValue().get(key);
        if(inputText.equals(validText))
            return true;
        return false;
    }
 
 
 
}