Draft
Conversation
|
我已收到 谢谢 我会尽快查看
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题描述
Spring Cloud Gateway 过滤器(如
StripPrefix=1)和自定义AbstractGatewayFilterFactory实现无法正常工作。调试显示网关路由未被执行,导致所有过滤器失效。根本原因
SaToken 的
SaReactorFilter在 Spring Cloud Gateway 处理其路由和过滤器之前拦截了所有请求(/**)。这阻止了网关的内部过滤器链执行,使得所有网关过滤器(包括内置的StripPrefix)失效。解决方案
修改
SaTokenConfig.java以排除网关路由路径,使其不受 SaReactorFilter 拦截,从而让 Spring Cloud Gateway 过滤器正常工作。同时创建了示例自定义过滤器来演示AbstractGatewayFilterFactory和GatewayFilter的实现。更改内容
1. 修复 SaToken 配置
文件:
mall-gateway/src/main/java/com/macro/mall/config/SaTokenConfig.javagetSaReactorFilter()方法以排除网关路由不受 SaToken 拦截/mall-auth/**、/mall-admin/**、/mall-portal/**、/mall-search/**、/mall-demo/**2. 创建自定义网关过滤器
文件:
mall-gateway/src/main/java/com/macro/mall/filter/LoggingGatewayFilterFactory.javaAbstractGatewayFilterFactorypreLogger和postLogger布尔选项的Config类文件:
mall-gateway/src/main/java/com/macro/mall/filter/SimpleLoggingFilter.javaGatewayFilter实现X-Gateway-Filtered、X-Request-Time)3. 更新网关配置
文件:
mall-gateway/src/main/resources/application.ymlmall-portal路由过滤器中添加LoggingGatewayFilterFactoryStripPrefix=1和自定义日志过滤器4. 修复编译问题
@Slf4j注解替换为使用LoggerFactory.getLogger()的手动日志器创建5. 添加单元测试
文件:
mall-gateway/src/test/java/com/macro/mall/filter/LoggingGatewayFilterFactoryTest.java文件:
mall-gateway/src/test/java/com/macro/mall/filter/SimpleLoggingFilterTest.java