mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-09 20:57:23 +08:00
refactor(log): 新增 excludePatterns 放行路由配置
This commit is contained in:
@@ -20,7 +20,9 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
|||||||
import top.continew.starter.core.constant.PropertiesConstants;
|
import top.continew.starter.core.constant.PropertiesConstants;
|
||||||
import top.continew.starter.log.core.enums.Include;
|
import top.continew.starter.log.core.enums.Include;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -47,6 +49,11 @@ public class LogProperties {
|
|||||||
*/
|
*/
|
||||||
private Set<Include> includes = new HashSet<>(Include.defaultIncludes());
|
private Set<Include> includes = new HashSet<>(Include.defaultIncludes());
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 放行路由
|
||||||
|
*/
|
||||||
|
private List<String> excludePatterns = new ArrayList<>();
|
||||||
|
|
||||||
public boolean isEnabled() {
|
public boolean isEnabled() {
|
||||||
return enabled;
|
return enabled;
|
||||||
}
|
}
|
||||||
@@ -70,4 +77,12 @@ public class LogProperties {
|
|||||||
public void setIncludes(Set<Include> includes) {
|
public void setIncludes(Set<Include> includes) {
|
||||||
this.includes = includes;
|
this.includes = includes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<String> getExcludePatterns() {
|
||||||
|
return excludePatterns;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setExcludePatterns(List<String> excludePatterns) {
|
||||||
|
this.excludePatterns = excludePatterns;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -16,10 +16,12 @@
|
|||||||
|
|
||||||
package top.continew.starter.log.httptracepro.handler;
|
package top.continew.starter.log.httptracepro.handler;
|
||||||
|
|
||||||
|
import cn.hutool.extra.spring.SpringUtil;
|
||||||
import jakarta.servlet.FilterChain;
|
import jakarta.servlet.FilterChain;
|
||||||
import jakarta.servlet.ServletException;
|
import jakarta.servlet.ServletException;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||||
import org.springframework.core.Ordered;
|
import org.springframework.core.Ordered;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
import org.springframework.web.filter.OncePerRequestFilter;
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
@@ -28,6 +30,7 @@ import org.springframework.web.util.ContentCachingResponseWrapper;
|
|||||||
import org.springframework.web.util.WebUtils;
|
import org.springframework.web.util.WebUtils;
|
||||||
import top.continew.starter.log.core.enums.Include;
|
import top.continew.starter.log.core.enums.Include;
|
||||||
import top.continew.starter.log.httptracepro.autoconfigure.LogProperties;
|
import top.continew.starter.log.httptracepro.autoconfigure.LogProperties;
|
||||||
|
import top.continew.starter.web.util.SpringWebUtils;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
@@ -63,26 +66,48 @@ public class LogFilter extends OncePerRequestFilter implements Ordered {
|
|||||||
protected void doFilterInternal(@NonNull HttpServletRequest request,
|
protected void doFilterInternal(@NonNull HttpServletRequest request,
|
||||||
@NonNull HttpServletResponse response,
|
@NonNull HttpServletResponse response,
|
||||||
@NonNull FilterChain filterChain) throws ServletException, IOException {
|
@NonNull FilterChain filterChain) throws ServletException, IOException {
|
||||||
if (!isRequestValid(request)) {
|
if (!this.isFilter(request)) {
|
||||||
filterChain.doFilter(request, response);
|
filterChain.doFilter(request, response);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 包装输入流,可重复读取
|
// 包装输入流,可重复读取
|
||||||
if (isRequestWrapper(request)) {
|
if (this.isRequestWrapper(request)) {
|
||||||
request = new ContentCachingRequestWrapper(request);
|
request = new ContentCachingRequestWrapper(request);
|
||||||
}
|
}
|
||||||
// 包装输出流,可重复读取
|
// 包装输出流,可重复读取
|
||||||
boolean isResponseWrapper = isResponseWrapper(response);
|
boolean isResponseWrapper = this.isResponseWrapper(response);
|
||||||
if (isResponseWrapper) {
|
if (isResponseWrapper) {
|
||||||
response = new ContentCachingResponseWrapper(response);
|
response = new ContentCachingResponseWrapper(response);
|
||||||
}
|
}
|
||||||
filterChain.doFilter(request, response);
|
filterChain.doFilter(request, response);
|
||||||
// 更新响应(不操作这一步,会导致接口响应空白)
|
// 更新响应(不操作这一步,会导致接口响应空白)
|
||||||
if (isResponseWrapper) {
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 请求是否有效
|
* 请求是否有效
|
||||||
*
|
*
|
||||||
|
@@ -17,7 +17,6 @@
|
|||||||
package top.continew.starter.log.httptracepro.handler;
|
package top.continew.starter.log.httptracepro.handler;
|
||||||
|
|
||||||
import cn.hutool.core.text.CharSequenceUtil;
|
import cn.hutool.core.text.CharSequenceUtil;
|
||||||
import cn.hutool.extra.spring.SpringUtil;
|
|
||||||
import com.alibaba.ttl.TransmittableThreadLocal;
|
import com.alibaba.ttl.TransmittableThreadLocal;
|
||||||
import io.swagger.v3.oas.annotations.Hidden;
|
import io.swagger.v3.oas.annotations.Hidden;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
@@ -26,7 +25,6 @@ import jakarta.servlet.http.HttpServletRequest;
|
|||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
import org.springframework.web.method.HandlerMethod;
|
import org.springframework.web.method.HandlerMethod;
|
||||||
import org.springframework.web.servlet.HandlerInterceptor;
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
@@ -63,7 +61,7 @@ public class LogInterceptor implements HandlerInterceptor {
|
|||||||
@NonNull HttpServletResponse response,
|
@NonNull HttpServletResponse response,
|
||||||
@NonNull Object handler) {
|
@NonNull Object handler) {
|
||||||
Clock timestamp = Clock.systemUTC();
|
Clock timestamp = Clock.systemUTC();
|
||||||
if (this.isRequestRecord(handler, request)) {
|
if (this.isRequestRecord(handler)) {
|
||||||
if (Boolean.TRUE.equals(logProperties.getIsPrint())) {
|
if (Boolean.TRUE.equals(logProperties.getIsPrint())) {
|
||||||
log.info("[{}] {}", request.getMethod(), request.getRequestURI());
|
log.info("[{}] {}", request.getMethod(), request.getRequestURI());
|
||||||
}
|
}
|
||||||
@@ -194,18 +192,12 @@ public class LogInterceptor implements HandlerInterceptor {
|
|||||||
* 是否要记录日志
|
* 是否要记录日志
|
||||||
*
|
*
|
||||||
* @param handler 处理器
|
* @param handler 处理器
|
||||||
* @param request 请求对象
|
|
||||||
* @return true:需要记录;false:不需要记录
|
* @return true:需要记录;false:不需要记录
|
||||||
*/
|
*/
|
||||||
private boolean isRequestRecord(Object handler, HttpServletRequest request) {
|
private boolean isRequestRecord(Object handler) {
|
||||||
if (!(handler instanceof HandlerMethod handlerMethod)) {
|
if (!(handler instanceof HandlerMethod handlerMethod)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// 不拦截 /error
|
|
||||||
ServerProperties serverProperties = SpringUtil.getBean(ServerProperties.class);
|
|
||||||
if (request.getRequestURI().equals(serverProperties.getError().getPath())) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// 如果接口被隐藏,不记录日志
|
// 如果接口被隐藏,不记录日志
|
||||||
Operation methodOperation = handlerMethod.getMethodAnnotation(Operation.class);
|
Operation methodOperation = handlerMethod.getMethodAnnotation(Operation.class);
|
||||||
if (null != methodOperation && methodOperation.hidden()) {
|
if (null != methodOperation && methodOperation.hidden()) {
|
||||||
|
Reference in New Issue
Block a user