一、准备阶段
需要准备事项:
1.一个能在公网上访问的项目:
见:Java微信公众平台开发01本地服务器映射外网
2.一个企业微信账号:
3.策略文件
见:Java企业微信开发
4.接入验证的 微信加解密包
二、接收消息服务器配置
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
package com.ray.util;
/**
* 微信参数
* @author shirayner
*
*/
public class WeiXinParamesUtil {
//token
public final static String token = "ray";
// encodingAESKey
public final static String encodingAESKey = "z2W9lyOAR1XjY8mopEmiSqib0TlBZzCFiCLp6IdS2Iv";
//企业ID
public final static String corpId = "ww92f5da92bb24696e";
//应用的凭证密钥
public final static String corpsecret = "I73733veH3uCs6H_ijPvIq0skjTaOePsFF4MyCEi3Ag";
}
此类集中管理微信开发中所要用到的微信的相关参数
2.4 核心servlet-CoreServlet.java
package com.ray.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.qq.weixin.mp.aes.AesException;
import com.qq.weixin.mp.aes.WXBizMsgCrypt;
import com.ray.util.WeiXinParamesUtil;
/**
* 核心请求处理类
*
* @author liufeng
* @date 2013-05-18
*/
public class CoreServlet extends HttpServlet {
private static final long serialVersionUID = 4440739483644821986L;
/**
* 确认请求来自微信服务器
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 微信加密签名
String msg_signature = request.getParameter("msg_signature");
// 时间戳
String timestamp = request.getParameter("timestamp");
// 随机数
String nonce = request.getParameter("nonce");
// 随机字符串
String echostr = request.getParameter("echostr");
System.out.println("request=" + request.getRequestURL());
PrintWriter out = response.getWriter();
// 通过检验msg_signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败
String result = null;
try {
WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(WeiXinParamesUtil.token, WeiXinParamesUtil.encodingAESKey, WeiXinParamesUtil.corpId);
result = wxcpt.VerifyURL(msg_signature, timestamp, nonce, echostr);
} catch (AesException e) {
e.printStackTrace();
}
if (result == null) {
result = WeiXinParamesUtil.token;
}
out.print(result);
out.close();
out = null;
}
/**
* 处理微信服务器发来的消息
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
2.5 在web.xml中配置servlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>coreServlet</servlet-name>
<servlet-class>
com.ray.servlet.CoreServlet
</servlet-class>
</servlet>
<!-- url-pattern中配置的/coreServlet用于指定该Servlet的访问路径 -->
<servlet-mapping>
<servlet-name>coreServlet</servlet-name>
<url-pattern>/coreServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2.6 提交成功
点击2.1步中的保存按钮,会提示配置成功