-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathReceiveServletAES.java
More file actions
153 lines (137 loc) · 5.56 KB
/
ReceiveServletAES.java
File metadata and controls
153 lines (137 loc) · 5.56 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package weixin.popular.example;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.qq.weixin.mp.aes.AesException;
import com.qq.weixin.mp.aes.WXBizMsgCrypt;
import weixin.popular.bean.message.EventMessage;
import weixin.popular.bean.xmlmessage.XMLMessage;
import weixin.popular.bean.xmlmessage.XMLTextMessage;
import weixin.popular.support.ExpireKey;
import weixin.popular.support.expirekey.DefaultExpireKey;
import weixin.popular.util.SignatureUtil;
import weixin.popular.util.StreamUtils;
import weixin.popular.util.XMLConverUtil;
/**
* 服务端事件消息接收 加密模式
*
* @author Yi
*/
public class ReceiveServletAES extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private String appId = ""; //appid 通过微信后台获取
private String token = "test"; //通过微信后台获取
private String encodingToken = ""; //Token(令牌) 通过微信后台获取
private String encodingAesKey = ""; //EncodingAESKey(消息加解密密钥) 通过微信后台获取
//重复通知过滤
private static ExpireKey expireKey = new DefaultExpireKey();
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletInputStream inputStream = request.getInputStream();
ServletOutputStream outputStream = response.getOutputStream();
String signature = request.getParameter("signature");
String timestamp = request.getParameter("timestamp");
String nonce = request.getParameter("nonce");
String echostr = request.getParameter("echostr");
//加密模式
String encrypt_type = request.getParameter("encrypt_type");
String msg_signature = request.getParameter("msg_signature");
WXBizMsgCrypt wxBizMsgCrypt = null;
//加密方式
boolean isAes = "aes".equals(encrypt_type);
if (isAes) {
try {
wxBizMsgCrypt = new WXBizMsgCrypt(encodingToken, encodingAesKey, appId);
} catch (AesException e) {
e.printStackTrace();
}
}
EventMessage eventMessage = null;
if (isAes) {
//若为安全模式消息推送
try {
//获取XML数据(含加密参数)
String postData = StreamUtils.copyToString(inputStream, Charset.forName("utf-8"));
//解密XML 数据
assert wxBizMsgCrypt != null;
String xmlData = wxBizMsgCrypt.decryptMsg(msg_signature, timestamp, nonce, postData);
//XML 转换为bean 对象
eventMessage = XMLConverUtil.convertToObject(EventMessage.class, xmlData);
} catch (AesException e) {
e.printStackTrace();
}
} else {
//若非安全模式消息推送 则为验证消息推送 或 明文模式消息推送
if (signature == null) {
System.out.println("The request signature is null");
return;
}
//验证请求签名
if (!signature.equals(SignatureUtil.generateEventMessageSignature(encodingToken, timestamp, nonce))) {
System.out.println("The request signature is invalid");
return;
}
//首次请求申请验证,返回echostr
if (echostr != null) {
echostr = URLDecoder.decode(echostr, "utf-8");
outputStreamWrite(outputStream, echostr);
return;
}
//明文模式消息推送
if (inputStream != null) {
//XML 转换为bean 对象
eventMessage = XMLConverUtil.convertToObject(EventMessage.class, inputStream);
}
}
String key = eventMessage.getFromUserName() + "__"
+ eventMessage.getToUserName() + "__"
+ eventMessage.getMsgId() + "__"
+ eventMessage.getCreateTime();
if (expireKey.exists(key)) {
//重复通知不作处理
return;
} else {
expireKey.add(key);
}
//创建回复
XMLMessage xmlTextMessage = new XMLTextMessage(
eventMessage.getFromUserName(),
eventMessage.getToUserName(),
"你好");
//回复
xmlTextMessage.outputStreamWrite(outputStream, wxBizMsgCrypt);
}
/**
* 数据流输出
*
* @param outputStream
* @param text
* @return
*/
private boolean outputStreamWrite(OutputStream outputStream, String text) {
try {
outputStream.write(text.getBytes("utf-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
}