Java企业微信开发_01_接收消息服务器配置

时间:2022-06-06
本文章向大家介绍Java企业微信开发_01_接收消息服务器配置,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一、准备阶段

需要准备事项:

1.一个能在公网上访问的项目:

                     见:Java微信公众平台开发_01_本地服务器映射外网

2.一个企业微信账号: 

                     去注册:(https://work.weixin.qq.com

3.策略文件

                     见:Java企业微信开发_Exception_02_java.security.InvalidKeyException: Illegal key size

4.接入验证的 微信加解密包

此包封装了对 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步中的保存按钮,会提示配置成功