提交 | 用户 | 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)) { |
|
29 |
return PlatformResult.failure(PlatformCode.ERROR_SIGN, "签名错误"); |
|
30 |
} |
|
31 |
if (StringUtils.isEmpty(appId)) { |
|
32 |
return PlatformResult.failure(PlatformCode.ERROR_APPIS, "APPID错误"); |
|
33 |
} |
|
34 |
|
|
35 |
//校验appid是否存在 |
|
36 |
if(!sys_appId.equals("appId")){ |
|
37 |
return PlatformResult.failure(PlatformCode.ERROR_APPIS, "APPID错误"); |
|
38 |
} |
|
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)){ |
|
48 |
return PlatformResult.failure(PlatformCode.ERROR_SIGN, "签名错误"); |
|
49 |
} |
|
50 |
|
|
51 |
//decrypt格式:appId_时间戳_随机数(尽量少) |
|
52 |
String[] decrypts = decrypt.split("_"); |
|
53 |
if(decrypts.length != 3){ |
|
54 |
return PlatformResult.failure(PlatformCode.ERROR_SIGN, "签名错误"); |
|
55 |
} |
|
56 |
|
|
57 |
if (!appId.equals(decrypts[0])) { |
|
58 |
return PlatformResult.failure(PlatformCode.ERROR_SIGN, "签名错误"); |
|
59 |
} |
ad5086
|
60 |
|
C |
61 |
Date date = null; |
|
62 |
try{ |
|
63 |
|
|
64 |
//判断接口时间 |
|
65 |
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
66 |
String sd = sdf.format(new Date(Long.parseLong(String.valueOf(decrypts[1])))); // 时间戳转换成时间 |
|
67 |
date = DateUtil.parseString(sd,"yyyy-MM-dd HH:mm:ss"); |
|
68 |
}catch (Exception e){ |
|
69 |
return PlatformResult.failure(PlatformCode.ERROR_SIGN, "签名错误"); |
|
70 |
} |
|
71 |
|
|
72 |
//接口有效2秒钟 |
|
73 |
if(System.currentTimeMillis()-date.getTime() > 2000){ |
|
74 |
return PlatformResult.failure(PlatformCode.ERROR_INVALID_VISIT, "访问无效"); |
|
75 |
} |
|
76 |
|
6c1bfc
|
77 |
return PlatformResult.success(); |
C |
78 |
} |
|
79 |
|
ad5086
|
80 |
public static void main(String[] args) { |
C |
81 |
|
|
82 |
Long timeStamp = System.currentTimeMillis(); //获取当前时间戳 |
|
83 |
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
84 |
String sd = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp)))); |
|
85 |
|
|
86 |
Date date = DateUtil.parseString(sd,"yyyy-MM-dd HH:mm:ss"); |
|
87 |
|
|
88 |
System.out.println(); |
|
89 |
|
|
90 |
// 时间戳转换成时间 |
|
91 |
System.out.println("格式化结果:" + sd); |
|
92 |
|
|
93 |
} |
|
94 |
|
6c1bfc
|
95 |
} |