Skip to content

Commit 732d4b1

Browse files
authored
🎨 #4069 【小程序】完善微信小程序消息推送(安全模式)JSON 入参的验签解密能力
1 parent 5f9fe24 commit 732d4b1

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaMessage.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,23 @@ public static WxMaMessage fromEncryptedJson(String encryptedJson, WxMaConfig con
629629
}
630630
}
631631

632+
/**
633+
* 从加密字符串转换.
634+
*
635+
* @param encryptedJson 密文
636+
* @param config 配置存储器对象
637+
* @param timestamp 时间戳
638+
* @param nonce 随机串
639+
* @param msgSignature 签名串
640+
*/
641+
public static WxMaMessage fromEncryptedJson(String encryptedJson, WxMaConfig config,
642+
String timestamp, String nonce, String msgSignature) {
643+
WxMaMessage encryptedMessage = fromJson(encryptedJson);
644+
String plainText = new WxMaCryptUtils(config).decryptContent(msgSignature, timestamp, nonce,
645+
encryptedMessage.getEncrypt());
646+
return fromJson(plainText);
647+
}
648+
632649
public static WxMaMessage fromEncryptedJson(InputStream inputStream, WxMaConfig config) {
633650
try {
634651
return fromEncryptedJson(IOUtils.toString(inputStream, StandardCharsets.UTF_8), config);
@@ -637,6 +654,16 @@ public static WxMaMessage fromEncryptedJson(InputStream inputStream, WxMaConfig
637654
}
638655
}
639656

657+
public static WxMaMessage fromEncryptedJson(InputStream inputStream, WxMaConfig config,
658+
String timestamp, String nonce, String msgSignature) {
659+
try {
660+
return fromEncryptedJson(IOUtils.toString(inputStream, StandardCharsets.UTF_8), config,
661+
timestamp, nonce, msgSignature);
662+
} catch (IOException e) {
663+
throw new WxRuntimeException(e);
664+
}
665+
}
666+
640667
@Override
641668
public String toString() {
642669
return this.toJson();

weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/bean/WxMaMessageTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22

33
import cn.binarywang.wx.miniapp.bean.xpay.WxMaXPayTeamInfo;
44
import cn.binarywang.wx.miniapp.constant.WxMaConstants;
5+
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
6+
import cn.binarywang.wx.miniapp.util.crypt.WxMaCryptUtils;
57
import me.chanjar.weixin.common.api.WxConsts;
8+
import me.chanjar.weixin.common.error.WxRuntimeException;
9+
import me.chanjar.weixin.common.util.crypto.SHA1;
610
import org.testng.annotations.Test;
711

12+
import java.io.ByteArrayInputStream;
13+
import java.nio.charset.StandardCharsets;
814
import java.util.List;
915
import java.util.Map;
1016

1117
import static org.assertj.core.api.Assertions.as;
1218
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
1320
import static org.testng.Assert.assertEquals;
1421
import static org.testng.Assert.assertNotNull;
1522

@@ -459,6 +466,48 @@ private void checkXPayComplaintNotifyMessage(WxMaMessage msg) {
459466
assertEquals(msg.getRequestId(), "req_005");
460467
}
461468

469+
@Test
470+
public void testFromEncryptedJsonWithSignature() {
471+
WxMaDefaultConfigImpl config = buildMessagePushConfig();
472+
String plainJson = "{\"ToUserName\":\"gh_123456789abc\",\"FromUserName\":\"fromUser\",\"CreateTime\":1710000000,"
473+
+ "\"MsgType\":\"event\",\"Event\":\"subscribe_msg_popup_event\"}";
474+
String encrypt = new WxMaCryptUtils(config).encrypt("1234567890abcdef", plainJson);
475+
String timestamp = "1710000000";
476+
String nonce = "nonce123";
477+
String msgSignature = SHA1.gen(config.getToken(), timestamp, nonce, encrypt);
478+
String encryptedJson = "{\"Encrypt\":\"" + encrypt + "\"}";
479+
480+
WxMaMessage wxMessage = WxMaMessage.fromEncryptedJson(new ByteArrayInputStream(
481+
encryptedJson.getBytes(StandardCharsets.UTF_8)), config, timestamp, nonce, msgSignature);
482+
483+
assertEquals(wxMessage.getToUser(), "gh_123456789abc");
484+
assertEquals(wxMessage.getFromUser(), "fromUser");
485+
assertEquals(wxMessage.getCreateTime(), new Integer(1710000000));
486+
assertEquals(wxMessage.getMsgType(), WxConsts.XmlMsgType.EVENT);
487+
assertEquals(wxMessage.getEvent(), "subscribe_msg_popup_event");
488+
}
489+
490+
@Test
491+
public void testFromEncryptedJsonWithSignatureInvalidMsgSignature() {
492+
WxMaDefaultConfigImpl config = buildMessagePushConfig();
493+
String plainJson = "{\"ToUserName\":\"gh_123456789abc\",\"FromUserName\":\"fromUser\"}";
494+
String encrypt = new WxMaCryptUtils(config).encrypt("1234567890abcdef", plainJson);
495+
String encryptedJson = "{\"Encrypt\":\"" + encrypt + "\"}";
496+
497+
assertThatThrownBy(() -> WxMaMessage.fromEncryptedJson(encryptedJson, config,
498+
"1710000000", "nonce123", "invalidSignature"))
499+
.isInstanceOf(WxRuntimeException.class)
500+
.hasMessageContaining("加密消息签名校验失败");
501+
}
502+
503+
private WxMaDefaultConfigImpl buildMessagePushConfig() {
504+
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
505+
config.setAppid("wx1234567890abcdef");
506+
config.setToken("testToken");
507+
config.setAesKey("abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG");
508+
return config;
509+
}
510+
462511
/**
463512
* 虚拟支付 iOS 退款查询通知事件 xpay_subscribe_ios_refund_query_notify 测试用例(XML格式)
464513
*/

0 commit comments

Comments
 (0)