Spring 引导 Zuul hateoas REST 响应在资源中有直接服务链接

Spring Boot Zuul hateoas REST response has direct service links in resource

我有 Zuul + Eureka + Spring Boot Service Endpoint + Hateoas 响应配置。当我通过 Zuul 网关访问服务时,响应中的资源链接是服务端点的直接链接,它们不应该是网关链接吗?我在这里错过了什么?

网关端点:http://localhost:8762/catalog/products/10001 直接服务端点:http://localhost:8100/products/10001

application.properties 对于 Zuul

spring.application.name=zuul-server
eureka.client.service-url.default-zone=http://localhost:8761/eureka/

# Map paths to services
zuul.routes.catalog-service=/catalog/**
zuul.addProxyHeaders=true

网关端点的实际响应:http://localhost:8762/catalog/products/10001

{
  "title" : "The Title",
  "description" : "The Description",
  "brand" : "SOME BRAND",
  "price" : 100,
  "color" : "Black",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8100/products/10001"
    }
  }
}

预期响应应在 href

中包含网关 URL
{
  "title" : "The Title",
  "description" : "The Description",
  "brand" : "SOME BRAND",
  "price" : 100,
  "color" : "Black",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8762/catalog/products/10001"
    }
  }
}

我遇到了这个问题,并在 github

上通过 post 解决了它

要点是

spring-开机<=2.1

@Bean
ForwardedHeaderFilter forwardedHeaderFilter() {
    return new ForwardedHeaderFilter();
}

spring-boot >= 2.2

server.use-forward-headers=true

虽然@Nikhil 说他通过添加@Bean 来修复,但在我的情况下恰恰相反:

我刚刚添加了 forward-headers-strategy: FRAMEWORK(目前,server.use-forward-headers 已被弃用)并且它对我有用。

谢谢@Zipster!

附加信息:

属性 server.use-forward-headers 接受三个可能的值:

  • NONE
  • 原生
  • 框架

我测试了三个选项来检查差异。

我的Zuul网关(8080端口)路由配置如下:

zuul:
  prefix: /api/v0
  sensitive-headers: Cookie,Set-Cookie # Allow Authorization header forwarding
  routes:
    api-v0-questions:
       path: /questions/**
       service-id: api-v0-questions
       strip-prefix: false

NATIVE - URL 指向网关并剥离 /api/v0(顶部前缀):

"_links": {
  "self": {
    "href": "http://localhost:8080/questions/5f6fa0300ec87b34b70393ca"
  }

FRAMEWORK - URL 指向网关并且不去除 /api/v0 前缀:

"_links": {
  "self": {
    "href": "http://localhost:8080/api/v0/questions/5f6fa0300ec87b34b70393ca"
  }

NONE - URL 指向服务,就像根本没有添加 属性:

"_links": {
  "self": {
    "href": "http://localhost:8081/questions/5f6f96ba0ec87b34b70393b2"
  }