微信公众平台的官方网址:https://mp.weixin.qq.com
一.申请第三方平台资料,详见:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318292&token=&lang=zh_CN
第三方资料详解:
https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318462&lang=zh_CN
申请第三方平台成功后等待验证,验证通过,设置或者获取相关的数据:component_appid;component_appsecret;token;encodingAEKey等信息,通过这些信息可以获取到授权页需要的数据
二.准备环境:
1.依赖导入
commons-codec
commons-codec
1.9
2.进入https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318479&lang=zh_CN下载对应的demo
二.获取微信方每十分钟推送过来的数据(通过接收微信方事件的URI获取微信方每10分钟获取的component_verify_ticket数据,同时此路径可以接收微信方的解绑,更新绑定信息等事件信息)
具体代码如下:
1.接收微信端的事件推送
@RequestMapping("/getEvent")
public void getPushEvent(HttpServletRequest request,HttpServletResponse response){
PrintWriter out = null;
try {
logger.info("微推送事件request:{}", request);
out = response.getWriter();
processAuthorizeEvent(request);
out.write("success");// 直接返回success
} catch (Exception e) {
logger.info("获取微信推送数据出现异常-----------e:{}",e);
out.write("success");// 直接返回success
}
}
2.获取并且解析微信回调过来的数据信息
/**
*获取并且解析微信回调过来的数据信息
* @param request
* @throws IOException
* @throws AesException
* @throws DocumentException
*/
private void processAuthorizeEvent(HttpServletRequest request) throws IOException, AesException, DocumentException, AesException {
logger.info("开始获取微信推送的数据------");
Map
String nonce = map.get("nonce")[0];
String timestamp = map.get("timestamp")[0];
String signature = map.get("signature")[0];
String msg_signature = map.get("msg_signature")[0];
String encrypt_type = map.get("encrypt_type")[0];
logger.info("微信回调event中获取微信回调的信息-----------------data:{}",request.getQueryString());
if (StringUtils.isBlank(msg_signature)) {
logger.info("没有获取到微信发送过来的消息----------");
return;// 微信推送给第三方开放平台的消息一定是加过密的,无消息无法解密消息
}
StringBuffer sb = new StringBuffer();
BufferedReader in = request.getReader();
String line;
while((line=in.readLine())!=null){
sb.append(line);
}
String xml = sb.toString();
logger.info("获取解密前的postdata的xml:{}",xml);
WXBizMsgCrypt componentAccount = new WXBizMsgCrypt(wechatConfigUtils.getToken(), wechatConfigUtils.getEncodingAEKey(), wechatConfigUtils.getComponentAppId());
logger.info("获取微信解码对象componentAccount:{},微信第三方平台数据token:{},EncodingAEKey:{},ComponentAppId:{}", FastJsonUtils.toJSONString(componentAccount),wechatConfigUtils.getToken(), wechatConfigUtils.getEncodingAEKey(), wechatConfigUtils.getComponentAppId());
xml = componentAccount.decryptMsg(msg_signature, timestamp, nonce, xml);
logger.info("获取微信推送的数据解密后明文xml:{}",xml);
wechatAccountService.parseAboutAccountEventDate(xml);//解析微信推送的数据
// addComponentVerifyTicketToRedis(xml);//解析微信推送的数据
}
3.解析微信方传送过来的数据
service:
//解析微信方传送过来的数据
@Override
public void parseAboutAccountEventDate(String xml) {
logger.info("解析微信推送过来的数据---------------------xml",xml);
try {
Document document = DocumentHelper.parseText(xml);
Element rootElement = document.getRootElement();
String infoType = rootElement.elementText("InfoType");
logger.info("数据类型为--------------------infoType:{}",infoType);
if(infoType != null){
if("unauthorized".equals(infoType)){
deleteAccount(rootElement);//解除绑定
}else if("updateauthorized".equals(infoType)){
updateWechatAccount(rootElement);//用户更新了绑定信息
}else if("component_verify_ticket".equals(infoType)){
saveComponentVerifyTicket(rootElement);//保存componentVerifyTicket
}else if("authorized".equals(infoType)){
creatWechatAccount(rootElement);//用户新绑定
}
}else{
logger.info("微信关于账号绑定,解绑,更新账号绑定信息,获取ticket信息出现错误------------");
}
} catch (DocumentException e) {
logger.error("处理公众号绑定出现异常----------e:{}", e);
}
}
4.保存component_verify_ticket到redis中
//保存component_verify_ticket到redis中
private void saveComponentVerifyTicket(Element rootElement) {
String componentVerifyTicket = rootElement.elementText("ComponentVerifyTicket");
logger.info("获取componentVerifyTicket:{}",componentVerifyTicket);
String expiresInStr = rootElement.elementText("expiresIn");
long expireIn = 0;
if(expiresInStr!=null){
expireIn = Long.parseLong(expiresInStr);
}
logger.info("获取expireIn:{}",expireIn);
Object componentVerifyTicket1 = redisCacheManagerTool.get("componentVerifyTicket");
if(componentVerifyTicket1!=null){
redisCacheManagerTool.set("pre_componentVerifyTicket",(String)componentVerifyTicket1);
logger.info("redis中存在componentVerifyTicket,将原来的componentVerifyTicket存放到redis中key为pre_componentVerifyTicket");
}
if(StringUtils.isNotEmpty(componentVerifyTicket)){
redisCacheManagerTool.set("componentVerifyTicket", componentVerifyTicket, expireIn);
logger.info("redis缓存存入componentVerifyTicket{}", componentVerifyTicket);
}
}