405问题日志添加
This commit is contained in:
1 parent
4818f25d8f
commit
43dec4c94b
5 files changed
+134
-4
No files matched your search
@@ -0,0 +1,50 @@
|
||||
package com.ruoyi.system.controller;
|
||||
|
||||
import java.io.IOException;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
|
||||
/** 在MVC匹配之前记录调试回调:即使405未进入Controller,也能判断是否到达本服务。 */
|
||||
@Component
|
||||
@Order(org.springframework.core.Ordered.HIGHEST_PRECEDENCE + 20)
|
||||
public class WebhookDebugTraceFilter extends OncePerRequestFilter {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(WebhookDebugTraceFilter.class);
|
||||
|
||||
@Override
|
||||
protected boolean shouldNotFilter(HttpServletRequest request) {
|
||||
String path = request.getRequestURI().substring(request.getContextPath().length());
|
||||
return !"/api/webhook/debug".equals(path) && !"/api/api/webhook/debug".equals(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
|
||||
throws ServletException, IOException {
|
||||
String incoming = request.getHeader("X-Rent-Callback-Id");
|
||||
String correlation = incoming != null && incoming.matches("[A-Za-z0-9_-]{1,64}")
|
||||
? incoming : java.util.UUID.randomUUID().toString();
|
||||
response.setHeader("X-Rent-Debug-Backend", "ruoyi-webhook-debug-v1");
|
||||
response.setHeader("X-Rent-Callback-Id", correlation);
|
||||
long started = System.nanoTime();
|
||||
LOG.info("租赁调试回调入口 callbackId={} method={} path={} localPort={} contentLength={}",
|
||||
correlation, request.getMethod(), request.getRequestURI(), request.getLocalPort(), request.getContentLengthLong());
|
||||
try {
|
||||
chain.doFilter(request, response);
|
||||
} finally {
|
||||
Object handler = request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE);
|
||||
String matched = handler instanceof HandlerMethod
|
||||
? ((HandlerMethod) handler).getBeanType().getSimpleName() + "#" + ((HandlerMethod) handler).getMethod().getName()
|
||||
: "NO_CONTROLLER_MATCH";
|
||||
LOG.info("租赁调试回调出口 callbackId={} method={} status={} handler={} elapsedMs={}",
|
||||
correlation, request.getMethod(), response.getStatus(), matched, (System.nanoTime() - started) / 1000000);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -319,9 +319,11 @@ public class RentalWorkers {
|
||||
String nonce = RentalCrypto.random();
|
||||
String body = text(row, "payload");
|
||||
long timestamp = System.currentTimeMillis();
|
||||
String callbackId = java.util.UUID.randomUUID().toString();
|
||||
Request request =
|
||||
new Request.Builder()
|
||||
.url(url)
|
||||
.header("X-Rent-Callback-Id", callbackId)
|
||||
.header("X-Rent-Key", key)
|
||||
.header("X-Rent-Timestamp", Long.toString(timestamp))
|
||||
.header("X-Rent-Nonce", nonce)
|
||||
@@ -341,8 +343,8 @@ public class RentalWorkers {
|
||||
MediaType.parse("application/json"), body))
|
||||
.build();
|
||||
stage = "HTTP_EXECUTE";
|
||||
LOG.info("租赁回调请求开始 webhookId={} orderId={} method=POST path={} connectTimeoutMs=5000 callTimeoutMs=10000",
|
||||
id, row.get("order_id"), parsed.encodedPath());
|
||||
LOG.info("租赁回调请求开始 webhookId={} orderId={} callbackId={} method={} scheme={} host={} port={} path={} contentType=application/json followRedirects=false connectTimeoutMs=5000 callTimeoutMs=10000",
|
||||
id, row.get("order_id"), callbackId, request.method(), parsed.scheme(), parsed.host(), parsed.port(), parsed.encodedPath());
|
||||
OkHttpClient client = parsed.isHttps() ? callbacks : privateHttpCallbacks;
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
LOG.info("租赁回调收到HTTP响应 webhookId={} httpStatus={} contentType={} contentLength={} elapsedMs={}",
|
||||
@@ -350,8 +352,18 @@ public class RentalWorkers {
|
||||
response.body() == null ? -1 : response.body().contentLength(),
|
||||
(System.nanoTime() - started) / 1000000);
|
||||
stage = "RESPONSE_ACK";
|
||||
// 仅记录白名单响应头,不记录Location、Cookie、认证头或错误正文。
|
||||
LOG.info("租赁回调响应来源 webhookId={} callbackId={} actualMethod={} actualPath={} server={} allow={} backendMarker={} responseCallbackId={} upstreamTraceId={}",
|
||||
id, callbackId, response.request().method(), response.request().url().encodedPath(),
|
||||
callbackHeader(response, "Server"), callbackHeader(response, "Allow"),
|
||||
callbackHeader(response, "X-Rent-Debug-Backend"), callbackHeader(response, "X-Rent-Callback-Id"),
|
||||
callbackHeader(response, "X-Trace-Id"));
|
||||
if (response.code() != 200) {
|
||||
failure = "HTTP_STATUS_" + response.code();
|
||||
if (response.code() == 405) {
|
||||
LOG.warn("租赁回调方法被拒绝 webhookId={} callbackId={} reason=HTTP_STATUS_405;按callbackId查接收端入口/出口日志。无入口则检查代理路由、静态服务或旧实例;入口method非POST则检查代理改写;入口POST且405则检查运行版本、MVC映射及过滤器。backendMarker={}",
|
||||
id, callbackId, callbackHeader(response, "X-Rent-Debug-Backend"));
|
||||
}
|
||||
} else {
|
||||
String responseBody = RentalHttp.body(response);
|
||||
acked = "ok".equals(responseBody);
|
||||
@@ -386,6 +398,13 @@ public class RentalWorkers {
|
||||
}
|
||||
}
|
||||
|
||||
/** 响应头属于不可信输入,限制长度和字符,防止伪造日志或无界输出。 */
|
||||
private static String callbackHeader(Response response, String name) {
|
||||
String value = response.header(name);
|
||||
if (value == null) return "[absent]";
|
||||
return value.substring(0, Math.min(value.length(), 160)).replaceAll("[^A-Za-z0-9 _.,;:/()=+\\-]", "_");
|
||||
}
|
||||
|
||||
/** 仅返回固定原因码,底层异常原文可能包含URL、响应体或凭据,禁止直接存储或打印。 */
|
||||
static String callbackFailure(Throwable error, String stage) {
|
||||
Set<Throwable> visited = Collections.newSetFromMap(new IdentityHashMap<Throwable, Boolean>());
|
||||
|
||||
Reference in new issue
Block a user