修复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
@JsonIgnoreProperties(ignoreUnknown = true) // 忽略未知属性字段
public class Request {
@JsonIgnore
HttpServletRequest httpServletRequest;// 原始请求
@JsonProperty(RecordField.REQUEST_UA)
private String userAgent; // 客户端ua
@JsonProperty(RecordField.REQUEST_CLIENT_IP)

View File

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