Skip to content

Commit

Permalink
perf: 处理Esb请求时,请求结束后加上出口日志,web过滤器只处理request,esb和service过滤器处理request和r…
Browse files Browse the repository at this point in the history
  • Loading branch information
wuyzh39 committed Jan 15, 2025
1 parent a217c9c commit 2cc4230
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,50 @@
package com.tencent.bk.job.common.web.config;

import com.tencent.bk.job.common.web.filter.RepeatableReadWriteServletRequestResponseFilter;
import com.tencent.bk.job.common.web.filter.WebRepeatableReadServletRequestFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FilterConfig {

/**
* 给/esb/api/*, /service/* 用的过滤器,包装request和response
*
*/
@Bean
public FilterRegistrationBean repeatableRSRRFilterRegister() {
FilterRegistrationBean<RepeatableReadWriteServletRequestResponseFilter> registration =
new FilterRegistrationBean<>();
registration.setFilter(repeatableRRRFilter());
registration.addUrlPatterns("/esb/api/*", "/service/*", "/web/*");
registration.addUrlPatterns("/esb/api/*", "/service/*");
registration.setName("repeatableReadRequestResponseFilter");
registration.setOrder(0);
return registration;
}

/**
* 给/web/* 用的过滤器,仅包装request
*
*/
@Bean
public FilterRegistrationBean webRepeatableReadRequestFilterRegister() {
FilterRegistrationBean<WebRepeatableReadServletRequestFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(webRepeatableReadRequestFilter());
registration.addUrlPatterns("/web/*");
registration.setName("webRepeatableReadRequestFilter");
registration.setOrder(1);
return registration;
}

@Bean(name = "repeatableReadRequestResponseFilter")
public RepeatableReadWriteServletRequestResponseFilter repeatableRRRFilter() {
return new RepeatableReadWriteServletRequestResponseFilter();
}

@Bean(name = "webRepeatableReadRequestFilter")
public WebRepeatableReadServletRequestFilter webRepeatableReadRequestFilter() {
return new WebRepeatableReadServletRequestFilter();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

package com.tencent.bk.job.common.web.filter;

import com.tencent.bk.job.common.web.filter.utils.ServletUtil;
import com.tencent.bk.job.common.web.model.RepeatableReadHttpServletResponse;
import com.tencent.bk.job.common.web.model.RepeatableReadWriteHttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
Expand All @@ -49,35 +49,15 @@ public void init(FilterConfig filterConfig) {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
ServletRequest servletRequest = request;
ServletResponse servletResponse = response;
if (isJsonRequest(request)) {
// 仅处理 ContentType: application/json 请求
servletRequest = new RepeatableReadWriteHttpServletRequest((HttpServletRequest) request);
}
if (isJsonResponse(response)) {
// 仅处理 ContentType: application/json 响应
servletResponse = new RepeatableReadHttpServletResponse((HttpServletResponse) response);
if (!ServletUtil.isJsonRequest(request)) {
chain.doFilter(request, response);
return;
}
ServletRequest servletRequest = new RepeatableReadWriteHttpServletRequest((HttpServletRequest) request);
ServletResponse servletResponse = new RepeatableReadHttpServletResponse((HttpServletResponse) response);
chain.doFilter(servletRequest, servletResponse);
}

private boolean isJsonRequest(ServletRequest request) {
return isJsonContentType(request.getContentType());
}

private boolean isJsonResponse(ServletResponse response) {
return isJsonContentType(response.getContentType());
}

private boolean isJsonContentType(String contentType) {
if (StringUtils.isBlank(contentType)) {
return false;
}
contentType = contentType.trim().toLowerCase();
return contentType.startsWith("application/json");
}

@Override
public void destroy() {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.tencent.bk.job.common.web.filter;

import com.tencent.bk.job.common.web.filter.utils.ServletUtil;
import com.tencent.bk.job.common.web.model.RepeatableReadWriteHttpServletRequest;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class WebRepeatableReadServletRequestFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// do nothing
}

/**
* 仅包装ServletRequest,给/web使用
*
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
ServletRequest servletRequest = request;

if (ServletUtil.isJsonRequest(request)) {
// 仅处理 ContentType: application/json 请求
servletRequest = new RepeatableReadWriteHttpServletRequest((HttpServletRequest) request);
}

chain.doFilter(servletRequest, response);
}

@Override
public void destroy() {
// do nothing
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.tencent.bk.job.common.web.filter.utils;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletRequest;


/**
* 处理servlet请求和响应的工具类
*/
public class ServletUtil {

/**
* 判断 servlet 请求的 Content-Type 是否是 application/json
* @param request 请求
* @return 请求的 Content-Type 是否是 application/json
*/
public static boolean isJsonRequest(ServletRequest request) {
return isJsonContentType(request.getContentType());
}

private static boolean isJsonContentType(String contentType) {
if (StringUtils.isBlank(contentType)) {
return false;
}
contentType = contentType.trim().toLowerCase();
return contentType.startsWith("application/json");
}
}

0 comments on commit 2cc4230

Please sign in to comment.