前面幾篇文章一直都在說微信公眾平臺的開發準備工作,那么從這篇開始我們就將正式的進入java微信公眾平臺開發的整個流程,那么這篇我們開始聊聊如何將我們的服務端和微信公眾平臺對接!
(一)接入流程解析
在我們的開發過程中無論如何最好的參考工具當然是我們的官方文檔了:http://mp.weixin.qq.com/wiki/8/f9a0b8382e0b77d87b3bcc1ce6fbc104.html
通過上面我們可以看出其中接入微信公眾平臺開發,開發者需要按照如下步驟完成:
- 填寫服務器配置
- 驗證服務器地址的有效性
- 依據接口文檔實現業務邏輯
按照上面的邏輯可能是填寫服務器配置信息是在第一步,但是我們在真實的開發過程中往往都是先做第二步【編寫代碼實現驗證服務器地址的有效性】,因為沒有第二步的完成第一步的配置是不能達到任何效果的!
(二)驗證服務器有效性代碼編寫
按照開發文檔我們知道我們的應用服務器需要接受微信服務器的get請求,其中包含四個參數(signature、timestamp、nonce、echostr)然后通過校驗方式校驗服務器的可靠性,校驗方式如下:
- 將token、timestamp、nonce三個參數進行字典序排序
- 將三個參數字符串拼接成一個字符串進行sha1加密
- 開發者獲得加密后的字符串可與signature對比,標識該請求來源于微信
①我在這里寫了一個工具類去實現其中的前兩步,將三個參數排序并返回sha1加密后的字符串,代碼如下:
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
|
package com.cuiyongzhi.wechat.util; import java.security.messagedigest; import java.security.nosuchalgorithmexception; import java.util.arrays; /** * classname: signutil * @description: 請求校驗工具類 * @author dapengniao * @date 2016年3月4日 下午6:25:41 */ public class signutil { // 與接口配置信息中的token要一致 private static string token = "dapengniaowechat" ; /** * 驗證簽名 * @param signature * @param timestamp * @param nonce * @return */ public static boolean checksignature(string signature, string timestamp, string nonce) { string[] arr = new string[] { token, timestamp, nonce }; // 將token、timestamp、nonce三個參數進行字典序排序 arrays.sort(arr); stringbuilder content = new stringbuilder(); for ( int i = 0 ; i < arr.length; i++) { content.append(arr[i]); } messagedigest md = null ; string tmpstr = null ; try { md = messagedigest.getinstance( "sha-1" ); // 將三個參數字符串拼接成一個字符串進行sha1加密 byte [] digest = md.digest(content.tostring().getbytes()); tmpstr = bytetostr(digest); } catch (nosuchalgorithmexception e) { e.printstacktrace(); } content = null ; // 將sha1加密后的字符串可與signature對比,標識該請求來源于微信 return tmpstr != null ? tmpstr.equals(signature.touppercase()) : false ; } /** * 將字節數組轉換為十六進制字符串 * @param bytearray * @return */ private static string bytetostr( byte [] bytearray) { string strdigest = "" ; for ( int i = 0 ; i < bytearray.length; i++) { strdigest += bytetohexstr(bytearray[i]); } return strdigest; } /** * 將字節轉換為十六進制字符串 * @param mbyte * @return */ private static string bytetohexstr( byte mbyte) { char [] digit = { '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' }; char [] temparr = new char [ 2 ]; temparr[ 0 ] = digit[(mbyte >>> 4 ) & 0x0f ]; temparr[ 1 ] = digit[mbyte & 0x0f ]; string s = new string(temparr); return s; } } |
②將我們的工具類應用到我們的服務器驗證過程中,這里我新建一個controller為wechatsecurity,實現同一個get用于接收參數和返回驗證參數,簡單代碼如下:
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
|
package com.cuiyongzhi.wechat.controller; import java.io.printwriter; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.apache.log4j.logger; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.requestparam; import com.cuiyongzhi.wechat.util.signutil; @controller @requestmapping ( "/wechat" ) public class wechatsecurity { private static logger logger = logger.getlogger(wechatsecurity. class ); /** * * @description: 用于接收get參數,返回驗證參數 * @param @param request * @param @param response * @param @param signature * @param @param timestamp * @param @param nonce * @param @param echostr * @author dapengniao * @date 2016年3月4日 下午6:20:00 */ @requestmapping (value = "security" , method = requestmethod.get) public void doget( httpservletrequest request, httpservletresponse response, @requestparam (value = "signature" , required = true ) string signature, @requestparam (value = "timestamp" , required = true ) string timestamp, @requestparam (value = "nonce" , required = true ) string nonce, @requestparam (value = "echostr" , required = true ) string echostr) { try { if (signutil.checksignature(signature, timestamp, nonce)) { printwriter out = response.getwriter(); out.print(echostr); out.close(); } else { logger.info( "這里存在非法請求!" ); } } catch (exception e) { logger.error(e, e); } } @requestmapping (value = "security" , method = requestmethod.post) // post方法用于接收微信服務端消息 public void dopost() { system.out.println( "這是post方法!" ); } } |
那么到這里我們的服務器驗證的代碼就基本完成了,下面我們就進入驗證過程!
(三)服務器驗證
這里我用來驗證的是我的個人公眾號【崔用志】,如果大家有興趣可以搜索看到的,通過微博認證的一個私人號,當然有想法在這里我們也是可以一起交流的,驗證方法如下圖:
點擊【提交】成功之后如下圖所示:
點擊圖中【啟用】即可,那么到這里我們的服務器接入配置就完成了,【下一篇我們將講述如何接收消息并進行消息處理】,感謝你的翻閱,如有疑問可以留言討論!
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。