如何在应用程序中指定自定义过滤器.yml 春季云网关
2022-09-03 12:30:25
我有一个自定义网关过滤器MYGatewayFilter.java文件,现在我想使用此网关过滤器,并将我的路由写入应用程序。yml
spring:
cloud:
gateway:
routes:
- id: login2_route
uri: http://127.0.0.1:8083/login
predicates:
- Path: /login/
filters:
如何为上述路由定义过滤器
自定义筛选器 MyGatewayFilter.java
public class MyGatewayFilter implements GatewayFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request;
if(request.getHeaders().get("x-mydata")!=null){
request= exchange.getRequest().mutate().header("my-new-header",request.getHeaders().get("x-mydata").get(0)).build();
}
return chain.filter(exchange.mutate().request(request).build());
}
}