一、本节要点
1.消息的加解密
微信加解密包 下载地址:http://qydev.weixin.qq.com/java.zip ,此包中封装好了AES加解密方法,直接调用方法即可。
其中,解密方法为:
//2.获取消息明文:对加密的请求消息进行解密获得明文 WXBizMsgCrypt wxcpt=new WXBizMsgCrypt(WeiXinParamesUtil.token,WeiXinParamesUtil.encodingAESKey,WeiXinParamesUtil.corpId); result = wxcpt.DecryptMsg(msg_signature, timestamp, nonce, postData);
加密方法为:
//6.加密 WXBizMsgCrypt wxcpt=new WXBizMsgCrypt(WeiXinParamesUtil.token,WeiXinParamesUtil.encodingAESKey,WeiXinParamesUtil.corpId); respMessage = wxcpt.EncryptMsg(respMessage, timestamp, msgType);
2.被动回复消息的流程
用户发送消息之后,微信服务器将消息传递给 第三方服务器,第三方服务器接收到消息后,再对消息做出相应的回复消息。
接收消息:需先从request请求对象的输入流中获取请求参数和已加密的请求消息,再对已加密的请求消息进行解密操作,即可获得明文。
然后就行对明文消息的业务处理了。
回复消息:封装好回复消息后,需先对回复消息进行加密,获得已已加密消息,然后再通过http请求调用被动回复消息的接口,来发送消息。
二、接收消息服务器配置
接受消息服务器配置好后,用户发送消息时,微信服务器会将消息转发到配置的接受消息服务器url上,即以POST方式转发到 CoreServlet 上。
三、接收消息实体类的封装(req)
参见官方文档的说明
3.1 消息基类——BaseMessage
package com.ray.pojo.message.req; /** * 消息基类(普通用户 -> 企业微信) * @author shirayner * */ public class BaseMessage { // 开发者微信号 private String ToUserName; // 发送方帐号(一个OpenID) private String FromUserName; // 消息创建时间 (整型) private long CreateTime; // 消息类型(text/image/location/link) private String MsgType; // 消息id,64位整型 private long MsgId; //企业应用的id,整型。可在应用的设置页面查看 private int AgentID; public String getToUserName() { return ToUserName; } public void setToUserName(String toUserName) { ToUserName = toUserName; } public String getFromUserName() { return FromUserName; } public void setFromUserName(String fromUserName) { FromUserName = fromUserName; } public long getCreateTime() { return CreateTime; } public void setCreateTime(long createTime) { CreateTime = createTime; } public String getMsgType() { return MsgType; } public void setMsgType(String msgType) { MsgType = msgType; } public long getMsgId() { return MsgId; } public void setMsgId(long msgId) { MsgId = msgId; } public int getAgentID() { return AgentID; } public void setAgentID(int agentID) { AgentID = agentID; } }