如何在 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路由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

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


任何帮助深表感谢。


忽然笑
浏览 136回答 3
3回答

翻阅古今

这个解决方案对我有用,我使用注入的 CustomGatewayFilterFactory 创建了一个 OrderedGatewayFilter ,如下所示,并将该过滤器添加到路由中:@Beanpublic RouteLocator myRoutes(RouteLocatorBuilder routeLocatorBuilder, CustomGatewayFilterFactory customGatewayFilterFactory)&nbsp;{&nbsp; &nbsp; OrderedGatewayFilter orderedGatewayFilter =&nbsp; &nbsp; &nbsp; new OrderedGatewayFilter(customGatewayFilterFactory.apply(config), 45);&nbsp; &nbsp; return routeLocatorBuilder.routes()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.route( p -> p.path("/hello").uri("http://localhost:8081").filter(orderedGatewayFilter))&nbsp; &nbsp; &nbsp; &nbsp; .build();}

饮歌长啸

下面是一个路由示例,该路由定义了一个谓词来匹配所有带有 /api/v1/first/** 的请求 URL,并应用预过滤器来重写路径。还有另一个过滤器用于修改请求标头,然后将请求路由到负载平衡的 FIRST-SERVICE。builder.routes()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .route(r -> r.path("/api/v1/first/**")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filters(f -> f.rewritePath("/api/v1/first/(?.*)", "/${remains}")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addRequestHeader("X-first-Header", "first-service-header")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .uri("lb://FIRST-SERVICE/") //downstream endpoint&nbsp; lb - load balanced&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .id("queue-service"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();下面是等效的 .yaml 配置。spring:&nbsp; cloud:&nbsp; &nbsp; gateway:&nbsp; &nbsp; &nbsp; routes:&nbsp; &nbsp; &nbsp; - id: first-service&nbsp; &nbsp; &nbsp; &nbsp; uri: lb://FIRST-SERVICE&nbsp; &nbsp; &nbsp; &nbsp; predicates:&nbsp; &nbsp; &nbsp; &nbsp; - Path=/api/v1/first/**&nbsp; &nbsp; &nbsp; &nbsp; filters:&nbsp; &nbsp; &nbsp; &nbsp; - RewritePath=/api/v1/first/(?.*), /$\{remains}&nbsp; &nbsp; &nbsp; &nbsp; - AddRequestHeader=X-first-Header, first-service-header您可以在此链接中找到更多此类过滤器。希望这就是您正在寻找的。

海绵宝宝撒

您需要注入自定义过滤器并将其包含在路由中。像这样的东西..&nbsp; &nbsp; &nbsp; &nbsp; @Bean&nbsp; &nbsp; &nbsp; public RouteLocator myRoutes(RouteLocatorBuilder routeLocatorBuilder, CustomPreFilterFactory cpf)&nbsp;{&nbsp; &nbsp; &nbsp; return routeLocatorBuilder.routes()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.route( p -> p.path("/hello").filters(f -> f.filter(myCustomFilter.apply(new Config()))).uri("http://localhost:8081"))&nbsp; &nbsp; &nbsp; &nbsp; .build();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java