chenjiahe
2021-11-30 a51ba771e45114bbbc1654904f42df7995d5103a
提交 | 用户 | age
6c1bfc 1 package com.platform.verification;
C 2
3
a51ba7 4 import com.alibaba.fastjson.JSONObject;
C 5 import com.hx.redis.RedisUtil;
ad5086 6 import com.hx.util.DateUtil;
6c1bfc 7 import com.hx.util.StringUtils;
C 8 import com.hx.util.rsa.RSAUtil;
a51ba7 9 import com.platform.constants.LoginConstant;
C 10 import com.platform.entity.ThirtApplication;
6c1bfc 11 import com.platform.resultTool.PlatformCode;
C 12 import com.platform.resultTool.PlatformResult;
ad5086 13
a51ba7 14 import javax.servlet.http.HttpServletRequest;
ad5086 15 import java.text.SimpleDateFormat;
C 16 import java.util.Date;
6c1bfc 17
C 18 /**平台校验方法
19  * @author CJH
20  * @date 2021-10-20
21  */
22 public class VerificationTool {
23
24     /**校验方法请求
a51ba7 25      * @param appId 用户的APPID `
6c1bfc 26      * @param sign 用户签名
a51ba7 27      * @param thirtApplication ThirtApplication
6c1bfc 28      * @return
C 29      */
a51ba7 30     public static PlatformResult verification(String appId, String sign, ThirtApplication thirtApplication,HttpServletRequest request){
6c1bfc 31
C 32         if (StringUtils.isEmpty(sign)) {
7842d6 33             return PlatformResult.failure(PlatformCode.ERROR_SIGN, "签名错误01");
6c1bfc 34         }
C 35         if (StringUtils.isEmpty(appId)) {
670a05 36             return PlatformResult.failure(PlatformCode.ERROR_APPIS, "APPID错误");
6c1bfc 37         }
C 38
39         //校验appid是否存在
a51ba7 40         if(thirtApplication == null){
670a05 41             return PlatformResult.failure(PlatformCode.ERROR_APPIS, "APPID无效");
a51ba7 42         }
C 43
44
45         if(StringUtils.isEmpty(thirtApplication.getPrivateKey())){
46             return PlatformResult.failure(PlatformCode.ERROR_SIGN, "签名错误,APPID无效");
6c1bfc 47         }
C 48
49         //解密RSA
50         String decrypt = null;
51         try{
a51ba7 52             decrypt = RSAUtil.privateDecrypt(sign, thirtApplication.getPrivateKey());
6c1bfc 53         }catch (Exception e){
C 54
55         }
56         if(StringUtils.isEmpty(decrypt)){
7842d6 57             return PlatformResult.failure(PlatformCode.ERROR_SIGN, "签名错误02");
6c1bfc 58         }
C 59
60         //decrypt格式:appId_时间戳_随机数(尽量少)
61         String[] decrypts = decrypt.split("_");
62         if(decrypts.length != 3){
7842d6 63             return PlatformResult.failure(PlatformCode.ERROR_SIGN, "签名错误03");
6c1bfc 64         }
C 65
66         if (!appId.equals(decrypts[0])) {
7842d6 67             return PlatformResult.failure(PlatformCode.ERROR_SIGN, "签名错误04");
6c1bfc 68         }
ad5086 69
C 70         Date date  = null;
71         try{
72             //判断接口时间
73             SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
74             String sd = sdf.format(new Date(Long.parseLong(String.valueOf(decrypts[1]))));      // 时间戳转换成时间
75             date = DateUtil.parseString(sd,"yyyy-MM-dd HH:mm:ss");
76         }catch (Exception e){
7842d6 77             return PlatformResult.failure(PlatformCode.ERROR_SIGN, "签名错误05");
ad5086 78         }
C 79
ce7f46 80         //接口有效5秒钟
C 81         if(System.currentTimeMillis()-date.getTime() > 5000){
670a05 82             return PlatformResult.failure(PlatformCode.ERRO_VISIT_OVERTIMR, "访问超时");
ad5086 83         }
C 84
a51ba7 85         thirtApplication.setPrivateKey(null);
C 86         request.setAttribute(LoginConstant.LOGIN_APPLY,thirtApplication);
87
6c1bfc 88         return PlatformResult.success();
C 89     }
90
ad5086 91     public static void main(String[] args) {
C 92
93         Long timeStamp = System.currentTimeMillis();  //获取当前时间戳
94         SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
95         String sd = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
96
97         Date date = DateUtil.parseString(sd,"yyyy-MM-dd HH:mm:ss");
98
99         System.out.println();
100
101         // 时间戳转换成时间
102         System.out.println("格式化结果:" + sd);
103
104     }
105
6c1bfc 106 }