服务端添加一个webhook通用的调试免登接口,主要是打印数据,和校验webhook是否有生效用的
This commit is contained in:
1 parent
285d08b00d
commit
3ca5ff43ea
2 files changed
+90
No files matched your search
@@ -146,6 +146,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
// 租赁商户及节点另用四域HMAC认证;绝不共用支付MD5签名。
|
||||
.antMatchers("/api/rental/v1/**", "/node/v1/socket").permitAll()
|
||||
.antMatchers("/api/telegram/webhook").permitAll()
|
||||
// 通用回调调试入口,仅接收并记录数据,不执行任何业务操作。
|
||||
.antMatchers("/api/webhook/debug").permitAll()
|
||||
.antMatchers("/system/demoUsdt/**").permitAll()
|
||||
.anyRequest().authenticated()
|
||||
.and()
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package com.ruoyi.system.controller;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.regex.Pattern;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.util.UriUtils;
|
||||
|
||||
/**
|
||||
* 通用 Webhook 免登录调试入口。只用于确认网络连通和观察回调数据,
|
||||
* 不校验业务签名、不修改订单,也不代表业务回调已验签或已处理成功。
|
||||
*/
|
||||
@RestController
|
||||
public class WebhookDebugController {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(WebhookDebugController.class);
|
||||
|
||||
/** 限制单次读取量,避免匿名请求产生无限大的日志或内存占用。 */
|
||||
private static final int MAX_BODY_BYTES = 64 * 1024;
|
||||
|
||||
/** JSON、表单和查询参数中的常见凭据不得直接写入日志。 */
|
||||
private static final Pattern SENSITIVE_VALUE = Pattern.compile(
|
||||
"(?i)([\"']?(?:token_key|private_key|privateKey|api_secret|apiSecret|password|"
|
||||
+ "mnemonic|authorization|secret|access_token|token|signature)[\"']?\\s*[:=]\\s*)"
|
||||
+ "(\"(?:\\\\.|[^\"\\\\])*\"|'[^']*'|[^&\\s,}]+)");
|
||||
|
||||
/**
|
||||
* 接受任意 HTTP 方法和正文格式,兼容 JSON、表单及纯文本回调。
|
||||
* 正常接收返回 HTTP 200 和纯文本 ok,便于回调发送方确认送达。
|
||||
* 仅记录 Content-Type 等必要元数据,不记录可能携带令牌的请求头。
|
||||
*
|
||||
* @param request 原始回调请求
|
||||
* @return 回调接收确认;正文超过上限时返回 HTTP 413
|
||||
* @throws IOException 请求流读取失败时由框架返回错误,不伪装成成功
|
||||
*/
|
||||
@RequestMapping(value = "/api/webhook/debug", produces = MediaType.TEXT_PLAIN_VALUE)
|
||||
public ResponseEntity<String> receive(HttpServletRequest request) throws IOException {
|
||||
byte[] buffer = new byte[MAX_BODY_BYTES + 1];
|
||||
int total = 0;
|
||||
int count;
|
||||
while (total < buffer.length
|
||||
&& (count = request.getInputStream().read(buffer, total, buffer.length - total)) != -1) {
|
||||
total += count;
|
||||
}
|
||||
if (total > MAX_BODY_BYTES) {
|
||||
LOG.warn("Webhook debug rejected: body exceeds {} bytes", MAX_BODY_BYTES);
|
||||
return ResponseEntity.status(413).body("payload too large");
|
||||
}
|
||||
String body = new String(buffer, 0, total, StandardCharsets.UTF_8);
|
||||
String query = request.getQueryString();
|
||||
// 表单先解码再脱敏,防止百分号编码的字段名绕过敏感字段匹配。
|
||||
if (request.getContentType() != null
|
||||
&& request.getContentType().startsWith(MediaType.APPLICATION_FORM_URLENCODED_VALUE)) {
|
||||
body = decode(body);
|
||||
}
|
||||
LOG.info("Webhook debug received: method={}, path={}, remote={}, contentType={}, query={}, body={}",
|
||||
new Object[] {request.getMethod(), request.getRequestURI(), request.getRemoteAddr(),
|
||||
safe(request.getContentType()), safe(decode(query)), safe(body)});
|
||||
return ResponseEntity.ok("ok");
|
||||
}
|
||||
|
||||
/** 非法 URL 编码不回显原文,避免错误日志泄露凭据。 */
|
||||
private static String decode(String value) {
|
||||
if (value == null) {
|
||||
return "";
|
||||
}
|
||||
try {
|
||||
return UriUtils.decode(value, StandardCharsets.UTF_8.name());
|
||||
} catch (Exception exception) {
|
||||
return "[invalid URL encoding]";
|
||||
}
|
||||
}
|
||||
|
||||
/** 脱敏后转义换行,确保一条回调对应一条日志,避免伪造日志行。 */
|
||||
private static String safe(String value) {
|
||||
if (value == null) {
|
||||
return "";
|
||||
}
|
||||
return SENSITIVE_VALUE.matcher(value).replaceAll("$1\"***\"")
|
||||
.replace("\r", "\\r").replace("\n", "\\n");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user