在Spring Boot应用程序中使用API​​网关时,HATEOAS路径无效

And*_*ter 8 spring spring-data-rest spring-hateoas spring-boot spring-cloud

我有两个Spring启动应用程序,其中一个作为API网关(如本文所讨论的Spring示例).连接到第一个的另一个是使用spring-data-rest(spring-data-neo4j-rest)公开配置文件服务.

第一个应用程序从端口8080开始,并使用zuul将请求路由到第二个,如下所示:

zuul:
  routes:
    profiles:
      path: /profiles/**
      url: http://localhost:8083/profiles/  
Run Code Online (Sandbox Code Playgroud)

这一切都运行良好,并从第二个应用程序提供http:// localhost:8080/profiles的请求.但问题是响应中的HATEOAS链接不正确.调用第二个服务的响应是正确的:

{
    "_links": {
        "self": {
            "href": "http://localhost:8083/profiles{?page,size,sort}",
            "templated": true
        },
        "search": {
            "href": "http://localhost:8083/profiles/search"
        }
    },
    "_embedded": {
        "profiles": [
            {
                "name": "Andrew Rutter",
                "_links": {
                    "self": {
                        "href": "http://localhost:8083/profiles/0"
                    }
                }
            },
            {
                "name": "Andrew Rutter",
                "_links": {
                    "self": {
                        "href": "http://localhost:8083/profiles/1"
                    }
                }
            }
        ]
    },
    "page": {
        "size": 20,
        "totalElements": 2,
        "totalPages": 1,
        "number": 0
    }
}
Run Code Online (Sandbox Code Playgroud)

但当这回到我的API网关时,链接正在被重写

{
  "name": "Andrew Rutter",
  "_links": {
    "self": {
      "href": "http://localhost:8080/profiles/profiles/0"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

哪个是网关路径别名加上实际的服务基础Uri.我错过了一个zuul选项来禁用该行为,只是通过主机调整留下hateoas uri.或者有没有办法让我的网关后面的服务连接到/然后连接到/ profiles的默认资源端点(在这种情况下),这将避免添加不需要的路径.

谢谢!

Bij*_*men 6

Zuul或Spring-Cloud将"X-Forwarded-Host"标头添加到所有转发的请求中,Spring-hateoas尊重并适当地修改链接.引用Spring-Cloud文档:

默认情况下,X-Forwarded-Host标头添加到转发的请求中.要将其关闭,请设置zuul.addProxyHeaders = false.默认情况下,前缀路径被剥离,对后端的请求会选择一个标题"X-Forwarded-Prefix"(上面示例中的"/ myusers").

您可以尝试建议的修复,即设置 zuul.addProxyHeaders=false