WeixinUtil.java
3.91 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.topdraw.weixin.util;
import com.topdraw.config.WeixinInfoConfig;
import com.topdraw.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@Component
@Slf4j
public class WeixinUtil {
private static WeixinInfoConfig WEIXININFOCONFIG;
@Autowired
public void setWeixinInfoConfig(WeixinInfoConfig weixinInfoConfig) {
WEIXININFOCONFIG = weixinInfoConfig;
}
public static Map<String, String> getWeixinInfoByAppid(String appid) {
if (StringUtils.isBlank(appid)) {
throw new RuntimeException("wxAppid can not be null");
}
List<Map<String, String>> list = WEIXININFOCONFIG.getList();
Optional<Map<String, String>> weixinInfoOptional = list.stream().filter(o -> o.get("appid").equals(appid)).findFirst();
if (!weixinInfoOptional.isPresent()) {
throw new RuntimeException("wxAppid error, appid is : " + appid);
}
return weixinInfoOptional.get();
}
public static Map<String, String> getWeixinInfoByIndex(Integer index) {
List<Map<String, String>> list = WEIXININFOCONFIG.getList();
if (list.size() < index + 1) {
throw new RuntimeException("wxinfo error, index out of range : {}" + index);
}
return list.get(index);
}
/**
* 使用SHA1算法对字符串数组进行加密
*
* @param strList
* @return
*/
public static String encodeUsingSHA1(String... strList) {
//将strList的值进行字典排序
Arrays.sort(strList);
StringBuilder content = new StringBuilder();
for (int i = 0; i < strList.length; i++) {
content.append(strList[i]);
}
return doEncodeUsingSHA1(content.toString());
}
/**
* SHA1实现
*
* @return sha1加密后的字符串
*/
private static String doEncodeUsingSHA1(String inStr) {
byte[] byteArray ;
try {
MessageDigest sha = MessageDigest.getInstance("SHA-1");
byteArray = sha.digest(inStr.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("no sha-1 algorithm");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("unsupported utf-8 encoding");
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < byteArray.length; i++) {
sb.append(Integer.toString((byteArray[i] & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
}
/**
* 公众号,小程序后台配置服务器,初次检验时使用
* @throws IOException
*/
public static void doGet(HttpServletRequest request, HttpServletResponse response, Map<String, String> weixinInfo) throws IOException {
log.info("doGet receive WeChat server request parameters:{}", request.getParameterMap());
String signature = request.getParameter("signature");
String timestamp = request.getParameter("timestamp");
String nonce = request.getParameter("nonce");
String echoStr = request.getParameter("echostr");
String[] arr = new String[]{weixinInfo.get("token"), timestamp, nonce};
String encrypt = WeixinUtil.encodeUsingSHA1(arr);
if (encrypt.equals(signature)) {
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(echoStr.getBytes());
outputStream.flush();
}
}
}