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

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());
  }
}       


Solution 1:[1]

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

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nitin
Solution 2 Yubi Lee