|
|
@@ -62,10 +62,23 @@ public class WechatCalllbackController extends BaseController { |
|
|
@Autowired |
|
|
@Autowired |
|
|
private WxMpManager mpManager; |
|
|
private WxMpManager mpManager; |
|
|
|
|
|
|
|
|
@RequestMapping(value = "/notify", method = RequestMethod.POST) |
|
|
|
|
|
|
|
|
@PostMapping(value = "/notify") |
|
|
@ApiOperation("接收ticket回调") |
|
|
@ApiOperation("接收ticket回调") |
|
|
public Object receiveTicket(HttpServletRequest request) throws IOException { |
|
|
|
|
|
|
|
|
@ResponseBody |
|
|
|
|
|
public String receiveTicket(HttpServletRequest request) throws IOException { |
|
|
logger.info("[" + getIpAddr() + "]接收微信回调 /wxOpen/notify"); |
|
|
logger.info("[" + getIpAddr() + "]接收微信回调 /wxOpen/notify"); |
|
|
|
|
|
|
|
|
|
|
|
String timestamp = request.getParameter("timestamp"); |
|
|
|
|
|
String nonce = request.getParameter("nonce"); |
|
|
|
|
|
String signature = request.getParameter("signature"); |
|
|
|
|
|
String encType = request.getParameter("encrypt_type"); |
|
|
|
|
|
String msgSignature = request.getParameter("msg_signature"); |
|
|
|
|
|
|
|
|
|
|
|
if (!StringUtils.equalsIgnoreCase("aes", encType) |
|
|
|
|
|
|| !openService.getWxOpenComponentService().checkSignature(timestamp, nonce, signature)) { |
|
|
|
|
|
throw new IllegalArgumentException("非法请求,可能属于伪造的请求!"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
InputStream inStream = request.getInputStream(); |
|
|
InputStream inStream = request.getInputStream(); |
|
|
ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); |
|
|
ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); |
|
|
byte[] buffer = new byte[1024]; |
|
|
byte[] buffer = new byte[1024]; |
|
|
@@ -79,21 +92,10 @@ public class WechatCalllbackController extends BaseController { |
|
|
outSteam.close(); |
|
|
outSteam.close(); |
|
|
inStream.close(); |
|
|
inStream.close(); |
|
|
|
|
|
|
|
|
String timestamp = request.getParameter("timestamp"); |
|
|
|
|
|
String nonce = request.getParameter("nonce"); |
|
|
|
|
|
String signature = request.getParameter("signature"); |
|
|
|
|
|
String encType = request.getParameter("encrypt_type"); |
|
|
|
|
|
String msgSignature = request.getParameter("msg_signature"); |
|
|
|
|
|
|
|
|
|
|
|
logger.info("\n接收微信回调:[signature=[{}], encType=[{}], msgSignature=[{}]," |
|
|
logger.info("\n接收微信回调:[signature=[{}], encType=[{}], msgSignature=[{}]," |
|
|
+ " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ", |
|
|
+ " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ", |
|
|
signature, encType, msgSignature, timestamp, nonce, resultxml); |
|
|
signature, encType, msgSignature, timestamp, nonce, resultxml); |
|
|
|
|
|
|
|
|
if (!StringUtils.equalsIgnoreCase("aes", encType) |
|
|
|
|
|
|| !openService.getWxOpenComponentService().checkSignature(timestamp, nonce, signature)) { |
|
|
|
|
|
throw new IllegalArgumentException("非法请求,可能属于伪造的请求!"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// aes加密的消息 |
|
|
// aes加密的消息 |
|
|
WxOpenXmlMessage inMessage = WxOpenXmlMessage.fromEncryptedXml(resultxml, |
|
|
WxOpenXmlMessage inMessage = WxOpenXmlMessage.fromEncryptedXml(resultxml, |
|
|
openService.getWxOpenConfigStorage(), timestamp, nonce, msgSignature); |
|
|
openService.getWxOpenConfigStorage(), timestamp, nonce, msgSignature); |
|
|
@@ -109,8 +111,8 @@ public class WechatCalllbackController extends BaseController { |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
} else if (inMessage.getInfoType().equals(EnumWxAuthorizationInfoType.AUTHORIZED.getMessage()) || |
|
|
} else if (inMessage.getInfoType().equals(EnumWxAuthorizationInfoType.AUTHORIZED.getMessage()) || |
|
|
inMessage.getInfoType().equals(EnumWxAuthorizationInfoType.UPDATEAUTHORIZED.getMessage()) |
|
|
|
|
|
) { |
|
|
|
|
|
|
|
|
inMessage.getInfoType().equals(EnumWxAuthorizationInfoType.UPDATEAUTHORIZED.getMessage())) { |
|
|
|
|
|
// 审核通过 |
|
|
try { |
|
|
try { |
|
|
WxOpenQueryAuthResult queryAuthResult = openService.getWxOpenComponentService().getQueryAuth(inMessage.getAuthorizationCode()); |
|
|
WxOpenQueryAuthResult queryAuthResult = openService.getWxOpenComponentService().getQueryAuth(inMessage.getAuthorizationCode()); |
|
|
logger.info("getQueryAuth", queryAuthResult); |
|
|
logger.info("getQueryAuth", queryAuthResult); |
|
|
@@ -133,6 +135,7 @@ public class WechatCalllbackController extends BaseController { |
|
|
logger.error(e.getMessage()); |
|
|
logger.error(e.getMessage()); |
|
|
} |
|
|
} |
|
|
} else if (inMessage.getInfoType().equals(EnumWxAuthorizationInfoType.UNAUTHORIZED.getMessage())) { |
|
|
} else if (inMessage.getInfoType().equals(EnumWxAuthorizationInfoType.UNAUTHORIZED.getMessage())) { |
|
|
|
|
|
// 审核未通过 |
|
|
authorizerInfoService.saveOrUpdate(new WxAuthorizerInfo() { |
|
|
authorizerInfoService.saveOrUpdate(new WxAuthorizerInfo() { |
|
|
{ |
|
|
{ |
|
|
setAuthorizerAppid(inMessage.getAuthorizerAppid()); |
|
|
setAuthorizerAppid(inMessage.getAuthorizerAppid()); |
|
|
@@ -141,6 +144,7 @@ public class WechatCalllbackController extends BaseController { |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
|
String out = openService.getWxOpenComponentService().route(inMessage); |
|
|
String out = openService.getWxOpenComponentService().route(inMessage); |
|
|
this.logger.info("组装回复信息:{}", out); |
|
|
this.logger.info("组装回复信息:{}", out); |
|
|
@@ -163,6 +167,12 @@ public class WechatCalllbackController extends BaseController { |
|
|
String openid = request.getParameter("openid"); |
|
|
String openid = request.getParameter("openid"); |
|
|
String msgSignature = request.getParameter("msg_signature"); |
|
|
String msgSignature = request.getParameter("msg_signature"); |
|
|
|
|
|
|
|
|
|
|
|
// 第三方平台一定接收的是加密的消息 |
|
|
|
|
|
if (!StringUtils.equalsIgnoreCase("aes", encType) |
|
|
|
|
|
|| !openService.getWxOpenComponentService().checkSignature(timestamp, nonce, signature)) { |
|
|
|
|
|
throw new IllegalArgumentException("非法请求,可能属于伪造的请求!"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
InputStream inStream = request.getInputStream(); |
|
|
InputStream inStream = request.getInputStream(); |
|
|
ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); |
|
|
ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); |
|
|
byte[] buffer = new byte[1024]; |
|
|
byte[] buffer = new byte[1024]; |
|
|
@@ -179,11 +189,7 @@ public class WechatCalllbackController extends BaseController { |
|
|
logger.info("接收微信APP回调:[appId=[{}], openid=[{}], signature=[{}], encType=[{}], msgSignature=[{}]," |
|
|
logger.info("接收微信APP回调:[appId=[{}], openid=[{}], signature=[{}], encType=[{}], msgSignature=[{}]," |
|
|
+ " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ", |
|
|
+ " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ", |
|
|
appId, openid, signature, encType, msgSignature, timestamp, nonce, resultxml); |
|
|
appId, openid, signature, encType, msgSignature, timestamp, nonce, resultxml); |
|
|
// 第三方平台一定接收的是加密的消息 |
|
|
|
|
|
if (!StringUtils.equalsIgnoreCase("aes", encType) |
|
|
|
|
|
|| !openService.getWxOpenComponentService().checkSignature(timestamp, nonce, signature)) { |
|
|
|
|
|
throw new IllegalArgumentException("非法请求,可能属于伪造的请求!"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WxAppinfo appinfo = appinfoService.getByAppId(appId); |
|
|
WxAppinfo appinfo = appinfoService.getByAppId(appId); |
|
|
|
|
|
|
|
|
@@ -192,33 +198,7 @@ public class WechatCalllbackController extends BaseController { |
|
|
WxMpXmlMessage inMessage = WxOpenXmlMessage.fromEncryptedMpXml(resultxml, |
|
|
WxMpXmlMessage inMessage = WxOpenXmlMessage.fromEncryptedMpXml(resultxml, |
|
|
openService.getWxOpenConfigStorage(), timestamp, nonce, msgSignature); |
|
|
openService.getWxOpenConfigStorage(), timestamp, nonce, msgSignature); |
|
|
logger.info("接收微信APP回调,消息解密后内容为:\n{}", inMessage.toString()); |
|
|
logger.info("接收微信APP回调,消息解密后内容为:\n{}", inMessage.toString()); |
|
|
// 全网发布测试用例 |
|
|
|
|
|
/* |
|
|
|
|
|
if (StringUtils.equalsAnyIgnoreCase(appId, "wx570bc396a51b8ff8", "wxd101a85aa106f53e")) { |
|
|
|
|
|
try { |
|
|
|
|
|
if (StringUtils.equals(inMessage.getMsgType(), "text")) { |
|
|
|
|
|
if (StringUtils.equals(inMessage.getContent(), "TESTCOMPONENT_MSG_TYPE_TEXT")) { |
|
|
|
|
|
out = WxOpenXmlMessage.wxMpOutXmlMessageToEncryptedXml( |
|
|
|
|
|
WxMpXmlOutMessage.TEXT().content("TESTCOMPONENT_MSG_TYPE_TEXT_callback") |
|
|
|
|
|
.fromUser(inMessage.getToUser()) |
|
|
|
|
|
.toUser(inMessage.getFromUser()) |
|
|
|
|
|
.build(), |
|
|
|
|
|
openService.getWxOpenConfigStorage() |
|
|
|
|
|
); |
|
|
|
|
|
} else if (StringUtils.startsWith(inMessage.getContent(), "QUERY_AUTH_CODE:")) { |
|
|
|
|
|
String msg = inMessage.getContent().replace("QUERY_AUTH_CODE:", "") + "_from_api"; |
|
|
|
|
|
WxMpKefuMessage kefuMessage = WxMpKefuMessage.TEXT().content(msg).toUser(inMessage.getFromUser()).build(); |
|
|
|
|
|
openService.getWxOpenComponentService().getWxMpServiceByAppid(appId).getKefuService().sendKefuMessage(kefuMessage); |
|
|
|
|
|
} |
|
|
|
|
|
} else if (StringUtils.equals(inMessage.getMsgType(), "event")) { |
|
|
|
|
|
WxMpKefuMessage kefuMessage = WxMpKefuMessage.TEXT().content(inMessage.getEvent() + "from_callback").toUser(inMessage.getFromUser()).build(); |
|
|
|
|
|
openService.getWxOpenComponentService().getWxMpServiceByAppid(appId).getKefuService().sendKefuMessage(kefuMessage); |
|
|
|
|
|
} |
|
|
|
|
|
} catch (WxErrorException e) { |
|
|
|
|
|
logger.error("callback", e); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
if(appinfo.getType().equals(EnumAppType.B.getCode()) || appinfo.getType().equals(EnumAppType.C.getCode())) { |
|
|
if(appinfo.getType().equals(EnumAppType.B.getCode()) || appinfo.getType().equals(EnumAppType.C.getCode())) { |
|
|
// 小程序 |
|
|
// 小程序 |
|
|
/**判断消息类型,调用对应的方法*/ |
|
|
/**判断消息类型,调用对应的方法*/ |
|
|
@@ -240,13 +220,11 @@ public class WechatCalllbackController extends BaseController { |
|
|
} |
|
|
} |
|
|
case "text": |
|
|
case "text": |
|
|
WxMpXmlOutMessage outMessage = openService.getWxOpenMessageRouter().route(inMessage, appId); |
|
|
WxMpXmlOutMessage outMessage = openService.getWxOpenMessageRouter().route(inMessage, appId); |
|
|
if (outMessage != null) { |
|
|
|
|
|
out = WxOpenXmlMessage.wxMpOutXmlMessageToEncryptedXml(outMessage, openService.getWxOpenConfigStorage()); |
|
|
|
|
|
logger.info(out); |
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
|
|
|
out = "success"; |
|
|
|
|
|
|
|
|
if (outMessage == null) { |
|
|
|
|
|
return "success"; |
|
|
} |
|
|
} |
|
|
|
|
|
out = WxOpenXmlMessage.wxMpOutXmlMessageToEncryptedXml(outMessage, openService.getWxOpenConfigStorage()); |
|
|
|
|
|
logger.info(out); |
|
|
break; |
|
|
break; |
|
|
default: |
|
|
default: |
|
|
return "success"; |
|
|
return "success"; |
|
|
@@ -260,7 +238,9 @@ public class WechatCalllbackController extends BaseController { |
|
|
this.logger.info("2 return: success"); |
|
|
this.logger.info("2 return: success"); |
|
|
return "success"; |
|
|
return "success"; |
|
|
} |
|
|
} |
|
|
|
|
|
logger.info(outMessage.toString()); |
|
|
out = outMessage.toEncryptedXml(mpService.getWxMpConfigStorage()); |
|
|
out = outMessage.toEncryptedXml(mpService.getWxMpConfigStorage()); |
|
|
|
|
|
logger.info("组装回复消息-->"+out); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
logger.error("路由消息时出现异常!", e); |
|
|
logger.error("路由消息时出现异常!", e); |
|
|
} |
|
|
} |
|
|
|