修复RequestInitFilter的BUG

This commit is contained in:
白茶清欢 2025-06-06 22:54:10 +08:00
parent 7f9f0adab9
commit 4b494639f5
2 changed files with 18 additions and 20 deletions

View File

@ -15,8 +15,6 @@ import java.util.Map;
@Setter @Setter
@JsonIgnoreProperties(ignoreUnknown = true) // 忽略未知属性字段 @JsonIgnoreProperties(ignoreUnknown = true) // 忽略未知属性字段
public class Request { public class Request {
@JsonIgnore
HttpServletRequest httpServletRequest;// 原始请求
@JsonProperty(RecordField.REQUEST_UA) @JsonProperty(RecordField.REQUEST_UA)
private String userAgent; // 客户端ua private String userAgent; // 客户端ua
@JsonProperty(RecordField.REQUEST_CLIENT_IP) @JsonProperty(RecordField.REQUEST_CLIENT_IP)

View File

@ -40,10 +40,12 @@ public class RequestInitFilter extends HttpFilter {
@Override @Override
public void doFilter(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws IOException, ServletException { public void doFilter(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws IOException, ServletException {
RuntimeContext runtimeContext = new RuntimeContext(); // 运行时上下文数据 RuntimeContext runtimeContext = new RuntimeContext(); // 运行时上下文数据
runtimeContext.setHttpServletRequest(httpServletRequest);
runtimeContext.setHttpServletResponse(httpServletResponse);
setBaseInfo(httpServletRequest,httpServletResponse, runtimeContext); // 设置基础信息 setBaseInfo(httpServletRequest,httpServletResponse, runtimeContext); // 设置基础信息
setRequestQuery(runtimeContext.getRequestInfo()); // 设置请求query信息 setRequestQuery(runtimeContext); // 设置请求query信息
setRequestBody(runtimeContext.getRequestInfo()); // 设置请求Body setRequestBody(runtimeContext); // 设置请求Body
setRequestHeaderAndCookie(runtimeContext.getRequestInfo()); // 填充请求信息: header + cookie setRequestHeaderAndCookie(runtimeContext); // 填充请求信息: header + cookie
setRequestId(runtimeContext); // 设置请求ID, 每次请求需要重新生成 setRequestId(runtimeContext); // 设置请求ID, 每次请求需要重新生成
httpServletRequest.setAttribute(RecordField.RUNTIME_THREAD_CONTEXT, runtimeContext); // 记录到请求上下文中 httpServletRequest.setAttribute(RecordField.RUNTIME_THREAD_CONTEXT, runtimeContext); // 记录到请求上下文中
// 继续向后执行 // 继续向后执行
@ -55,7 +57,6 @@ public class RequestInitFilter extends HttpFilter {
private void setBaseInfo(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, RuntimeContext runtimeContext) { private void setBaseInfo(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, RuntimeContext runtimeContext) {
// request 信息 // request 信息
Request request = new Request(); Request request = new Request();
request.setHttpServletRequest(httpServletRequest);
request.setClientIp(httpServletRequest.getRemoteAddr()); // client ip request.setClientIp(httpServletRequest.getRemoteAddr()); // client ip
request.setRequestMethod(httpServletRequest.getMethod()); // 请求类型 request.setRequestMethod(httpServletRequest.getMethod()); // 请求类型
request.setRequestContentType(httpServletRequest.getContentType()); // 请求类型 request.setRequestContentType(httpServletRequest.getContentType()); // 请求类型
@ -68,10 +69,9 @@ public class RequestInitFilter extends HttpFilter {
} }
// 设置请求Query // 设置请求Query
private void setRequestQuery(Request request) { private void setRequestQuery(RuntimeContext runtimeContext) {
Map<String, String> query = new HashMap<>(); Map<String, String> query = new HashMap<>();
HttpServletRequest httpServletRequest = request.getHttpServletRequest(); // 原始请求实例 Map<String, String[]> queryParamList = runtimeContext.getHttpServletRequest().getParameterMap(); // query参数列表
Map<String, String[]> queryParamList = httpServletRequest.getParameterMap(); // query参数列表
for (Map.Entry<String, String[]> entity : queryParamList.entrySet()) { for (Map.Entry<String, String[]> entity : queryParamList.entrySet()) {
if (entity.getValue().length == 0) { if (entity.getValue().length == 0) {
// 参数值为空 // 参数值为空
@ -81,25 +81,25 @@ public class RequestInitFilter extends HttpFilter {
query.put(entity.getKey(), entity.getValue()[0]); query.put(entity.getKey(), entity.getValue()[0]);
} }
} }
request.setRequestQuery(query); runtimeContext.getRequestInfo().setRequestQuery(query);
} }
// 设置请求Body // 设置请求Body
private void setRequestBody(Request request) throws IOException { private void setRequestBody(RuntimeContext runtimeContext) throws IOException {
HttpServletRequest httpServletRequest = request.getHttpServletRequest(); // 原始请求实例 HttpServletRequest httpServletRequest = runtimeContext.getHttpServletRequest(); // 原始请求实例
ServletInputStream servletInputStream = httpServletRequest.getInputStream(); ServletInputStream servletInputStream = httpServletRequest.getInputStream();
byte[] requestBody = servletInputStream.readAllBytes(); byte[] requestBody = servletInputStream.readAllBytes();
request.setRequestBodyRaw(requestBody); // 原始请求Body runtimeContext.getRequestInfo().setRequestBodyRaw(requestBody); // 原始请求Body
request.setRequestBody(Arrays.toString(requestBody)); // 请求body runtimeContext.getRequestInfo().setRequestBody(Arrays.toString(requestBody)); // 请求body
} }
// 设置请求信息: header + cookie // 设置请求信息: header + cookie
private void setRequestHeaderAndCookie(Request request) { private void setRequestHeaderAndCookie(RuntimeContext runtimeContext) {
HttpServletRequest httpServletRequest = request.getHttpServletRequest(); // 原始请求实例 HttpServletRequest httpServletRequest = runtimeContext.getHttpServletRequest(); // 原始请求实例
request.setRequestHeader(new HashMap<>()); // 请求header runtimeContext.getRequestInfo().setRequestHeader(new HashMap<>()); // 请求header
request.setRequestCookie(new HashMap<>()); // 请求cookie runtimeContext.getRequestInfo().setRequestCookie(new HashMap<>()); // 请求cookie
// 全部请求header // 全部请求header
Enumeration<String> enumeration = httpServletRequest.getHeaderNames(); Enumeration<String> enumeration = httpServletRequest.getHeaderNames();
while (enumeration.hasMoreElements()) { while (enumeration.hasMoreElements()) {
@ -109,12 +109,12 @@ public class RequestInitFilter extends HttpFilter {
continue; continue;
} }
String value = httpServletRequest.getHeader(name); String value = httpServletRequest.getHeader(name);
request.getRequestHeader().put(name, value); runtimeContext.getRequestInfo().getRequestHeader().put(name, value);
} }
// 全部请求cookie // 全部请求cookie
Cookie[] cookieList = httpServletRequest.getCookies(); Cookie[] cookieList = httpServletRequest.getCookies();
for (Cookie cookie : cookieList) { for (Cookie cookie : cookieList) {
request.getRequestCookie().put(cookie.getName(), cookie.getValue()); runtimeContext.getRequestInfo().getRequestCookie().put(cookie.getName(), cookie.getValue());
} }
} }