chenjiahe
2022-08-11 c1cf02563eb667944181b2e4638d36e6dc7f2354
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
package com.platform.verification;
 
import com.hx.util.DateUtil;
import com.hx.util.StringUtils;
import com.hx.util.rsa.RSAUtil;
import com.platform.constants.LoginConstant;
import com.platform.entity.ThirtApplication;
import com.platform.resultTool.PlatformCode;
import com.platform.resultTool.PlatformResult;
 
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
 
/**平台校验方法
 * @author CJH
 * @date 2021-10-20
 */
public class VerificationTool {
 
    /**校验方法请求
     * @param appId 用户的APPID `
     * @param sign 用户签名
     * @param thirtApplication ThirtApplication
     * @return
     */
    public static PlatformResult verification(String appId, String sign, ThirtApplication thirtApplication,HttpServletRequest request){
 
        if (StringUtils.isEmpty(sign)) {
            return PlatformResult.failure(PlatformCode.ERROR_SIGN, "签名错误01");
        }
        if (StringUtils.isEmpty(appId)) {
            return PlatformResult.failure(PlatformCode.ERROR_APPIS, "APPID错误");
        }
 
        //校验appid是否存在
        if(thirtApplication == null){
            return PlatformResult.failure(PlatformCode.ERROR_APPIS, "APPID无效");
        }
 
        if(StringUtils.isEmpty(thirtApplication.getPrivateKey())){
            return PlatformResult.failure(PlatformCode.ERROR_SIGN, "签名错误,APPID无效");
        }
 
        //解密RSA
        String decrypt = null;
        try{
            decrypt = RSAUtil.privateDecrypt(sign, thirtApplication.getPrivateKey());
        }catch (Exception e){
 
        }
        if(StringUtils.isEmpty(decrypt)){
            return PlatformResult.failure(PlatformCode.ERROR_SIGN, "签名错误02");
        }
 
        //decrypt格式:appId_时间戳_随机数(尽量少)
        String[] decrypts = decrypt.split("_");
        if(decrypts.length != 3){
            return PlatformResult.failure(PlatformCode.ERROR_SIGN, "签名错误03");
        }
 
        if (!appId.equals(decrypts[0])) {
            return PlatformResult.failure(PlatformCode.ERROR_SIGN, "签名错误04");
        }
 
        Date date  = null;
        try{
            //判断接口时间
            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String sd = sdf.format(new Date(Long.parseLong(String.valueOf(decrypts[1]))));      // 时间戳转换成时间
            date = DateUtil.parseString(sd,"yyyy-MM-dd HH:mm:ss");
        }catch (Exception e){
            return PlatformResult.failure(PlatformCode.ERROR_SIGN, "签名错误05");
        }
 
        //接口有效5秒钟
       /* if(System.currentTimeMillis()-date.getTime() > 10000){
            return PlatformResult.failure(PlatformCode.ERRO_VISIT_OVERTIMR, "访问超时");
        }*/
 
        thirtApplication.setPrivateKey(null);
        request.getSession().setAttribute(LoginConstant.LOGIN_APPLY,thirtApplication);
 
        return PlatformResult.success();
    }
 
    public static void main(String[] args) {
 
        Long timeStamp = System.currentTimeMillis();  //获取当前时间戳
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
 
        Date date = DateUtil.parseString(sd,"yyyy-MM-dd HH:mm:ss");
 
        System.out.println();
 
        // 时间戳转换成时间
        System.out.println("格式化结果:" + sd);
 
    }
 
}