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