Skip to content

Commit

Permalink
refactor(log): 新增 excludePatterns 放行路由配置
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Jul 31, 2024
1 parent 702dcca commit bd07f9b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import top.continew.starter.core.constant.PropertiesConstants;
import top.continew.starter.log.core.enums.Include;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
Expand All @@ -47,6 +49,11 @@ public class LogProperties {
*/
private Set<Include> includes = new HashSet<>(Include.defaultIncludes());

/**
* 放行路由
*/
private List<String> excludePatterns = new ArrayList<>();

public boolean isEnabled() {
return enabled;
}
Expand All @@ -70,4 +77,12 @@ public Set<Include> getIncludes() {
public void setIncludes(Set<Include> includes) {
this.includes = includes;
}

public List<String> getExcludePatterns() {
return excludePatterns;
}

public void setExcludePatterns(List<String> excludePatterns) {
this.excludePatterns = excludePatterns;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

package top.continew.starter.log.httptracepro.handler;

import cn.hutool.extra.spring.SpringUtil;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.core.Ordered;
import org.springframework.lang.NonNull;
import org.springframework.web.filter.OncePerRequestFilter;
Expand All @@ -28,6 +30,7 @@
import org.springframework.web.util.WebUtils;
import top.continew.starter.log.core.enums.Include;
import top.continew.starter.log.httptracepro.autoconfigure.LogProperties;
import top.continew.starter.web.util.SpringWebUtils;

import java.io.IOException;
import java.net.URI;
Expand Down Expand Up @@ -63,24 +66,46 @@ public int getOrder() {
protected void doFilterInternal(@NonNull HttpServletRequest request,
@NonNull HttpServletResponse response,
@NonNull FilterChain filterChain) throws ServletException, IOException {
if (!isRequestValid(request)) {
if (!this.isFilter(request)) {
filterChain.doFilter(request, response);
return;
}
// 包装输入流,可重复读取
if (isRequestWrapper(request)) {
if (this.isRequestWrapper(request)) {
request = new ContentCachingRequestWrapper(request);
}
// 包装输出流,可重复读取
boolean isResponseWrapper = isResponseWrapper(response);
boolean isResponseWrapper = this.isResponseWrapper(response);
if (isResponseWrapper) {
response = new ContentCachingResponseWrapper(response);
}
filterChain.doFilter(request, response);
// 更新响应(不操作这一步,会导致接口响应空白)
if (isResponseWrapper) {
updateResponse(response);
this.updateResponse(response);
}
}

/**
* 是否过滤请求
*
* @param request 请求对象
* @return 是否过滤请求
*/
private boolean isFilter(HttpServletRequest request) {
if (!isRequestValid(request)) {
return false;
}
// 不拦截 /error
ServerProperties serverProperties = SpringUtil.getBean(ServerProperties.class);
if (request.getRequestURI().equals(serverProperties.getError().getPath())) {
return false;
}
// 放行
boolean isMatch = logProperties.getExcludePatterns()
.stream()
.anyMatch(pattern -> SpringWebUtils.match(pattern, request.getRequestURI()));
return !isMatch;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package top.continew.starter.log.httptracepro.handler;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.ttl.TransmittableThreadLocal;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -26,7 +25,6 @@
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.lang.NonNull;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
Expand Down Expand Up @@ -63,7 +61,7 @@ public boolean preHandle(@NonNull HttpServletRequest request,
@NonNull HttpServletResponse response,
@NonNull Object handler) {
Clock timestamp = Clock.systemUTC();
if (this.isRequestRecord(handler, request)) {
if (this.isRequestRecord(handler)) {
if (Boolean.TRUE.equals(logProperties.getIsPrint())) {
log.info("[{}] {}", request.getMethod(), request.getRequestURI());
}
Expand Down Expand Up @@ -194,18 +192,12 @@ private void logModule(LogRecord logRecord, Log methodLog, Log classLog, Handler
* 是否要记录日志
*
* @param handler 处理器
* @param request 请求对象
* @return true:需要记录;false:不需要记录
*/
private boolean isRequestRecord(Object handler, HttpServletRequest request) {
private boolean isRequestRecord(Object handler) {
if (!(handler instanceof HandlerMethod handlerMethod)) {
return false;
}
// 不拦截 /error
ServerProperties serverProperties = SpringUtil.getBean(ServerProperties.class);
if (request.getRequestURI().equals(serverProperties.getError().getPath())) {
return false;
}
// 如果接口被隐藏,不记录日志
Operation methodOperation = handlerMethod.getMethodAnnotation(Operation.class);
if (null != methodOperation && methodOperation.hidden()) {
Expand Down

0 comments on commit bd07f9b

Please sign in to comment.