【问题标题】:spring gateway: how to dynamically set URI and path based on request headers and pathspring gateway:如何根据请求头和路径动态设置 URI 和路径
【发布时间】:2020-05-08 22:24:48
【问题描述】:

假设我有传入请求: 主机:my-XXX.domain.com 和路径 /YYY/ZZZ,我想将该请求路由到 uri XXX.YYY.internal.domain.com/ZZZ。我怎样才能实现它?

标准 api 似乎不允许任何类型的模式提取

builder.routes()
    .route{ it
            .header(xxx,xxx)
            .path("/*/**")
            .uri("i can't use here anything captured in header or path function")
    }

有一个函数可以让我访问请求并允许返回任何 URI

                        .filters{
                            it.changeRequestUri {
                                val service = it.request....
                                Optional.of(URI("http://...."))
                            }
                        }
                        .uri("https://this will be ignored")

但我不能在那里设置路径。

是否有任何现有的 api 可以简单地实现它,还是我必须编写自定义过滤器?应该怎么做呢?

【问题讨论】:

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


【解决方案1】:

这对我有用

.filters{
    it.changeRequestUri {
        val (service, environment, newPath) = computeUrlParts(it)
        Optional.of(URI("http://$environment$service.internal.my-company.com/$newPath"))
    }
}
.uri("http://postman-echo.com") //ignored

【讨论】:

  • @piltrek 如果可能,您能否提供更多详细信息。是语法是Java。我觉得这有点不同。
  • @RaviMCA 这是 kotlin
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-12
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
  • 2015-09-09
  • 2023-03-28
相关资源
最近更新 更多