小编k32*_*32y的帖子

Spring Cloud Gateway:如何将参数传递给自定义过滤器

我在我的application.yml. 我需要从 YAML 中的过滤器定义中获取更多参数,以便我可以在自定义过滤器中执行逻辑。我定义了多个这样的路由,其中​​过滤器参数不同。我面临的问题是,无法读取 YAML 文件中指定的值。

application.yml ---

spring:
  cloud:
    gateway:
      routes:
      - id: test_route
        uri: https://api.rangon.pi
        predicates:
        - Path=/api/staticdata/rattlefeed*
        filters:
        - AddRequestHeader=X-Y-Host, rangon
        - TestGatewayFilter=admin, XY8382, basic
Run Code Online (Sandbox Code Playgroud)

//有没有办法在我的自定义过滤器类中获得“admin, XY8382, basic”

我的过滤器类

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

     @Override
     public GatewayFilter apply(Config config) {
         // grab configuration from Config object

         return (exchange, chain) -> {
             Route r = (Route) exchange.getAttributes().get(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);

             Route route = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
            // ServerWebExchange route =
            // exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
            // List<GatewayFilter> list = r.getFilters();

            GatewayFilter gwf …
Run Code Online (Sandbox Code Playgroud)

cloud spring gateway

5
推荐指数
1
解决办法
4208
查看次数

标签 统计

cloud ×1

gateway ×1

spring ×1