回调验证和调试日志
This commit is contained in:
1 parent
f15f4162eb
commit
4818f25d8f
4 files changed
+164
-56
No files matched your search
@@ -8,6 +8,12 @@ rental:
|
||||
webhook:
|
||||
# 允许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,不跳过验签。
|
||||
api-key: ${RENT_WEBHOOK_DEBUG_API_KEY:}
|
||||
api-secret: ${RENT_WEBHOOK_DEBUG_API_SECRET:}
|
||||
# 签名使用外部URL路径;代理移除前缀时可填写/api/api/webhook/debug。
|
||||
signature-path: ${RENT_WEBHOOK_DEBUG_SIGNATURE_PATH:}
|
||||
# 正常在线、无排队且RPC及时返回时,调度+推送等待约0~5秒,目标10秒左右接单。
|
||||
# 均为上轮执行完成后的等待时间;链上查询耗时不包含在间隔中。修改后重启服务端。
|
||||
delivery:
|
||||
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package com.ruoyi.web.controller.system;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.ruoyi.system.controller.WebhookDebugController;
|
||||
import com.ruoyi.system.rental.RentalCrypto;
|
||||
import com.ruoyi.system.rental.RentalStore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
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 String body = "{\"event_no\":\"WH-test\",\"event_type\":\"SUCCESS\"}";
|
||||
private final String externalPath = "/api/api/webhook/debug";
|
||||
|
||||
private void configure() {
|
||||
ReflectionTestUtils.setField(controller, "apiKey", "test-key");
|
||||
ReflectionTestUtils.setField(controller, "apiSecret", "test-secret");
|
||||
ReflectionTestUtils.setField(controller, "signaturePath", externalPath);
|
||||
}
|
||||
|
||||
private MockHttpServletRequest signed(String content, long timestamp, String path) {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/api/webhook/debug");
|
||||
request.setContentType("application/json");
|
||||
request.setContent(content.getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
||||
request.addHeader("X-Rent-Key", "test-key");
|
||||
request.addHeader("X-Rent-Timestamp", Long.toString(timestamp));
|
||||
request.addHeader("X-Rent-Nonce", "abcdefghijklmnop");
|
||||
request.addHeader("X-Rent-Signature", RentalCrypto.sign("test-secret", "WEBHOOK", "test-key",
|
||||
timestamp, "abcdefghijklmnop", "POST", path, body));
|
||||
return request;
|
||||
}
|
||||
|
||||
@Test public void verifiedPostUsesExternalPathAndRejectsReplay() throws Exception {
|
||||
configure();
|
||||
MockHttpServletRequest request = signed(body, System.currentTimeMillis(), externalPath);
|
||||
assertEquals("ok", controller.receive(request).getBody());
|
||||
verify(store).insert(eq("nonce"), argThat(values -> "DEBUG_WEBHOOK".equals(values.get("scope"))));
|
||||
when(store.insert(eq("nonce"), anyMap())).thenThrow(new DuplicateKeyException("duplicate"));
|
||||
assertEquals(409, controller.receive(signed(body, System.currentTimeMillis(), externalPath)).getStatusCodeValue());
|
||||
}
|
||||
|
||||
@Test public void tamperedExpiredOrWrongPathCannotReachNonceStore() throws Exception {
|
||||
configure();
|
||||
assertEquals(401, controller.receive(signed(body + " ", System.currentTimeMillis(), externalPath)).getStatusCodeValue());
|
||||
assertEquals(401, controller.receive(signed(body, System.currentTimeMillis() - 120000, externalPath)).getStatusCodeValue());
|
||||
assertEquals(401, controller.receive(signed(body, System.currentTimeMillis(), "/wrong")).getStatusCodeValue());
|
||||
verifyNoInteractions(store);
|
||||
}
|
||||
|
||||
@Test public void postMappingAndMissingConfigurationAreExplicit() throws Exception {
|
||||
MockMvc mvc = MockMvcBuilders.standaloneSetup(controller).build();
|
||||
mvc.perform(get("/api/webhook/debug")).andExpect(status().isMethodNotAllowed());
|
||||
mvc.perform(post("/api/webhook/debug").contentType("application/json").content(body))
|
||||
.andExpect(status().isServiceUnavailable());
|
||||
configure();
|
||||
mvc.perform(post("/api/webhook/debug").contentType("application/json").content(body))
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user