当前位置:   article > 正文

微信开发平台 第三方平台 后台接口的实现 授权事件接收URL 消息与事件接收URL Java_@requestparam(value = "signature")

@requestparam(value = "signature")

1、授权事件接收 URL

此处要注意5个参数和1个xml响应体,其中xml要注意先Unicode解码一下,然后需要再按照指定格式解析出来encrypt,然后注意wxBizMsgCrypt.decryptMsg的最后一个参数是经过格式转换的,不是原来微信直接返回的xml,此处是关键,很多博主的代码中这一块被工具类代码隐藏了。具体代码如下:

  1. @ApiOperation("授权事件接收URL")
  2. @RequestMapping(value = "/receiveAuth", method ={RequestMethod.POST, RequestMethod.GET})
  3. public Object receiveAuth(@RequestBody String postData,
  4. @RequestParam(value = "signature", required = false) String signature,
  5. @RequestParam(value = "timestamp", required = false) String timeStamp,
  6. @RequestParam(value = "nonce", required = false) String nonce,
  7. @RequestParam(value = "encrypt_type", required = false) String encryptType,
  8. @RequestParam(value = "msg_signature", required = false) String msgSignature) {
  9. try {
  10. //从XML中获取<Encrypt></Encrypt>标签内的密文文本
  11. if (postData.endsWith("\\n")) {
  12. postData = postData.substring(0, postData.length() -2);
  13. }
  14. postData = UnicodeUtil.toString(postData);
  15. Document document = XmlUtil.readXML(postData);
  16. NodeList nodeList = document.getElementsByTagName("Encrypt");
  17. String encrypt = nodeList.item(0).getTextContent();
  18. log.info("Encrypt:" + encrypt);
  19. //格式化密文文本,否则没有<ToUserName>标签,会解密失败,参考官方的加解密代码JAVA版本
  20. String format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%1$s]]></Encrypt></xml>";
  21. String fromXML = String.format(format, encrypt);
  22. String msg; //解密后的明文
  23. if(StrUtil.isEmpty(encrypt)) {
  24. msg = fromXML;
  25. } else {
  26. WXBizMsgCrypt wxBizMsgCrypt = new WXBizMsgCrypt(Token, EncodingAESKey, appId);
  27. // 解密消息
  28. msg = wxBizMsgCrypt.decryptMsg(msgSignature, timeStamp, nonce, fromXML);
  29. }
  30. log.info("解密后的明文:" + msg);
  31. //将XML格式字符串转为Map类型
  32. Document resultDocument = XmlUtil.readXML(msg);
  33. NodeList infoTypeNodeList = resultDocument.getElementsByTagName("InfoType");
  34. String infoType = infoTypeNodeList.item(0).getTextContent();
  35. switch (infoType) {
  36. case "component_verify_ticket":
  37. NodeList ticketNodeList = resultDocument.getElementsByTagName("ComponentVerifyTicket");
  38. String componentVerifyTicket = ticketNodeList.item(0).getTextContent();
  39. Jedis jedis = null;
  40. try {
  41. jedis = RedisUtil.getWeChatJedis();
  42. jedis.set(MiniProgramComponentVerifyTicket, componentVerifyTicket);
  43. jedis.expire(MiniProgramComponentVerifyTicket, 60*60*12);
  44. } finally {
  45. jedis.close();
  46. }
  47. log.info("ComponentVerifyTicket成功更新:{}", componentVerifyTicket);
  48. break;
  49. default:
  50. break;
  51. }
  52. } catch (Exception e) {
  53. log.error(e.getMessage());
  54. }
  55. return "success";
  56. }

 2、消息与事件接收URL,此处只是简单的实现了接口,没有具体做处理

  1. @ApiOperation("消息与事件接收URL")
  2. @RequestMapping(value = "/appid/callback", method ={RequestMethod.POST, RequestMethod.GET})
  3. public Object callback(@RequestBody String xml,
  4. @RequestParam(value = "signature", required = false) String signature,
  5. @RequestParam(value = "timestamp", required = false) String timeStamp,
  6. @RequestParam(value = "nonce", required = false) String nonce,
  7. @RequestParam(value = "encrypt_type", required = false) String encryptType,
  8. @RequestParam(value = "msg_signature", required = false) String msgSignature) throws Exception {
  9. return "success";
  10. }

3、其他接口的实现,要注意2点:

一、参数component_access_token是放在请求链接上的

二、请求体是json转换的字符串,不是json。

相关示例代码:

  1. public String getComponentAccessToken() {
  2. String componentAccessToken = null;
  3. Jedis jedis = null;
  4. try {
  5. jedis = RedisUtil.getWeChatJedis();
  6. componentAccessToken = jedis.get(ComponentAccessToken);
  7. if (componentAccessToken != null) {
  8. return componentAccessToken;
  9. }
  10. Map<String, Object> param = new HashMap<>();
  11. param.put("component_appid", ComponentAppid);
  12. param.put("component_appsecret", ComponentAppsecret);
  13. String componentVerifyTicket = jedis.get(MiniProgramComponentVerifyTicket);
  14. param.put("component_verify_ticket", componentVerifyTicket);
  15. System.out.println(JSONUtil.toJsonStr(param));
  16. String post = HttpUtil.post("https://api.weixin.qq.com/cgi-bin/component/api_component_token", JSONUtil.toJsonStr(param));
  17. log.info("微信第三方平台接口:获取令牌,返回结果:{}", post);
  18. JSONObject jsonObject = JSONUtil.parseObj(post);
  19. Object token = jsonObject.get("component_access_token");
  20. if (token != null) {
  21. jedis.set(ComponentAccessToken, (String) token);
  22. jedis.expire(ComponentAccessToken, 7100);
  23. componentAccessToken = (String) token;
  24. log.info("重新获取component_access_token并放Redis中缓存成功:", token);
  25. }
  26. } catch (Exception e) {
  27. log.error("获取component_access_token失败:", e);
  28. } finally {
  29. jedis.close();
  30. }
  31. return componentAccessToken;
  32. }
  33. /**
  34. * 获取预授权码
  35. * https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/pre_auth_code.html
  36. * @return
  37. */
  38. public ResultDto getPreAuthCode() {
  39. String componentAccessToken = getComponentAccessToken();
  40. Map<String, Object> param = new HashMap<>();
  41. param.put("component_appid", ComponentAppid);
  42. String post = HttpUtil.post("https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=" + componentAccessToken, JSONUtil.toJsonStr(param));
  43. log.info("微信第三方平台接口:获取预授权码,返回结果:{}", post);
  44. JSONObject jsonObject = JSONUtil.parseObj(post);
  45. if (jsonObject.get("pre_auth_code") != null) {
  46. String preAuthCode = (String) jsonObject.get("pre_auth_code");
  47. log.info("获取component_access_token成功:", preAuthCode);
  48. return new ResultDto(CodeEnum.SUCCESS, preAuthCode);
  49. } else {
  50. return new ResultDto(CodeEnum.ERROR_WE_CHAT, post);
  51. }
  52. }
  53. /**
  54. * 使用授权码获取授权信息
  55. * https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/authorization_info.html
  56. * @param req
  57. * @return
  58. */
  59. public ResultDto getAuthorizationInfo(GetAuthorizationInfoReq req) {
  60. String componentAccessToken = getComponentAccessToken();
  61. Map<String, Object> param = new HashMap<>();
  62. param.put("component_appid", ComponentAppid);
  63. param.put("authorization_code", req.getAuthorizationCode());
  64. String post = HttpUtil.post("https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=" + componentAccessToken, JSONUtil.toJsonStr(param));
  65. log.info("微信第三方平台接口:使用授权码获取授权信息,返回结果:{}", post);
  66. JSONObject jsonObject = JSONUtil.parseObj(post);
  67. return new ResultDto(CodeEnum.SUCCESS, jsonObject);
  68. }
  69. public ResultDto getOrRefreshToken(GetOrRefreshTokenReq req) {
  70. String componentAccessToken = getComponentAccessToken();
  71. Map<String, Object> param = new HashMap<>();
  72. param.put("component_appid", ComponentAppid);
  73. param.put("authorizer_appid", req.getAuthorizerAppid());
  74. param.put("authorizer_refresh_token", req.getAuthorizerRefreshToken());
  75. String post = HttpUtil.post("https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=" + componentAccessToken, JSONUtil.toJsonStr(param));
  76. log.info("微信第三方平台接口:获取/刷新接口调用令牌,返回结果:{}", post);
  77. JSONObject jsonObject = JSONUtil.parseObj(post);
  78. return new ResultDto(CodeEnum.SUCCESS, jsonObject);
  79. }
  80. /**
  81. * https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/api_get_authorizer_info.html
  82. * @param req
  83. * @return
  84. */
  85. public ResultDto getAuthorizerInfo(GetAuthorizerInfoReq req) {
  86. String componentAccessToken = getComponentAccessToken();
  87. Map<String, Object> param = new HashMap<>();
  88. param.put("component_appid", ComponentAppid);
  89. param.put("authorizer_appid", req.getAuthorizerAppid());
  90. String post = HttpUtil.post("https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=" + componentAccessToken, JSONUtil.toJsonStr(param));
  91. log.info("微信第三方平台接口:获取授权帐号信息,返回结果:{}", post);
  92. JSONObject jsonObject = JSONUtil.parseObj(post);
  93. return new ResultDto(CodeEnum.SUCCESS, jsonObject);
  94. }
  95. public ResultDto commitCode(CommitCodeReq req) {
  96. Map<String, Object> param = new HashMap<>();
  97. param.put("template_id", req.getTemplateId());
  98. param.put("ext_json", req.getExtJson());
  99. param.put("user_version", req.getUserVersion());
  100. param.put("user_desc", req.getUserDesc());
  101. String post = HttpUtil.post("https://api.weixin.qq.com/wxa/commit?access_token=" + req.getAccessToken(), JSONUtil.toJsonStr(param));
  102. log.info("微信第三方平台接口:上传代码,返回结果:{}", post);
  103. byte[] bytes = HttpUtil.downloadBytes("https://api.weixin.qq.com/wxa/get_qrcode?access_token=" + req.getAccessToken());
  104. // String get = HttpUtil.get("https://api.weixin.qq.com/wxa/get_qrcode?access_token=" + req.getAccessToken());
  105. // log.info("微信第三方平台接口:获取体验版二维码,返回结果:{}", get);
  106. // JSONObject jsonObject = JSONUtil.parseObj(get);
  107. return new ResultDto(CodeEnum.SUCCESS, bytes);
  108. }
  109. public ResultDto getQrcode(GetQrcodeReq req) {
  110. String get = HttpUtil.get("https://api.weixin.qq.com/wxa/get_qrcode?access_token=" + req.getAccessToken());
  111. log.info("微信第三方平台接口:获取体验版二维码,返回结果:{}", get);
  112. // JSONObject jsonObject = JSONUtil.parseObj(get);
  113. return new ResultDto(CodeEnum.SUCCESS, get);
  114. }

 相关参考:

微信开放平台_第三方平台授权流程_验证票据_小张写bug的博客-CSDN博客

微信开放平台---授权事件接收URL---Java_u011645644的博客-CSDN博客_授权事件接收url

java三方开放平台_微信开放平台---授权事件接收URL---Java_weixin_39978749的博客-CSDN博客

java三方开放平台_微信开放平台---授权事件接收URL---Java_weixin_39978749的博客-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/698478
推荐阅读
相关标签
  

闽ICP备14008679号