简要描述
xml格式的数据在WxMaMessage类中有fromEncryptedXml方法,包含5个参数。
public static WxMaMessage fromEncryptedXml(String encryptedXml,
WxMaConfig wxMaConfig, String timestamp, String nonce,
String msgSignature)
该方法可以验证消息是否来源微信官方+解密消息。而json格式的数据,只有如下方法:
public static WxMaMessage fromEncryptedJson(String encryptedJson, WxMaConfig config)
该方法只能解密消息,无法验证消息来源。
我目前曲线救国的方案
// 1. 解析请求体,获取Encrypt字段
JSONObject bodyJson = JSONObject.parseObject(body);
String encrypt = bodyJson.getString("Encrypt");
// 2. 验证并解密
WxMaCryptUtils cryptUtils = new WxMaCryptUtils(wxMaService.getWxMaConfig());
String plainText = cryptUtils.decryptContent(msgSignature, timestamp, nonce, encrypt);
虽然可用,但觉得不太优雅,按理说应该也要有一个5个参数重载的fromEncryptedJson方法,同时验证并解密消息。
官方文档地址
https://developers.weixin.qq.com/miniprogram/dev/framework/server-ability/message-push.html
简要描述
xml格式的数据在WxMaMessage类中有fromEncryptedXml方法,包含5个参数。
public static WxMaMessage fromEncryptedXml(String encryptedXml,
WxMaConfig wxMaConfig, String timestamp, String nonce,
String msgSignature)
该方法可以验证消息是否来源微信官方+解密消息。而json格式的数据,只有如下方法:
public static WxMaMessage fromEncryptedJson(String encryptedJson, WxMaConfig config)
该方法只能解密消息,无法验证消息来源。
我目前曲线救国的方案
// 1. 解析请求体,获取Encrypt字段
JSONObject bodyJson = JSONObject.parseObject(body);
String encrypt = bodyJson.getString("Encrypt");
// 2. 验证并解密
WxMaCryptUtils cryptUtils = new WxMaCryptUtils(wxMaService.getWxMaConfig());
String plainText = cryptUtils.decryptContent(msgSignature, timestamp, nonce, encrypt);
虽然可用,但觉得不太优雅,按理说应该也要有一个5个参数重载的fromEncryptedJson方法,同时验证并解密消息。
官方文档地址
https://developers.weixin.qq.com/miniprogram/dev/framework/server-ability/message-push.html