一、准備階段
需要准備事項:
1.一個能在公網上訪問的項目:
2.一個企業微信賬號:
去注冊:(https://work.weixin.qq.com)
3.策略文件
見:Java企業微信開發_Exception_02_java.security.InvalidKeyException: Illegal key size
此包封裝了對 msg_signature對請求進行校驗的相關操作,直接用就可以了
下載地址:http://qydev.weixin.qq.com/java.zip
二、接收消息服務器配置
2.1 接收消息服務器參數配置:
在企業微信的管理端后台,進入需要設置接收消息的目標應用,點擊“接收消息”的“設置”,進入如下頁面

- URL是企業應用接收企業微信推送請求的訪問協議和地址,支持http或https協議。
- Token可由企業任意填寫,用於生成簽名。
- EncodingAESKey用於消息體的加密,是AES密鑰的Base64編碼。
2.1.1 驗證URL有效性
當點擊“保存”提交以上信息時,企業微信將發送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一致。
2.2 下載的加解密包添加到項目中
注意要在lib中添加 commons-codec-1.9.jar

2.3 微信相關參數封裝類-WeiXinParamesUtil.java
此類集中管理微信開發中所要用到的微信的相關參數
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.4 核心servlet-CoreServlet.java
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 }
2.5 在web.xml中配置servlet
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.6 提交成功
點擊2.1步中的保存按鈕,會提示配置成功
