How to Specify custom filter in application.yml Spring Cloud Gateway

GOURAV MEHTA :

I have a custom gateway filter MYGatewayFilter.java file now i want to use this gateway filter with my route written in application.yml

 spring:
  cloud:
   gateway:
    routes:
      - id: login2_route
      uri: http://127.0.0.1:8083/login
      predicates:
       - Path: /login/
      filters:

How do i define filters for above route

Custom Filter 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());
  }
}       
devo :

Instead of implementing GatewayFilter you should implement GatewayFilterFactory

and make it a Component:

@Component
public class MyGatewayFilter implements GatewayFilterFactory {

Then you can refer to it by the bean name in your route.

filters:
- MyGatewayFilter

The documentation on this isn't very good at the moment. I was only able to figure this out by looking at the source code for spring-cloud-gateway on github

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=437531&siteId=1