From de014483a89107e7aa8a3df845788397d32c329b Mon Sep 17 00:00:00 2001
From: xiaowang <2161656029@qq.com>
Date: Tue, 3 Nov 2026 10:11:13 +0800
Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=BE=AE=E4=BF=A1?=
=?UTF-8?q?=E5=AE=A1=E6=A0=B8=E5=9B=9E=E8=B0=83=E6=94=AF=E6=8C=81JSON?=
=?UTF-8?q?=E6=A0=BC=E5=BC=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...atContentModerationCallbackController.java | 25 +++-
...ntentModerationCallbackControllerTest.java | 134 ++++++++++++++++++
.../scripts/Salon安全接入配置说明_20261030.md | 2 +-
docs/项目变更记录.md | 9 +-
4 files changed, 166 insertions(+), 4 deletions(-)
create mode 100644 JC-Salon-Core/src/test/java/com/xiaowang/jayucloud/salon/core/modules/privacy/controller/front/WechatContentModerationCallbackControllerTest.java
diff --git a/JC-Salon-Core/src/main/java/com/xiaowang/jayucloud/salon/core/modules/privacy/controller/front/WechatContentModerationCallbackController.java b/JC-Salon-Core/src/main/java/com/xiaowang/jayucloud/salon/core/modules/privacy/controller/front/WechatContentModerationCallbackController.java
index b0ee033d..19041638 100644
--- a/JC-Salon-Core/src/main/java/com/xiaowang/jayucloud/salon/core/modules/privacy/controller/front/WechatContentModerationCallbackController.java
+++ b/JC-Salon-Core/src/main/java/com/xiaowang/jayucloud/salon/core/modules/privacy/controller/front/WechatContentModerationCallbackController.java
@@ -46,7 +46,12 @@ public class WechatContentModerationCallbackController {
return echoString;
}
- @PostMapping(value = "/callback", consumes = {MediaType.APPLICATION_XML_VALUE, MediaType.TEXT_XML_VALUE})
+ @PostMapping(value = "/callback", consumes = {
+ MediaType.APPLICATION_JSON_VALUE,
+ "application/*+json",
+ MediaType.APPLICATION_XML_VALUE,
+ MediaType.TEXT_XML_VALUE
+ })
public String callback(@RequestParam("timestamp") String timestamp,
@RequestParam("nonce") String nonce,
@RequestParam(value = "msg_signature", required = false) String messageSignature,
@@ -54,7 +59,7 @@ public class WechatContentModerationCallbackController {
@RequestParam(value = "encrypt_type", required = false) String encryptType,
@org.springframework.web.bind.annotation.RequestBody String body) throws Exception {
ModerationRuntimeConfig config = requireWechatConfig();
- String encrypted = crypto.encryptedValue(body);
+ String encrypted = extractEncryptedPayload(body);
String expectedSignature = "aes".equalsIgnoreCase(encryptType) ? messageSignature : signature;
if (!crypto.verify(config.getCallbackToken(), timestamp, nonce,
"aes".equalsIgnoreCase(encryptType) ? encrypted : "", expectedSignature)) {
@@ -84,6 +89,22 @@ public class WechatContentModerationCallbackController {
return "success";
}
+ private String extractEncryptedPayload(String body) throws Exception {
+ if (!StringUtils.hasText(body)) {
+ throw new IllegalArgumentException("微信审核回调消息体为空");
+ }
+ String trimmed = body.trim();
+ if (trimmed.startsWith("{")) {
+ JsonNode root = objectMapper.readTree(trimmed);
+ String encrypted = firstText(root, "Encrypt", "encrypt");
+ if (!StringUtils.hasText(encrypted)) {
+ throw new IllegalArgumentException("微信审核回调 JSON 缺少 Encrypt 字段");
+ }
+ return encrypted;
+ }
+ return crypto.encryptedValue(body);
+ }
+
private String firstText(JsonNode node, String... keys) {
for (String key : keys) if (node != null && node.hasNonNull(key)) return node.path(key).asText();
return null;
diff --git a/JC-Salon-Core/src/test/java/com/xiaowang/jayucloud/salon/core/modules/privacy/controller/front/WechatContentModerationCallbackControllerTest.java b/JC-Salon-Core/src/test/java/com/xiaowang/jayucloud/salon/core/modules/privacy/controller/front/WechatContentModerationCallbackControllerTest.java
new file mode 100644
index 00000000..34e592e5
--- /dev/null
+++ b/JC-Salon-Core/src/test/java/com/xiaowang/jayucloud/salon/core/modules/privacy/controller/front/WechatContentModerationCallbackControllerTest.java
@@ -0,0 +1,134 @@
+package com.xiaowang.jayucloud.salon.core.modules.privacy.controller.front;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.xiaowang.jayucloud.salon.core.config.WechatMiniProperties;
+import com.xiaowang.jayucloud.salon.core.modules.privacy.service.ContentModerationService;
+import com.xiaowang.jayucloud.salon.core.modules.privacy.support.WechatCallbackCrypto;
+import com.xiaowang.jayucloud.salon.core.modules.system.secure.ModerationRuntimeConfig;
+import com.xiaowang.jayucloud.salon.core.modules.system.service.SecureIntegrationConfigService;
+import org.springframework.http.MediaType;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+class WechatContentModerationCallbackControllerTest {
+ @Test
+ void aesHandshakeVerifiesEncryptedEchoAndReturnsDecryptedValue() {
+ SecureIntegrationConfigService secureConfig = mock(SecureIntegrationConfigService.class);
+ when(secureConfig.getModerationCallbackConfig()).thenReturn(
+ new ModerationRuntimeConfig("WECHAT", "https://sit.example.test/api/integration/wechat/content-moderation/callback", "callback-token", "encoding-aes-key", true));
+ WechatMiniProperties wechatProperties = new WechatMiniProperties();
+ wechatProperties.setAppId("mini-app-id");
+ WechatCallbackCrypto crypto = mock(WechatCallbackCrypto.class);
+ when(crypto.verify("callback-token", "1000", "nonce", "encrypted-echo", "message-signature"))
+ .thenReturn(true);
+ when(crypto.decrypt("encrypted-echo", "encoding-aes-key", "mini-app-id"))
+ .thenReturn("verified-echo");
+ WechatContentModerationCallbackController controller = new WechatContentModerationCallbackController(
+ secureConfig,
+ wechatProperties,
+ crypto,
+ mock(ContentModerationService.class),
+ new ObjectMapper());
+
+ String result = controller.verifyEndpoint("1000", "nonce", "message-signature", null, "aes", "encrypted-echo");
+
+ assertEquals("verified-echo", result);
+ verify(crypto).verify("callback-token", "1000", "nonce", "encrypted-echo", "message-signature");
+ verify(crypto).decrypt("encrypted-echo", "encoding-aes-key", "mini-app-id");
+ }
+
+ @Test
+ void plaintextHandshakeStillUsesPlainSignature() {
+ SecureIntegrationConfigService secureConfig = mock(SecureIntegrationConfigService.class);
+ when(secureConfig.getModerationCallbackConfig()).thenReturn(
+ new ModerationRuntimeConfig("WECHAT", "https://sit.example.test/api/integration/wechat/content-moderation/callback", "callback-token", "encoding-aes-key", true));
+ WechatCallbackCrypto crypto = mock(WechatCallbackCrypto.class);
+ when(crypto.verifyPlain("callback-token", "1000", "nonce", "plain-signature")).thenReturn(true);
+ WechatContentModerationCallbackController controller = new WechatContentModerationCallbackController(
+ secureConfig,
+ new WechatMiniProperties(),
+ crypto,
+ mock(ContentModerationService.class),
+ new ObjectMapper());
+
+ assertEquals("echo", controller.verifyEndpoint("1000", "nonce", null, "plain-signature", null, "echo"));
+ verify(crypto).verifyPlain("callback-token", "1000", "nonce", "plain-signature");
+ }
+
+ @Test
+ void acceptsJsonEnvelopeForAesEncryptedCallback() throws Exception {
+ SecureIntegrationConfigService secureConfig = readyWechatConfig();
+ WechatCallbackCrypto crypto = mock(WechatCallbackCrypto.class);
+ ContentModerationService moderationService = mock(ContentModerationService.class);
+ when(crypto.verify("callback-token", "1000", "nonce", "encrypted-json", "message-signature"))
+ .thenReturn(true);
+ when(crypto.decrypt("encrypted-json", "encoding-aes-key", "mini-app-id"))
+ .thenReturn("{\"trace_id\":\"task-json\",\"result\":{\"suggest\":\"pass\"}}");
+ MockMvc mvc = MockMvcBuilders.standaloneSetup(controller(secureConfig, crypto, moderationService)).build();
+
+ mvc.perform(post("/integration/wechat/content-moderation/callback")
+ .param("timestamp", "1000")
+ .param("nonce", "nonce")
+ .param("msg_signature", "message-signature")
+ .param("encrypt_type", "aes")
+ .contentType(MediaType.APPLICATION_JSON)
+ .content("{\"Encrypt\":\"encrypted-json\"}"))
+ .andExpect(status().isOk())
+ .andExpect(content().string("success"));
+
+ verify(crypto).verify("callback-token", "1000", "nonce", "encrypted-json", "message-signature");
+ verify(moderationService).handleWechatImageResult("task-json", "pass", "微信异步图片审核回调");
+ }
+
+ @Test
+ void keepsXmlEnvelopeSupportForAesEncryptedCallback() throws Exception {
+ SecureIntegrationConfigService secureConfig = readyWechatConfig();
+ WechatCallbackCrypto crypto = mock(WechatCallbackCrypto.class);
+ ContentModerationService moderationService = mock(ContentModerationService.class);
+ String xml = "