在项目中有个特殊的模块,可走可不走gateway,如果让他走gateway,可以定义一个忽略全局过滤器判断的过滤器。
定义IgnoreGlobalFilter
@Componentpublic class IgnoreGlobalFilterFactor extends AbstractGatewayFilterFactory<IgnoreGlobalFilterFactor.Config> {public IgnoreGlobalFilterFactor() {super(Config.class);}@Overridepublic GatewayFilter apply(Config config) {return this::filter;}public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {exchange.getAttributes().put("@ignoreTestGlobalFilter", true);return chain.filter(exchange);}public static class Config {}@Overridepublic String name() {return "IgnoreGlobalFilter";}}
在全局GlobalFilter中判断忽略@ignoreTestGlobalFilter属性
public final static String ATTRIBUTE_IGNORE_GLOBAL_FILTER = "@ignoreTestGlobalFilter";@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {// 跳过检测if (exchange.getAttribute(ATTRIBUTE_IGNORE_GLOBAL_FILTER) != null) {return chain.filter(exchange);}
配置启用IgnoreGlobalFilter
spring:cloud:gateway:routes:- id: gloabl_filteruri: http://localhost:4101predicates:- Path=/filter/**filters:- StripPrefix=1- id: no_filteruri: http://localhost:4101predicates:- Path=/no-filter/**filters:- StripPrefix=1- IgnoreGlobalFilter #在本路由启用跳过全局过滤器
