修复微信审核回调支持JSON格式
This commit is contained in:
+23
-2
@@ -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;
|
||||
|
||||
+134
@@ -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 = "<xml><Encrypt>encrypted-xml</Encrypt></xml>";
|
||||
when(crypto.encryptedValue(xml)).thenReturn("encrypted-xml");
|
||||
when(crypto.verify("callback-token", "1000", "nonce", "encrypted-xml", "message-signature"))
|
||||
.thenReturn(true);
|
||||
when(crypto.decrypt("encrypted-xml", "encoding-aes-key", "mini-app-id"))
|
||||
.thenReturn("{\"trace_id\":\"task-xml\",\"result\":{\"suggest\":\"review\"}}");
|
||||
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_XML)
|
||||
.content(xml))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string("success"));
|
||||
|
||||
verify(crypto).verify("callback-token", "1000", "nonce", "encrypted-xml", "message-signature");
|
||||
verify(moderationService).handleWechatImageResult("task-xml", "review", "微信异步图片审核回调");
|
||||
}
|
||||
|
||||
private SecureIntegrationConfigService readyWechatConfig() {
|
||||
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));
|
||||
return secureConfig;
|
||||
}
|
||||
|
||||
private WechatContentModerationCallbackController controller(SecureIntegrationConfigService secureConfig,
|
||||
WechatCallbackCrypto crypto,
|
||||
ContentModerationService moderationService) {
|
||||
WechatMiniProperties wechatProperties = new WechatMiniProperties();
|
||||
wechatProperties.setAppId("mini-app-id");
|
||||
return new WechatContentModerationCallbackController(
|
||||
secureConfig, wechatProperties, crypto, moderationService, new ObjectMapper());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user