我正在使用Spring Cloud Gateway过滤器,并希望通过GatewayFilterFactory
过滤请求,
但订单不适用于@Order
注释
我尝试交换过滤器的顺序,但是不起作用。
@Slf4j
@Order(1)
@Component
public class AccessFilter implements GatewayFilterFactory<AccessFilter.Config> {
@Override
public GatewayFilter apply(AccessFilter.Config config) {
return (exchange, chain) -> {
log.info("in AccessFilter second");
// do something
}
}
}
@Order(-1)
@Slf4j
@Component
public class AddTraceIdGlobalFilter implements GlobalFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("in globalfilter first");
// do something
}
}
我的Yaml文件:
spring:
cloud:
gateway:
routes:
- id: user_1
uri: http://user:8080
predicates:
- Path=/v1/user/info
filters:
- AccessFilter
我希望输出:
in globalfilter first
in AccessFilter second
但实际输出:
in AccessFilter second
in globalfilter first
答案 0 :(得分:0)
GatewayFilterFactory无法应用@Order
或Ordered
。
您必须使用OrderedGatewayFilter
。
@Component
public class AccessFilter implements GatewayFilterFactory<AccessFilter.Config> {
@Override
public GatewayFilter apply(AccessFilter.Config config) {
return new OrderedGatewayFilter((exchange, chain) -> {
log.info("in AccessFilter second");
// do something
}, 1)
}
}