wang-hao-jie
2021-10-29 2f7cdcccdeb404833e20bc63eee5f8371fc730b6
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
package cn.exrick.xboot.base.controller.common;
 
import cn.exrick.xboot.core.common.constant.SettingConstant;
import cn.exrick.xboot.core.common.redis.RedisTemplateHelper;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import cn.hutool.http.HttpUtil;
import com.google.gson.JsonParser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.http.HttpServletResponse;
import java.security.SecureRandom;
import java.util.concurrent.TimeUnit;
 
/**
 * @author Exrickx
 */
@Api(tags = "Vaptcha验证码离线验证接口")
@RequestMapping("/xboot/common/vaptcha")
@RestController
@Slf4j
public class VaptchaController {
 
    public static final String CHAR = "0123456789abcdef";
 
    @Autowired
    private RedisTemplateHelper redisTemplate;
 
    @RequestMapping(value = "/offline", method = RequestMethod.GET)
    @ApiOperation(value = "vaptcha离线模式接口")
    public Object vaptchaOffline(String offline_action, String vid, String knock, String callback, String v,
                                 HttpServletResponse response) {
 
        response.setHeader("X-Content-Type-Options", "");
        // 获取offline_key
        String offline_key = redisTemplate.get(vid);
        if (StrUtil.isBlank(offline_key)) {
            // 校验是否进入离线模式
            String offCheck = HttpUtil.get(SettingConstant.CHANNEL_URL + vid);
            int offline_state = JsonParser.parseString(offCheck).getAsJsonObject().get("offline_state").getAsInt();
            if (offline_state == 0) {
                return "Vapthca未进入离线模式";
            } else {
                offline_key = JsonParser.parseString(offCheck).getAsJsonObject().get("offline_key").getAsString();
                redisTemplate.set(vid, offline_key, 3L, TimeUnit.MINUTES);
            }
        }
        if ("get".equals(offline_action)) {
            // 获取验证图
            String imageId = new Digester(DigestAlgorithm.MD5).digestHex(offline_key + getRandomStr());
            if (StrUtil.isBlank(knock)) {
                knock = IdUtil.simpleUUID();
            }
            redisTemplate.set(knock, imageId, 3L, TimeUnit.MINUTES);
            // 拼接并返回
            String result = callback + "({\"code\": \"" + SettingConstant.VALIDATE_SUCCESS + "\", \"imgid\": \""
                    + imageId + "\", \"knock\": \"" + knock + "\"})";
            return result;
        } else {
            String imageId = redisTemplate.get(knock);
            redisTemplate.delete(knock);
            if (StrUtil.isBlank(imageId)) {
                String result = callback + "({\"code\": \"" + SettingConstant.VALIDATE_FAIL + "\", \"msg\": \"knock过期\", \"token\": \"\"})";
                return result;
            }
            String validatekey = new Digester(DigestAlgorithm.MD5).digestHex(v + imageId);
            String offValidate = HttpUtil.get(SettingConstant.VALIDATE_URL + offline_key + "/" + validatekey);
            Boolean validateResult = JsonParser.parseString(offValidate).getAsJsonObject().get("result").getAsBoolean();
            String token, result;
            if (validateResult) {
                // 校验成功则生成token
                String uuid = IdUtil.simpleUUID();
                redisTemplate.set(knock, uuid, 3L, TimeUnit.MINUTES);
                token = SettingConstant.OFFLINE_MODE + knock + uuid;
                result = callback + "({\"code\": \"" + SettingConstant.VALIDATE_SUCCESS + "\", \"msg\": \"success\", \"token\": \"" + token + "\"})";
            } else {
                result = callback + "({\"code\": \"" + SettingConstant.VALIDATE_FAIL + "\", \"msg\": \"fail\", \"token\": \"\"})";
            }
            return result;
        }
    }
 
    public static String getRandomStr() {
 
        SecureRandom random = new SecureRandom();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            sb.append(CHAR.charAt(random.nextInt(16)));
        }
        return sb.toString();
    }
}