You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

326 lines
15 KiB
Java

5 years ago
package com.bsd.say.controller;
5 years ago
import com.bsd.say.util.LogUtils;
5 years ago
import com.bsd.say.util.MessageUtil;
5 years ago
import com.bsd.say.util.Xml2MapUtil;
import com.bsd.say.util.wechat.AesException;
import com.bsd.say.util.wechat.WXBizMsgCrypt;
import org.apache.commons.collections.MapUtils;
5 years ago
import org.dom4j.DocumentException;
5 years ago
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
5 years ago
import org.springframework.web.bind.annotation.*;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
5 years ago
import javax.annotation.Resource;
5 years ago
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
5 years ago
import java.io.BufferedReader;
5 years ago
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
5 years ago
import java.util.Map;
5 years ago
@RestController
@RequestMapping("wechat")
public class WechatController {
5 years ago
5 years ago
@Value("${wechat.aesKey}")
private String aesKey;
@Value("${wechat.componentToken}")
private String componentToken;
@Value("${wechat.appId}")
private String appId;
5 years ago
@Value("${wechat.componentAppId}")
private String componentAppId;
5 years ago
@Resource
private RedisTemplate redisTemplate;
Logger logger = LogUtils.getBussinessLogger();
5 years ago
5 years ago
/**
* component_verify_ticket authorized
*/
5 years ago
@PostMapping(value = "/getComponentVerifyTicket")
5 years ago
@ResponseBody
5 years ago
public void getComponentVerifyTicket(HttpServletRequest request, HttpServletResponse response) throws Exception {
5 years ago
String nonce = request.getParameter("nonce");
String timestamp = request.getParameter("timestamp");
String signature = request.getParameter("signature");
String msgSignature = request.getParameter("msg_signature");
// String postData = request.getParameter("postData");
5 years ago
logger.info("nonce: " + nonce);
logger.info("timestamp: " + timestamp);
logger.info("signature: " + signature);
logger.info("msgSignature: " + msgSignature);
// Map<String, String> map= MessageUtil.parseXml(request);
// System.out.println(map.toString());
StringBuilder sb = new StringBuilder();
BufferedReader in = request.getReader();
String line;
5 years ago
while ((line = in.readLine()) != null) {
sb.append(line);
}
String postData = sb.toString();
5 years ago
logger.info("postData: " + postData);
5 years ago
try {
//这个类是微信官网提供的解密类,需要用到消息校验Token 消息加密Key和服务平台appid
WXBizMsgCrypt pc = new WXBizMsgCrypt(componentToken,
5 years ago
aesKey, componentAppId);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
dbf.setXIncludeAware(false);
dbf.setExpandEntityReferences(false);
DocumentBuilder db = dbf.newDocumentBuilder();
StringReader sr = new StringReader(postData);
InputSource is = new InputSource(sr);
org.w3c.dom.Document document = db.parse(is);
org.w3c.dom.Element root = document.getDocumentElement();
NodeList nodelist1 = root.getElementsByTagName("Encrypt");
String encrypt = nodelist1.item(0).getTextContent();
String format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%1$s]]></Encrypt></xml>";
String fromXML = String.format(format, encrypt);
String xml = pc.decryptMsg(msgSignature, timestamp, nonce, fromXML);
5 years ago
Map<String, Object> result = Xml2MapUtil.xml2map(xml);// 将xml转为map
String componentVerifyTicket = MapUtils.getString(result, "ComponentVerifyTicket");
5 years ago
logger.info("----verify ticket--" + componentVerifyTicket);
5 years ago
// 存储平台授权票据,保存ticket
String TICKET = componentVerifyTicket;
5 years ago
redisTemplate.opsForValue().set("component_verify_ticket", TICKET);
5 years ago
} catch (Exception e) {
// log.error(e.getMessage(), e);
e.printStackTrace();
}
// WeChatUtils.responseReplyMessage(response, "success");
output(response, "success");
5 years ago
}
/**
* "文本消息"
5 years ago
*
* @param response
* @param returnvaleue
*/
5 years ago
public void output(HttpServletResponse response, String returnvaleue) {
try {
PrintWriter pw = response.getWriter();
pw.write(returnvaleue);
5 years ago
// logger.info("****************returnvaleue***************="+returnvaleue);
pw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
5 years ago
// /**
// * 处理授权事件的推送
// *
// * @param request
// * @throws IOException
// * @throws AesException
// * @throws DocumentException
// */
// public void processAuthorizeEvent(HttpServletRequest request) throws IOException, DocumentException, AesException {
// String nonce = request.getParameter("nonce");
// String timestamp = request.getParameter("timestamp");
// String signature = request.getParameter("signature");
5 years ago
// String msgSignature = request.getParameter("msg_signature");
//
// if (!StringUtils.isNotBlank(msgSignature))
5 years ago
// return;// 微信推送给第三方开放平台的消息一定是加过密的,无消息加密无法解密消息
// boolean isValid = checkSignature(COMPONENT_TOKEN, signature, timestamp, nonce);
// if (isValid) {
// StringBuilder sb = new StringBuilder();
// BufferedReader in = request.getReader();
// String line;
// while ((line = in.readLine()) != null) {
// sb.append(line);
// }
// String xml = sb.toString();
//// LogUtil.info("第三方平台全网发布-----------------------原始 Xml="+xml);
// String encodingAesKey = COMPONENT_ENCODINGAESKEY;// 第三方平台组件加密密钥
// String appId = getAuthorizerAppidFromXml(xml);// 此时加密的xml数据中ToUserName是非加密的解析xml获取即可
// //LogUtil.info("第三方平台全网发布-------------appid----------getAuthorizerAppidFromXml(xml)-----------appId="+appId);
// WXBizMsgCrypt pc = new WXBizMsgCrypt(COMPONENT_TOKEN, encodingAesKey, COMPONENT_APPID);
// xml = pc.decryptMsg(msgSignature, timestamp, nonce, xml);
//// LogUtil.info("第三方平台全网发布-----------------------解密后 Xml="+xml);
// processAuthorizationEvent(xml);
5 years ago
// }
// }
//
//
5 years ago
@RequestMapping(value = "/{appid}/callback", method = {RequestMethod.GET, RequestMethod.POST})
public void callBackEvent(HttpServletRequest request, HttpServletResponse response) throws IOException, DocumentException, AesException {
// String msgSignature = request.getParameter("msg_signature");
// //LogUtil.info("第三方平台全网发布-------------{appid}/callback-----------验证开始。。。。msg_signature="+msgSignature);
// if (!StringUtils.isNotBlank(msgSignature))
// return;// 微信推送给第三方开放平台的消息一定是加过密的,无消息加密无法解密消息
//
// StringBuilder sb = new StringBuilder();
5 years ago
// BufferedReader in = request.getReader();
// String line;
// while ((line = in.readLine()) != null) {
5 years ago
// sb.append(line);
// }
// in.close();
//
// String xml = sb.toString();
// Document doc = DocumentHelper.parseText(xml);
// Element rootElt = doc.getRootElement();
// String toUserName = rootElt.elementText("ToUserName");
//
// //微信全网测试账号
//// if (StringUtils.equalsIgnoreCase(toUserName, APPID)) {
//// LogUtil.info("全网发布接入检测消息反馈开始---------------APPID="+ APPID +"------------------------toUserName="+toUserName);
// checkWeixinAllNetworkCheck(request,response,xml);
5 years ago
}
//
//
// public void checkWeixinAllNetworkCheck(HttpServletRequest request, HttpServletResponse response,String xml) throws DocumentException, IOException, AesException{
// String nonce = request.getParameter("nonce");
// String timestamp = request.getParameter("timestamp");
// String msgSignature = request.getParameter("msg_signature");
//
// WXBizMsgCrypt pc = new WXBizMsgCrypt(componentToken, aesKey, componentAppId);
// xml = pc.decryptMsg(msgSignature, timestamp, nonce, xml);
//
// Document doc = DocumentHelper.parseText(xml);
// Element rootElt = doc.getRootElement();
// String msgType = rootElt.elementText("MsgType");
// String toUserName = rootElt.elementText("ToUserName");
// String fromUserName = rootElt.elementText("FromUserName");
//
//// LogUtil.info("---全网发布接入检测--step.1-----------msgType="+msgType+"-----------------toUserName="+toUserName+"-----------------fromUserName="+fromUserName);
//// LogUtil.info("---全网发布接入检测--step.2-----------xml="+xml);
// if("event".equals(msgType)){
//// LogUtil.info("---全网发布接入检测--step.3-----------事件消息--------");
// String event = rootElt.elementText("Event");
// replyEventMessage(request,response,event,toUserName,fromUserName);
// }else if("text".equals(msgType)){
//// LogUtil.info("---全网发布接入检测--step.3-----------文本消息--------");
// String content = rootElt.elementText("Content");
// processTextMessage(request,response,content,toUserName,fromUserName);
// }
// }
//
// public void replyEventMessage(HttpServletRequest request, HttpServletResponse response, String event, String toUserName, String fromUserName) throws DocumentException, IOException {
// String content = event + "from_callback";
//// LogUtil.info("---全网发布接入检测------step.4-------事件回复消息 content="+content + " toUserName="+toUserName+" fromUserName="+fromUserName);
// replyTextMessage(request,response,content,toUserName,fromUserName);
// }
//
//
// /**
// * 回复微信服务器"文本消息"
// * @param request
// * @param response
// * @param content
// * @param toUserName
// * @param fromUserName
// * @throws DocumentException
// * @throws IOException
// */
// public void replyTextMessage(HttpServletRequest request, HttpServletResponse response, String content, String toUserName, String fromUserName) throws DocumentException, IOException {
// Long createTime = Calendar.getInstance().getTimeInMillis() / 1000;
// StringBuffer sb = new StringBuffer();
// sb.append("<xml>");
// sb.append("<ToUserName><![CDATA["+fromUserName+"]]></ToUserName>");
// sb.append("<FromUserName><![CDATA["+toUserName+"]]></FromUserName>");
// sb.append("<CreateTime>"+createTime+"</CreateTime>");
// sb.append("<MsgType><![CDATA[text]]></MsgType>");
// sb.append("<Content><![CDATA["+content+"]]></Content>");
// sb.append("</xml>");
// String replyMsg = sb.toString();
//
// String returnvaleue = "";
// try {
// WXBizMsgCrypt pc = new WXBizMsgCrypt(componentToken, aesKey, componentAppId);
// returnvaleue = pc.encryptMsg(replyMsg, createTime.toString(), "easemob");
5 years ago
//// logger.info("------------------加密后的返回内容 returnvaleue "+returnvaleue);
// } catch (AesException e) {
// e.printStackTrace();
// }
// output(response, returnvaleue);
// }
//
// public void processTextMessage(HttpServletRequest request, HttpServletResponse response,String content,String toUserName, String fromUserName) throws IOException, DocumentException{
// if("TESTCOMPONENT_MSG_TYPE_TEXT".equals(content)){
// String returnContent = content+"_callback";
// replyTextMessage(request,response,returnContent,toUserName,fromUserName);
// }else if(StringUtils.startsWithIgnoreCase(content, "QUERY_AUTH_CODE")){
// output(response, "");
// //接下来客服API再回复一次消息
// replyApiTextMessage(request,response,content.split(":")[1],fromUserName);
// }
// }
//
// public void replyApiTextMessage(HttpServletRequest request, HttpServletResponse response, String auth_code, String fromUserName) throws DocumentException, IOException {
// String authorization_code = auth_code;
// // 得到微信授权成功的消息后,应该立刻进行处理!!相关信息只会在首次授权的时候推送过来
5 years ago
// logger.info("------step.1----使用客服消息接口回复粉丝----逻辑开始-------------------------");
5 years ago
// try {
// ApiComponentToken apiComponentToken = new ApiComponentToken();
// apiComponentToken.setComponent_appid(COMPONENT_APPID);
// apiComponentToken.setComponent_appsecret(COMPONENT_APPSECRET);
// WeixinOpenAccountEntity entity = getWeixinOpenAccount(APPID);
// apiComponentToken.setComponent_verify_ticket(entity.getTicket());
// String component_access_token = JwThirdAPI.getAccessToken(apiComponentToken);
//
5 years ago
// logger.info("------step.2----使用客服消息接口回复粉丝------- component_access_token = "+component_access_token + "---------authorization_code = "+authorization_code);
// net.sf.json.JSONObject authorizationInfoJson = JwThirdAPI.getApiQueryAuthInfo(COMPONENT_APPID, authorization_code, component_access_token);
5 years ago
// logger.info("------step.3----使用客服消息接口回复粉丝-------------- 获取authorizationInfoJson = "+authorizationInfoJson);
// net.sf.json.JSONObject infoJson = authorizationInfoJson.getJSONObject("authorization_info");
// String authorizer_access_token = infoJson.getString("authorizer_access_token");
5 years ago
//
//
// Map<String,Object> obj = new HashMap<String,Object>();
// Map<String,Object> msgMap = new HashMap<String,Object>();
5 years ago
// String msg = auth_code + "_from_api";
// msgMap.put("content", msg);
//
// obj.put("touser", fromUserName);
// obj.put("msgtype", "text");
// obj.put("text", msgMap);
// JwThirdAPI.sendMessage(obj, authorizer_access_token);
// } catch (WexinReqException e) {
5 years ago
// e.printStackTrace();
// }
//
// }
5 years ago
}