須要準備事項:php
1.一個能在公網上訪問的項目:html
見:Java微信公衆平臺開發_01_本地服務器映射外網java
2.一個企業微信帳號: web
去註冊:(https://work.weixin.qq.com)服務器
3.策略文件微信
見:Java企業微信開發_Exception_02_java.security.InvalidKeyException: Illegal key size微信開發
此包封裝了對 msg_signature對請求進行校驗的相關操做,直接用就能夠了微信公衆平臺
下載地址:http://qydev.weixin.qq.com/java.zipdom
在企業微信的管理端後臺,進入須要設置接收消息的目標應用,點擊「接收消息」的「設置」,進入以下頁面
當點擊「保存」提交以上信息時,企業微信將發送GET請求到填寫的URL上,GET請求攜帶如下四個參數
參數 | 必須 | 說明 |
---|---|---|
msg_signature | 是 | 企業微信加密簽名,msg_signature結合了企業填寫的token、請求中的timestamp、nonce參數、加密的消息體 |
timestamp | 是 | 時間戳 |
nonce | 是 | 隨機數 |
echostr | 是 | 加密的隨機字符串,以msg_encrypt格式提供。須要解密並返回echostr明文,解密後有random、msg_len、msg、$CorpID四個字段,其中msg即爲echostr明文 |
企業經過參數msg_signature對請求進行校驗,若是確認這次GET請求來自企業微信,那麼企業應該對echostr參數解密並原樣返回echostr明文(不能加引號,不能帶bom頭,不能帶換行符),則接入驗證生效,接收消息才能開啓。
後續推送消息給企業時都會在請求URL中帶上以上參數(echostr除外),校驗方式與首次驗證URL一致。
注意要在lib中添加 commons-codec-1.9.jar
此類集中管理微信開發中所要用到的微信的相關參數
1 package com.ray.util; 2 /** 3 * 微信參數 4 * @author shirayner 5 * 6 */ 7 public class WeiXinParamesUtil { 8 //token 9 public final static String token = "ray"; 10 // encodingAESKey 11 public final static String encodingAESKey = "z2W9lyOAR1XjY8mopEmiSqib0TlBZzCFiCLp6IdS2Iv"; 12 //企業ID 13 public final static String corpId = "ww92f5da92bb24696e"; 14 //應用的憑證密鑰 15 public final static String corpsecret = "I73733veH3uCs6H_ijPvIq0skjTaOePsFF4MyCEi3Ag"; 16 17 18 19 }
2.1步點擊提交以後,CoreServlet會收到請求,並調用加解密包中的工具類 對相關請求參數進行處理,以經過參數msg_signature對請求進行校驗
1 package com.ray.servlet; 2 3 4 import java.io.IOException; 5 import java.io.PrintWriter; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import com.qq.weixin.mp.aes.AesException; 13 import com.qq.weixin.mp.aes.WXBizMsgCrypt; 14 import com.ray.util.WeiXinParamesUtil; 15 16 17 18 19 /** 20 * 核心請求處理類 21 * 22 * @author liufeng 23 * @date 2013-05-18 24 */ 25 public class CoreServlet extends HttpServlet { 26 private static final long serialVersionUID = 4440739483644821986L; 27 28 /** 29 * 確認請求來自微信服務器 30 */ 31 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 32 33 // 微信加密簽名 34 String msg_signature = request.getParameter("msg_signature"); 35 // 時間戳 36 String timestamp = request.getParameter("timestamp"); 37 // 隨機數 38 String nonce = request.getParameter("nonce"); 39 // 隨機字符串 40 String echostr = request.getParameter("echostr"); 41 42 System.out.println("request=" + request.getRequestURL()); 43 44 PrintWriter out = response.getWriter(); 45 // 經過檢驗msg_signature對請求進行校驗,若校驗成功則原樣返回echostr,表示接入成功,不然接入失敗 46 String result = null; 47 try { 48 WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(WeiXinParamesUtil.token, WeiXinParamesUtil.encodingAESKey, WeiXinParamesUtil.corpId); 49 result = wxcpt.VerifyURL(msg_signature, timestamp, nonce, echostr); 50 } catch (AesException e) { 51 e.printStackTrace(); 52 } 53 if (result == null) { 54 result = WeiXinParamesUtil.token; 55 } 56 out.print(result); 57 out.close(); 58 out = null; 59 } 60 61 /** 62 * 處理微信服務器發來的消息 63 */ 64 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 65 66 67 } 68 69 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 5 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 6 <servlet> 7 <servlet-name>coreServlet</servlet-name> 8 <servlet-class> 9 com.ray.servlet.CoreServlet 10 </servlet-class> 11 </servlet> 12 13 14 <!-- url-pattern中配置的/coreServlet用於指定該Servlet的訪問路徑 --> 15 <servlet-mapping> 16 <servlet-name>coreServlet</servlet-name> 17 <url-pattern>/coreServlet</url-pattern> 18 </servlet-mapping> 19 20 21 <welcome-file-list> 22 <welcome-file>index.jsp</welcome-file> 23 </welcome-file-list> 24 </web-app>
點擊2.1步中的保存按鈕,會提示配置成功