首页 > 解决方案 > Springdoc OpenAPI ui 不遵守“位置”中的上下文路径

问题描述

设置:

我正在使用springdoc-openapi-ui1.4.0 版(通过 Maven)中的 Java 库,而在一个简单的 spring-boot 项目中没有任何自定义。

Swagger 页面在 https://my-url.com/my-context-path/swagger-ui/index.html下生成

以及https://my-url.com/my-context-path/v3/api-docs/下的 api-docs

这两项工作,我可以达到他们。到目前为止,一切都很好!

现在的问题:

当简单地导航到https://my-url.com/my-context-path/swagger-ui.html我得到一个 HTTP 状态 302 和一个location在响应标头中设置的属性,该属性应该将我重定向到招摇页面从上面(我假设)。

但是,location属性中的 URL 错过了上下文路径!它看起来像这样: https ://my-url.com/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config

它重定向到一个不存在的页面,我收到 404 错误代码。请注意,configUrl 似乎也缺少上下文路径。

任何想法为什么会发生这种情况以及如何解决?

这个 Github 问题似乎是同样的问题,但最后指出问题已得到解决:https ://github.com/springdoc/springdoc-openapi/issues/37这是我之前的版本。

标签: swaggerswagger-uispringdocspringdoc-openapi-uispringdoc-ui

解决方案


好的,所以问题是springdoc-openapi-ui不知道您的应用程序上下文路径,除非它是在 Spring Boot 中定义的,这对每个人来说可能都不可能。

X-Forwarded-Prefix希望它确实支持可以由您的网关发送的非标准标头。

在我的情况下(Kubernetes),可以通过简单地添加 Ingress 在您的图表中配置nginx.ingress.kubernetes.io/x-forwarded-prefix: "true"

在您的应用程序配置中,您还需要指定

server:
  forward-headers-strategy: framework

使用 Spring 对处理转发的标头的支持。

资料来源:

https://github.com/kubernetes/ingress-nginx/issues/3670

https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#x-forwarded-prefix-header

https://github.com/springdoc/springdoc-openapi/issues/607


推荐阅读