回调问题处理

This commit is contained in:
lu7213 committed 2026-11-02 22:34:18 -05:00
1 parent 43dec4c94b
commit 1151dc2535
7 files changed
+99 -16

No files matched your search

@@ -9,7 +9,8 @@ rental:
# 允许192.168/10/172.16~31及IPv6 ULA内网HTTP/HTTPS回调;HTTP允许自定义端口,HTTPS仍验证证书。
allow-private-network: ${RENT_WEBHOOK_ALLOW_PRIVATE_NETWORK:true}
webhook-debug:
# 填写发送回调使用的有效APP凭证;缺失返回503,不跳过验签。
# APP按请求Key读取当前有效应用凭证验签,避免轮换后固定Key不匹配;CONFIGURED使用下面的独立凭证。
credential-source: ${RENT_WEBHOOK_DEBUG_CREDENTIAL_SOURCE:APP}
api-key: ${RENT_WEBHOOK_DEBUG_API_KEY:}
api-secret: ${RENT_WEBHOOK_DEBUG_API_SECRET:}
# 签名使用外部URL路径;代理移除前缀时可填写/api/api/webhook/debug。
@@ -5,6 +5,7 @@ import com.ruoyi.system.controller.WebhookDebugController;
import com.ruoyi.system.controller.WebhookDebugTraceFilter;
import com.ruoyi.system.rental.RentalCrypto;
import com.ruoyi.system.rental.RentalStore;
import com.ruoyi.system.rental.RentalIdentity;
import org.junit.Test;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.mock.web.MockHttpServletRequest;
@@ -19,11 +20,13 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
/** 验证原文签名、代理路径、重放和HTTP方法,不连接真实数据库或回调目标。 */
public class WebhookDebugControllerTest {
private final RentalStore store = mock(RentalStore.class);
private final WebhookDebugController controller = new WebhookDebugController(store, new ObjectMapper());
private final RentalIdentity identity = mock(RentalIdentity.class);
private final WebhookDebugController controller = new WebhookDebugController(store, new ObjectMapper(), identity);
private final String body = "{\"event_no\":\"WH-test\",\"event_type\":\"SUCCESS\"}";
private final String externalPath = "/api/api/webhook/debug";
private void configure() {
ReflectionTestUtils.setField(controller, "credentialSource", "CONFIGURED");
ReflectionTestUtils.setField(controller, "apiKey", "test-key");
ReflectionTestUtils.setField(controller, "apiSecret", "test-secret");
ReflectionTestUtils.setField(controller, "signaturePath", externalPath);
@@ -59,6 +62,7 @@ public class WebhookDebugControllerTest {
}
@Test public void postMappingAndMissingConfigurationAreExplicit() throws Exception {
ReflectionTestUtils.setField(controller, "credentialSource", "CONFIGURED");
MockMvc mvc = MockMvcBuilders.standaloneSetup(controller).addFilters(new WebhookDebugTraceFilter()).build();
mvc.perform(get("/api/webhook/debug").header("X-Rent-Callback-Id", "test-callback"))
.andExpect(status().isMethodNotAllowed())
@@ -70,4 +74,26 @@ public class WebhookDebugControllerTest {
mvc.perform(post("/api/webhook/debug").contentType("application/json").content(body))
.andExpect(status().isUnauthorized());
}
@Test public void appModeUsesActualSigningCredentialInsteadOfStaleConfiguredKey() throws Exception {
configure();
ReflectionTestUtils.setField(controller, "credentialSource", "APP");
ReflectionTestUtils.setField(controller, "apiKey", "stale-key");
ReflectionTestUtils.setField(controller, "apiSecret", "stale-secret");
java.util.Map<String, Object> credential = new java.util.HashMap<>();
credential.put("api_key", "test-key");
when(store.one(anyString(), eq("test-key"), any())).thenReturn(credential);
when(identity.secret(credential)).thenReturn("test-secret");
assertEquals("ok", controller.receive(signed(body, System.currentTimeMillis(), externalPath)).getBody());
verify(identity).secret(credential);
verify(store).insert(eq("nonce"), argThat(values -> "test-key".equals(values.get("api_key"))));
}
@Test public void unknownOrInactiveAppKeyIsNotAccepted() throws Exception {
ReflectionTestUtils.setField(controller, "signaturePath", externalPath);
when(store.one(anyString(), eq("test-key"), any())).thenReturn(null);
assertEquals(401, controller.receive(signed(body, System.currentTimeMillis(), externalPath)).getStatusCodeValue());
verifyNoInteractions(identity);
verify(store, never()).insert(anyString(), anyMap());
}
}