kaiyu
2021-03-18 82b5c11b7f3bb0f74c108fe2c06721968ae2b5da
screen-common/src/main/java/com/moral/util/KaptchaUtils.java
@@ -7,6 +7,7 @@
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;
@@ -18,6 +19,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
 * @ClassName KaptchaUtils
@@ -33,6 +35,9 @@
    private static DefaultKaptcha defaultKaptcha;
    private static RedisTemplate redisTemplate;
    //验证码有效期 单位:秒
    private static final int validity_time = 60;
    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
@@ -54,6 +59,7 @@
     */
    public static VerificationCode createVerificationCode() throws IOException {
        VerificationCode verificationCode = new VerificationCode();
        //生成验证码内容
        String text = defaultKaptcha.createText();
        //生成图片
@@ -67,11 +73,18 @@
        String encode = encoder.encode(bytes);
        //将验证码存入redis
        String key = UUID.randomUUID().toString();
        //redisTemplate.opsForValsue().set(key,encode);
        //redisTemplate.expire();
        return null;
        redisTemplate.opsForValue().set(key,text);
        redisTemplate.expire(key,validity_time, TimeUnit.SECONDS);
        verificationCode.setKey(key);
        verificationCode.setEncode(encode);
        return verificationCode;
    }
    public boolean verify(String 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;
    }