feat(core): 新增 OrderedConstants 统一登记过滤器和拦截器相关顺序常量,并调整相关过滤器和拦截器顺序

This commit is contained in:
2025-07-22 20:53:59 +08:00
parent 3e822c0b84
commit a392fab782
8 changed files with 111 additions and 22 deletions

View File

@@ -36,6 +36,7 @@ import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.continew.starter.auth.satoken.autoconfigure.dao.SaTokenDaoConfiguration; import top.continew.starter.auth.satoken.autoconfigure.dao.SaTokenDaoConfiguration;
import top.continew.starter.core.constant.OrderedConstants;
import top.continew.starter.core.constant.PropertiesConstants; import top.continew.starter.core.constant.PropertiesConstants;
import top.continew.starter.core.constant.StringConstants; import top.continew.starter.core.constant.StringConstants;
import top.continew.starter.core.util.GeneralPropertySourceFactory; import top.continew.starter.core.util.GeneralPropertySourceFactory;
@@ -61,7 +62,9 @@ public class SaTokenAutoConfiguration implements WebMvcConfigurer {
@Override @Override
public void addInterceptors(InterceptorRegistry registry) { public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(SpringUtil.getBean(SaInterceptor.class)).addPathPatterns(StringConstants.PATH_PATTERN); registry.addInterceptor(SpringUtil.getBean(SaInterceptor.class))
.addPathPatterns(StringConstants.PATH_PATTERN)
.order(OrderedConstants.Interceptor.AUTH_INTERCEPTOR);
} }
/** /**

View File

@@ -0,0 +1,79 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.continew.starter.core.constant;
import org.springframework.core.Ordered;
/**
* 过滤器和拦截器相关顺序常量
*
* @author Charles7c
* @since 2.13.3
*/
public class OrderedConstants {
/**
* 过滤器顺序
*/
public static final class Filter {
/**
* 链路追踪过滤器顺序
*/
public static final int TRACE_FILTER = Ordered.HIGHEST_PRECEDENCE + 100;
/**
* XSS 过滤器顺序
*/
public static final int XSS_FILTER = Ordered.HIGHEST_PRECEDENCE + 200;
/**
* 日志过滤器顺序
*/
public static final int LOG_FILTER = Ordered.LOWEST_PRECEDENCE - 100;
private Filter() {
}
}
/**
* 拦截器顺序
*/
public static final class Interceptor {
/**
* 租户拦截器顺序
*/
public static final int TENANT_INTERCEPTOR = Ordered.HIGHEST_PRECEDENCE + 100;
/**
* 认证拦截器顺序
*/
public static final int AUTH_INTERCEPTOR = Ordered.HIGHEST_PRECEDENCE + 200;
/**
* 日志拦截器顺序
*/
public static final int LOG_INTERCEPTOR = Ordered.LOWEST_PRECEDENCE - 100;
private Interceptor() {
}
}
private OrderedConstants() {
}
}

View File

@@ -19,9 +19,9 @@ package top.continew.starter.extension.tenant.autoconfigure;
import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.continew.starter.core.constant.OrderedConstants;
import top.continew.starter.extension.tenant.annotation.ConditionalOnEnabledTenant; import top.continew.starter.extension.tenant.annotation.ConditionalOnEnabledTenant;
import top.continew.starter.extension.tenant.config.TenantProvider; import top.continew.starter.extension.tenant.config.TenantProvider;
import top.continew.starter.extension.tenant.interceptor.TenantInterceptor; import top.continew.starter.extension.tenant.interceptor.TenantInterceptor;
@@ -49,6 +49,6 @@ public class TenantWebMvcAutoConfiguration implements WebMvcConfigurer {
@Override @Override
public void addInterceptors(InterceptorRegistry registry) { public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new TenantInterceptor(tenantProperties, tenantProvider)) registry.addInterceptor(new TenantInterceptor(tenantProperties, tenantProvider))
.order(Ordered.HIGHEST_PRECEDENCE); .order(OrderedConstants.Interceptor.TENANT_INTERCEPTOR);
} }
} }

View File

@@ -22,8 +22,10 @@ import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import top.continew.starter.core.constant.OrderedConstants;
import top.continew.starter.log.annotation.ConditionalOnEnabledLog; import top.continew.starter.log.annotation.ConditionalOnEnabledLog;
import top.continew.starter.log.aspect.AccessLogAspect; import top.continew.starter.log.aspect.AccessLogAspect;
import top.continew.starter.log.aspect.LogAspect; import top.continew.starter.log.aspect.LogAspect;
@@ -61,8 +63,11 @@ public class LogAutoConfiguration {
*/ */
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public LogFilter logFilter() { public FilterRegistrationBean<LogFilter> logFilter() {
return new LogFilter(logProperties); FilterRegistrationBean<LogFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new LogFilter(logProperties));
registrationBean.setOrder(OrderedConstants.Filter.LOG_FILTER);
return registrationBean;
} }
/** /**

View File

@@ -22,7 +22,6 @@ 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.boot.autoconfigure.web.ServerProperties;
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;
import top.continew.starter.core.wrapper.RepeatReadRequestWrapper; import top.continew.starter.core.wrapper.RepeatReadRequestWrapper;
@@ -40,7 +39,7 @@ import java.net.URISyntaxException;
* @author echo * @author echo
* @since 1.1.0 * @since 1.1.0
*/ */
public class LogFilter extends OncePerRequestFilter implements Ordered { public class LogFilter extends OncePerRequestFilter {
private final LogProperties logProperties; private final LogProperties logProperties;
@@ -48,11 +47,6 @@ public class LogFilter extends OncePerRequestFilter implements Ordered {
this.logProperties = logProperties; this.logProperties = logProperties;
} }
@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE - 10;
}
@Override @Override
protected void doFilterInternal(@NonNull HttpServletRequest request, protected void doFilterInternal(@NonNull HttpServletRequest request,
@NonNull HttpServletResponse response, @NonNull HttpServletResponse response,

View File

@@ -22,10 +22,12 @@ import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.continew.starter.core.constant.OrderedConstants;
import top.continew.starter.core.constant.StringConstants; import top.continew.starter.core.constant.StringConstants;
import top.continew.starter.log.annotation.ConditionalOnEnabledLog; import top.continew.starter.log.annotation.ConditionalOnEnabledLog;
import top.continew.starter.log.dao.LogDao; import top.continew.starter.log.dao.LogDao;
@@ -59,7 +61,8 @@ public class LogAutoConfiguration implements WebMvcConfigurer {
public void addInterceptors(InterceptorRegistry registry) { public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LogInterceptor(logProperties, logHandler(), logDao())) registry.addInterceptor(new LogInterceptor(logProperties, logHandler(), logDao()))
.addPathPatterns(StringConstants.PATH_PATTERN) .addPathPatterns(StringConstants.PATH_PATTERN)
.excludePathPatterns(logProperties.getExcludePatterns()); .excludePathPatterns(logProperties.getExcludePatterns())
.order(OrderedConstants.Interceptor.LOG_INTERCEPTOR);
} }
/** /**
@@ -67,8 +70,11 @@ public class LogAutoConfiguration implements WebMvcConfigurer {
*/ */
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public LogFilter logFilter() { public FilterRegistrationBean<LogFilter> logFilter() {
return new LogFilter(logProperties); FilterRegistrationBean<LogFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new LogFilter(logProperties));
registrationBean.setOrder(OrderedConstants.Filter.LOG_FILTER);
return registrationBean;
} }
/** /**

View File

@@ -25,6 +25,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplicat
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import top.continew.starter.core.constant.OrderedConstants;
import top.continew.starter.core.constant.PropertiesConstants; import top.continew.starter.core.constant.PropertiesConstants;
import top.continew.starter.security.xss.filter.XssFilter; import top.continew.starter.security.xss.filter.XssFilter;
@@ -43,12 +44,13 @@ public class XssAutoConfiguration {
private static final Logger log = LoggerFactory.getLogger(XssAutoConfiguration.class); private static final Logger log = LoggerFactory.getLogger(XssAutoConfiguration.class);
/** /**
* XSS 过滤器配置 * XSS 过滤器
*/ */
@Bean @Bean
public FilterRegistrationBean<XssFilter> xssFilter(XssProperties xssProperties) { public FilterRegistrationBean<XssFilter> xssFilter(XssProperties xssProperties) {
FilterRegistrationBean<XssFilter> registrationBean = new FilterRegistrationBean<>(); FilterRegistrationBean<XssFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new XssFilter(xssProperties)); registrationBean.setFilter(new XssFilter(xssProperties));
registrationBean.setOrder(OrderedConstants.Filter.XSS_FILTER);
return registrationBean; return registrationBean;
} }

View File

@@ -30,7 +30,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered; import top.continew.starter.core.constant.OrderedConstants;
import top.continew.starter.core.constant.PropertiesConstants; import top.continew.starter.core.constant.PropertiesConstants;
import top.continew.starter.trace.filter.TLogServletFilter; import top.continew.starter.trace.filter.TLogServletFilter;
import top.continew.starter.trace.handler.TraceIdGenerator; import top.continew.starter.trace.handler.TraceIdGenerator;
@@ -70,14 +70,14 @@ public class TraceAutoConfiguration {
} }
/** /**
* TLog 过滤器配置 * TLog 过滤器
*/ */
@Bean @Bean
public FilterRegistrationBean<TLogServletFilter> tLogServletFilter() { public FilterRegistrationBean<TLogServletFilter> tLogServletFilter() {
FilterRegistrationBean<TLogServletFilter> registration = new FilterRegistrationBean<>(); FilterRegistrationBean<TLogServletFilter> registrationBean = new FilterRegistrationBean<>();
registration.setFilter(new TLogServletFilter(traceProperties)); registrationBean.setFilter(new TLogServletFilter(traceProperties));
registration.setOrder(Ordered.HIGHEST_PRECEDENCE); registrationBean.setOrder(OrderedConstants.Filter.TRACE_FILTER);
return registration; return registrationBean;
} }
/** /**