Java微信公眾平臺開發(fā)(1) 接入微信公眾平臺
前面幾篇文章一直都在說微信公眾平臺的開發(fā)準備工作,那么從這篇開始我們就將正式的進入JAVA微信公眾平臺開發(fā)的整個流程,那么這篇我們開始聊聊如何將我們的服務端和微信公眾平臺對接!
(一)接入流程解析
在我們的開發(fā)過程中無論如何最好的參考工具當然是我們的官方文檔了:http://mp.weixin.qq.com/wiki/8/f9a0b8382e0b77d87b3bcc1ce6fbc104.html
通過上面我們可以看出其中接入微信公眾平臺開發(fā),開發(fā)者需要按照如下步驟完成:
- 填寫服務器配置
- 驗證服務器地址的有效性
- 依據(jù)接口文檔實現(xiàn)業(yè)務邏輯
按照上面的邏輯可能是填寫服務器配置信息是在第一步,但是我們在真實的開發(fā)過程中往往都是先做第二步【編寫代碼實現(xiàn)驗證服務器地址的有效性】,因為沒有第二步的完成第一步的配置是不能達到任何效果的!
(二)驗證服務器有效性代碼編寫
按照開發(fā)文檔我們知道我們的應用服務器需要接受微信服務器的get請求,其中包含四個參數(shù)(signature、timestamp、nonce、echostr)然后通過校驗方式校驗服務器的可靠性,校驗方式如下:
- 將token、timestamp、nonce三個參數(shù)進行字典序排序
- 將三個參數(shù)字符串拼接成一個字符串進行sha1加密
- 開發(fā)者獲得加密后的字符串可與signature對比,標識該請求來源于微信
①我在這里寫了一個工具類去實現(xiàn)其中的前兩步,將三個參數(shù)排序并返回sha1加密后的字符串,代碼如下:
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三個參數(shù)進行字典序排序 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"); // 將三個參數(shù)字符串拼接成一個字符串進行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; } /** * 將字節(jié)數(shù)組轉(zhuǎn)換為十六進制字符串 * @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; } /** * 將字節(jié)轉(zhuǎn)換為十六進制字符串 * @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,實現(xiàn)同一個get用于接收參數(shù)和返回驗證參數(shù),簡單代碼如下:
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參數(shù),返回驗證參數(shù) * @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方法!"); } }
那么到這里我們的服務器驗證的代碼就基本完成了,下面我們就進入驗證過程!
(三)服務器驗證
這里我用來驗證的是我的個人公眾號【崔用志】,如果大家有興趣可以搜索看到的,通過微博認證的一個私人號,當然有想法在這里我們也是可以一起交流的,驗證方法如下圖:
點擊【提交】成功之后如下圖所示:
點擊圖中【啟用】即可,那么到這里我們的服務器接入配置就完成了,【下一篇我們將講述如何接收消息并進行消息處理】,感謝你的翻閱,如有疑問可以留言討論!
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
基于javamelody監(jiān)控springboot項目過程詳解
這篇文章主要介紹了基于javamelody監(jiān)控springboot項目過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-11-11springBoot集成redis(jedis)的實現(xiàn)示例
Redis是我們Java開發(fā)中,使用頻次非常高的一個nosql數(shù)據(jù)庫,本文主要介紹了springBoot集成redis(jedis)的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下2023-09-09springboot動態(tài)注入配置與docker設置環(huán)境變量的方法
這篇文章主要介紹了springboot動態(tài)注入配置與docker設置環(huán)境變量的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04SpringBoot實現(xiàn)全局異常的封裝和統(tǒng)一處理
在Spring Boot應用中,全局異常的處理是一個非常重要的方面,本文主要為大家詳細介紹了如何在Spring Boot中進行全局異常的封裝和統(tǒng)一處理,需要的可以參考下2023-12-12