自定义GatewayFilter的方式
1、继承AbstractGatewayfilterfactory
2、继承AbstractNameValueGatewayfilterFactory,针对Name-Value结构的配置处理默认处理好了,拿来即用
通过实现GatewayFilterFactory接口的方式来自定义GatewayFilter
不直接实现该接口而是采用继承AbstractGatewayFilterFactory
1,从AbstractNameValueGatewayFilterFactory类继承
2,编写的过滤器工厂类的命名规则按照 过滤器名称+GatewayFilterFactory来进行命名,并将该过滤器工厂实现交给spring容器
3,在配置文件中可配置前面起的过滤器名称,比如:
routes:
- id: route-driver
uri: lb://hailtaxi-driver
predicates:
- Path=/api/driver/**
- Cookie=username,value
- Header=X-Request-Id, \d+
- Token=Authorization
filters:
- AddRequestHeader=X-Request-red, blue
- AddResponseHeader=X-Response-Red, Blue
- StripPrefix=1
# 自定义filter
- Hello=aaa,bbb
按照规则我们编写的过滤器工厂类为:HelloGatewayFilterFactory
4,具体的实现类为
@Slf4j
@Component
public class HelloGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {
@Override
public GatewayFilter apply(NameValueConfig config) {
return new GatewayFilter() {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("name={},value={}",config.getName(),config.getValue());
// write your logic here
return chain.filter(exchange);//将请求交给下一个filter进行处理
}
};
}
}
补充:通过配置文件配置的Filter,它们的执行顺序由配置顺序决定
标签:GatewayFilter,自定义,配置,filter,过滤器,public From: https://www.cnblogs.com/Eduhg/p/18612676