[1].[代码] java后台,微信TOKEN验证 跳至 [1] [2] [3] [4]
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
|
package
com.wechat.core.servlet;
import
java.io.IOException;
import
java.io.PrintWriter;
import
java.util.Date;
import
java.util.Map;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.dom4j.DocumentException;
import
com.wechat.model.TextMessage;
import
com.wechat.util.CheckUtil;
import
com.wechat.util.MessageUtil;
public
class
weixinServlet
extends
HttpServlet {
@Override
protected
void
doGet(HttpServletRequest req, HttpServletResponse resp)
throws
ServletException, IOException {
//获取微信传入的四个参数
String signature = req.getParameter(
"signature"
);
String timestamp = req.getParameter(
"timestamp"
);
String nonce = req.getParameter(
"nonce"
);
String echostr = req.getParameter(
"echostr"
);
PrintWriter out = resp.getWriter();
if
(CheckUtil.checkSignature(signature, timestamp, nonce)){
out.print(echostr);
}
}
@SuppressWarnings
(
"null"
)
@Override
protected
void
doPost(HttpServletRequest req, HttpServletResponse resp)
throws
ServletException, IOException {
// TODO Auto-generated method stub
req.setCharacterEncoding(
"UTF-8"
);
resp.setCharacterEncoding(
"UTF-8"
);
PrintWriter out =
null
;
try
{
Map<String,String> map = MessageUtil.xmlToMap(req);
String fromUserName = map.get(
"FromUserName"
);
String toUserName = map.get(
"ToUserName"
);
String createTime = map.get(
"CreateTime"
);
String msgType = map.get(
"MsgType"
);
String content = map.get(
"Content"
);
/*String msgId = map.get("MsgId");*/
String message =
null
;
if
(
"text"
.equals(msgType)){
TextMessage text =
new
TextMessage();
text.setFromUserName(toUserName);
text.setToUserName(fromUserName);
text.setCreateTime(Long.toString(
new
Date().getTime()));
text.setMsgType(
"text"
);
text.setContent(
"Test"
+content);
message = MessageUtil.textMessageToXml(text);
System.err.println(message);
}
out.print(message);
}
catch
(DocumentException e) {
e.printStackTrace();
}
/*finally{
out.close();
} */
}
}
|
[2].[代码] servlet的web.xml配置 跳至 [1] [2] [3] [4]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<web-app xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns=
"http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation=
"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id=
"WebApp_ID"
version=
"2.5"
>
<display-name>Wechat</display-name>
<servlet>
<servlet-name>weixinServlet</servlet-name>
<servlet-
class
>com.wechat.core.servlet.weixinServlet</servlet-
class
>
</servlet>
<servlet-mapping>
<servlet-name>weixinServlet</servlet-name>
<url-pattern>/wx.
do
</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>
default
.html</welcome-file>
<welcome-file>
default
.htm</welcome-file>
<welcome-file>
default
.jsp</welcome-file>
</welcome-file-list>
</web-app>
|
[3].[代码] CheckUtil 跳至 [1] [2] [3] [4]
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
|
package
com.wechat.util;
import
java.security.MessageDigest;
import
java.util.Arrays;
public
class
CheckUtil {
private
static
final
String Token =
"Token"
;
public
static
boolean
checkSignature(String signature,String timestamp,String nonce ){
String[] arr =
new
String[]{Token,timestamp,nonce};
//排序
Arrays.sort(arr);
//生成字符串
StringBuffer content =
new
StringBuffer();
for
(
int
i =
0
; i < arr.length; i++) {
content.append(arr[i]);
}
String temp = getSha1(content.toString());
return
temp.equals(signature);
}
//sha1加密
public
static
String getSha1(String str){
if
(str==
null
||str.length()==
0
){
return
null
;
}
char
hexDigits[]={
'0'
,
'1'
,
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'7'
,
'8'
,
'9'
,
'a'
,
'b'
,
'c'
,
'd'
,
'e'
,
'f'
};
try
{
MessageDigest mdTemp = MessageDigest.getInstance(
"SHA1"
);
mdTemp.update(str.getBytes(
"UTF-8"
));
byte
[] md = mdTemp.digest();
int
j = md.length;
char
buf[] =
new
char
[j*
2
];
int
k =
0
;
for
(
int
i =
0
; i < j; i++) {
byte
byte0 = md[i];
buf[k++] = hexDigits[byte0>>>
4
&
0xf
];
buf[k++] = hexDigits[byte0&
0xf
];
}
return
new
String(buf);
}
catch
(Exception e) {
return
null
;
}
}
}
|
[4].[代码] MessageUtil 跳至 [1] [2] [3] [4]
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
|
package
com.wechat.util;
import
java.io.IOException;
import
java.io.InputStream;
import
java.util.HashMap;
import
java.util.List;
import
java.util.Map;
import
javax.servlet.http.HttpServletRequest;
import
org.dom4j.Document;
import
org.dom4j.DocumentException;
import
org.dom4j.Element;
import
org.dom4j.io.SAXReader;
import
com.thoughtworks.xstream.XStream;
import
com.wechat.model.TextMessage;
public
class
MessageUtil {
//xml转化map集合
public
static
Map<String, String>xmlToMap(HttpServletRequest request)
throws
IOException, DocumentException{
Map<String,String> map =
new
HashMap<String, String>();
SAXReader reader =
new
SAXReader();
InputStream ins = request.getInputStream();
Document doc = reader.read(ins);
Element root = doc.getRootElement();
List<Element> list = root.elements();
for
(Element e : list) {
map.put(e.getName(), e.getText());
}
ins.close();
return
map;
}
// 将文本消息对象转化为XML
public
static
String textMessageToXml(TextMessage textMessage){
XStream xstream =
new
XStream();
xstream.alias(
"xml"
,textMessage.getClass());
return
xstream.toXML(textMessage);
}
}
|