13

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());
  }
}       
GOURAV MEHTA
  • 179
  • 1
  • 1
  • 6
  • what is the problem is the filter not getting read or what.Also paste the filter code. – Grinish Nepal Jan 12 '18 at 17:29
  • @GrinishNepal filter code is attached, problem is that how to specify this filter in application.yml filters properties – GOURAV MEHTA Jan 12 '18 at 19:12
  • 3
    Why is this -1? I am trying to figure out the same thing, there is no documentation on how to add a custom filter to a route. – devo Jan 15 '18 at 18:27

3 Answers3

18

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

devo
  • 982
  • 1
  • 11
  • 25
  • 4
    Pay attention that the `PropertiesRouteDefinitionLocator` removes the final part of the filter name if it is equal to `RoutePredicateFactory` or `GatewayFilterFactory`. See: `PropertiesRouteDefinitionLocator`, `FilterDefinition`, `NameUtils`. So if your filter is in class `PrincipalNameGatewayFilterFactory` you have to configure the route with filter name `PrincipalName`. – Marco Gasparetto Jan 26 '18 at 11:18
3

You need to implement GatewayFilterFactory

@Component
public class DemoGatewayFilter implements GatewayFilterFactory<DemoGatewayFilter.Config> {

    @Override
    public GatewayFilter apply(Config config) {
        return (exchange, chain) -> {
            System.out.println("gateway filter name "+config.getName());
            return chain.filter(exchange);
        };
    }

    @Override
    public Config newConfig() {
        return new Config("DemoGatewayFilter");
    }

    public static class Config {

        public Config(String name){
            this.name = name;
        }
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}

and in application.yml file

 spring:
  application:
  cloud:
    gateway:
      routes:
      - id: MayApplication
        uri: http://myapplication:8080
        predicates:
        - Path=/apipath/to/filter/**
        filters:
          - DemoGatewayFilter
Nitin
  • 1,882
  • 1
  • 14
  • 42
0

I think there are some changes in Spring Cloud Gateway 2.2.1.
I referred to SaveSessionGatewayFilterFactory.java.

@Component
public class DemoGatewayFilter implements AbstractGatewayFilterFactory {

    @Override
    public GatewayFilter apply(Object config) {
        return (exchange, chain) -> {
            return chain.filter(exchange);
        };
/*
it works too
       return new GatewayFilter() {
           @Override
           public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
               return chain.filter(exchange);
           }
       }
*/
    }

}

and in application.yml file

 spring:
  application:
  cloud:
    gateway:
      routes:
      - id: MayApplication
        uri: http://myapplication:8080
        predicates:
        - Path=/api/path/to/filter/**
        filters:
          - DemoGatewayFilter
Yubi Lee
  • 23
  • 3