在Spring Boot应用程序中使用API​​网关时,HATEOAS路径无效 [英] HATEOAS paths are invalid when using an API Gateway in a Spring Boot app

查看:187
本文介绍了在Spring Boot应用程序中使用API​​网关时,HATEOAS路径无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个spring boot应用程序,其中一个充当API网关(如此处

I have two spring boot applications where one of them is acting as an API Gateway (as discussed here Spring Example). The other which is wired into the first one is exposing a profile service using spring-data-rest (spring-data-neo4j-rest).

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

The first application is starting on port 8080 and is using zuul to route requests to the second as follows:

zuul:
  routes:
    profiles:
      path: /profiles/**
      url: http://localhost:8083/profiles/  

这一切都正常,并且从第二个请求开始请求 http://localhost:8080/profiles 应用程序.但是,问题在于响应中的HATEOAS链接不正确.调用第二项服务的响应是正确的:

This all works fine and requests to http://localhost:8080/profiles are being served from the second app. The problem though is that the HATEOAS links in the response are incorrect. The response from calling that second service are correct:

{
    "_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
    }
}

但是当这回到我的API网关时,链接将被重写为

But when this comes back to my API Gateway, the links are being rewritten to

{
  "name": "Andrew Rutter",
  "_links": {
    "self": {
      "href": "http://localhost:8080/profiles/profiles/0"
    }
  }
}

这是网关路径别名加上实际的服务基础Uri.我是否缺少禁用该行为的zuul选项,只是通过主机调整将hateoas uri留在原地.还是有一种方法可以将网关后面的服务连接到/而不是/profiles的默认资源终结点(在这种情况下),从而避免在其中添加不需要的路径.

Which is the gateway path alias plus the actual service base Uri. Am I missing a zuul option to disable that behavior and just leave the hateoas uri in place with a host adjustment. Or is there a way for my service behind the gateway to be wired to / rather then the default resource endpoint of /profiles (in this case) which would avoid the undesirable path being added in.

谢谢!

推荐答案

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

Zuul or Spring-Cloud adds the "X-Forwarded-Host" header to all the forwarded requests, which Spring-hateoas respects and modifies the links appropriately. To quote from Spring-Cloud docs:

X-Forwarded-Host标头添加到 默认.要关闭它,请设置zuul.addProxyHeaders = false.前缀 默认情况下,路径被剥离,并且对后端的请求接收到一个 标头"X-Forwarded-Prefix"(在上面的示例中为"/myusers").

The X-Forwarded-Host header added to the forwarded requests by default. To turn it off set zuul.addProxyHeaders = false. The prefix path is stripped by default, and the request to the backend picks up a header "X-Forwarded-Prefix" ("/myusers" in the examples above).

您可以尝试推荐的修复程序,该修复程序是设置zuul.addProxyHeaders=false

You can try the recommended fix, which is to set the zuul.addProxyHeaders=false

这篇关于在Spring Boot应用程序中使用API​​网关时,HATEOAS路径无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆