|
@@ -1,14 +1,20 @@
|
1
|
1
|
package com.community.huiju.common.sign;
|
2
|
2
|
|
|
3
|
+import com.google.common.collect.Maps;
|
|
4
|
+import lombok.extern.slf4j.Slf4j;
|
|
5
|
+import org.apache.commons.codec.digest.DigestUtils;
|
3
|
6
|
import org.apache.commons.lang3.StringUtils;
|
4
|
7
|
|
5
|
|
-import java.util.HashMap;
|
|
8
|
+import java.io.UnsupportedEncodingException;
|
|
9
|
+import java.time.LocalDateTime;
|
6
|
10
|
import java.util.Map;
|
7
|
11
|
import java.util.Set;
|
|
12
|
+import java.util.TreeMap;
|
8
|
13
|
|
9
|
14
|
/**
|
10
|
15
|
* 大苏签名校验
|
11
|
16
|
*/
|
|
17
|
+@Slf4j
|
12
|
18
|
public class DaSuSign {
|
13
|
19
|
|
14
|
20
|
/**
|
|
@@ -17,7 +23,7 @@ public class DaSuSign {
|
17
|
23
|
* @param val_2
|
18
|
24
|
* @return true 相等 false 不相等
|
19
|
25
|
*/
|
20
|
|
- public boolean isMD5Sign(String val_1, String val_2) {
|
|
26
|
+ public static boolean isMD5Sign(String val_1, String val_2) {
|
21
|
27
|
return StringUtils.equals(val_1, val_2);
|
22
|
28
|
}
|
23
|
29
|
|
|
@@ -29,23 +35,49 @@ public class DaSuSign {
|
29
|
35
|
* @param appSecret
|
30
|
36
|
* @param datetime 时间戳
|
31
|
37
|
* @param encrypt 加密方式(目前就 MD5)
|
32
|
|
- * @param communityid 小区id
|
|
38
|
+ * @param communityId 小区id
|
33
|
39
|
* @return
|
34
|
40
|
*/
|
35
|
|
- public String createSign(String appid, String appSecret, String datetime, String encrypt, String communityid) {
|
36
|
|
- return null;
|
37
|
|
- }
|
|
41
|
+ public static String createSign(String appid, String appSecret, String datetime, String encrypt, String communityId){
|
|
42
|
+ TreeMap<String ,Object> treeMap = Maps.newTreeMap();
|
|
43
|
+ treeMap.put("appid", appid);
|
|
44
|
+ treeMap.put("datetime", datetime);
|
|
45
|
+ treeMap.put("encrypt", encrypt);
|
|
46
|
+ treeMap.put("communityid", communityId);
|
38
|
47
|
|
|
48
|
+ String sign = "";
|
39
|
49
|
|
40
|
|
- public static void main(String[] args) {
|
41
|
|
- Map<String, Object> map = new HashMap<>();
|
42
|
|
- map.put("a", "1");
|
43
|
|
- map.put("d", "4");
|
44
|
|
- map.put("b", "2");
|
|
50
|
+ StringBuilder sb = new StringBuilder();
|
|
51
|
+ Set<Map.Entry<String, Object>> entries = treeMap.entrySet();
|
|
52
|
+ entries.forEach(e-> {
|
|
53
|
+ sb.append(e.getKey());
|
|
54
|
+ sb.append("=");
|
|
55
|
+ sb.append(e.getValue());
|
|
56
|
+ sb.append("&");
|
|
57
|
+ });
|
|
58
|
+
|
|
59
|
+ sign = sb.toString();
|
|
60
|
+ sign = sign.substring(0, sign.lastIndexOf("&"));
|
|
61
|
+
|
|
62
|
+ log.info("签名之前参数: {}" , sign);
|
|
63
|
+
|
|
64
|
+ try {
|
|
65
|
+ String str = new String(sign.getBytes("UTF-8"), "UTF-8");
|
|
66
|
+ sign = DigestUtils.md5Hex( str + appSecret);
|
|
67
|
+ } catch (UnsupportedEncodingException e) {
|
|
68
|
+ e.printStackTrace();
|
|
69
|
+ }
|
45
|
70
|
|
46
|
|
- Set<Map.Entry<String, Object>> entries = map.entrySet();
|
47
|
|
- entries.forEach(e -> System.out.println(e.getKey()));
|
|
71
|
+ log.info("签名: {}" , sign);
|
|
72
|
+
|
|
73
|
+ return sign;
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ public static void main(String[] args) {
|
48
|
77
|
|
|
78
|
+ Long timeMillis = System.currentTimeMillis();
|
|
79
|
+ System.out.println(timeMillis);
|
|
80
|
+ String sign = createSign("dssdw2576sd997", "fgexhd855sf4", timeMillis + "", "md5", "101");
|
49
|
81
|
}
|
50
|
82
|
|
51
|
83
|
}
|