如何在Spring Cloud Gateway中添加特定于路由的自定义过滤器

时间:2019-11-04 04:06:55

标签: java spring spring-cloud api-gateway spring-cloud-gateway

因此,我是Spring Cloud Gateway的新手,并且刚刚开始使用它。我浏览了文档,偶然发现了如何创建自定义过滤器。

https://cloud.spring.io/spring-cloud-gateway/reference/html/#developer-guide

这是我用于创建自定义过滤器的代码-

       @Component
      public class CustomPreFilterFactory extends AbstractGatewayFilterFactory<CustomPreFilterFactory.Config> {


      public static class Config {
        //Put the configuration properties for your filter here
      }

     @Override
     public GatewayFilter apply(Config config) {

       return (exchange,chain) ->{
        ServerHttpRequest.Builder builder = exchange.getRequest().mutate();
        System.out.println("Request came in custom pre filter");
        return chain.filter(exchange.mutate().request(builder.build()).build());
      };
    }
  }

现在,我正在使用网关提供的java route api配置我的路由,所以这是我的路由代码-

        @Bean
      public RouteLocator myRoutes(RouteLocatorBuilder routeLocatorBuilder) 
{
      return routeLocatorBuilder.routes()
         .route( p -> p.path("/hello").uri("http://localhost:8081"))
        .build();
}

现在,我想知道如何将我刚刚创建的自定义过滤器工厂添加到上述以编程方式定义的路由中。

我查看了以下示例,他们在其中注册了自定义过滤器工厂-

  1. https://www.javainuse.com/spring/cloud-filter
  2. https://medium.com/@niral22/spring-cloud-gateway-tutorial-5311ddd59816

它们两者都使用属性而不是使用route api创建路由。

非常感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

这个解决方案对我有用,我创建了一个 OrderedGatewayFilter,像这样注入了 CustomGatewayFilterFactory 并将该过滤器添加到路由中:

@Bean
public RouteLocator myRoutes(RouteLocatorBuilder routeLocatorBuilder, CustomGatewayFilterFactory customGatewayFilterFactory) 
{
    OrderedGatewayFilter orderedGatewayFilter =
      new OrderedGatewayFilter(customGatewayFilterFactory.apply(config), 45);
    return routeLocatorBuilder.routes()
         .route( p -> p.path("/hello").uri("http://localhost:8081").filter(orderedGatewayFilter))
        .build();
}

答案 1 :(得分:0)

您需要注入自定义过滤器,并将其包括在路线中。像这样的东西。

        @Bean
      public RouteLocator myRoutes(RouteLocatorBuilder routeLocatorBuilder, CustomPreFilterFactory cpf) 
{
      return routeLocatorBuilder.routes()
         .route( p -> p.path("/hello").filters(f -> f.filter(myCustomFilter.apply(new Config()))).uri("http://localhost:8081"))
        .build();
}

答案 2 :(得分:-1)

以下是一个路由示例,该路由已定义谓词以匹配所有带有/ api / v1 / first / **的请求URL,并应用前置过滤器来重写路径。还有另一个过滤器用于修改请求标头,然后将请求路由到负载平衡的FIRST-SERVICE。

builder.routes()
            .route(r -> r.path("/api/v1/first/**")
                    .filters(f -> f.rewritePath("/api/v1/first/(?.*)", "/${remains}")
                                    .addRequestHeader("X-first-Header", "first-service-header")
                    )
                    .uri("lb://FIRST-SERVICE/") //downstream endpoint  lb - load balanced
                    .id("queue-service"))

            .build();

下面是等效的.yaml配置。

spring:
  cloud:
    gateway:
      routes:
      - id: first-service
        uri: lb://FIRST-SERVICE
        predicates:
        - Path=/api/v1/first/**
        filters:
        - RewritePath=/api/v1/first/(?.*), /$\{remains}
        - AddRequestHeader=X-first-Header, first-service-header

您可以在此link.

中找到更多此类过滤器

希望这就是您想要的。