欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > 使用gateway分流的场合下,服务中的swagger的basePath如何设置?

使用gateway分流的场合下,服务中的swagger的basePath如何设置?

2025/2/23 7:16:21 来源:https://blog.csdn.net/cdnight/article/details/140804597  浏览:    关键词:使用gateway分流的场合下,服务中的swagger的basePath如何设置?

结论:
好了,swagger本身是会读取header的:
X-Forwarded-Prefix “/custom-path”

How can I deploy springdoc-openapi-starter-webmvc-ui behind a reverse proxy?
If your application is running behind a proxy, a load-balancer or in the cloud, the request information (like the host, port, scheme…​) might change along the way. Your application may be running on 10.10.10.10:8080, but HTTP clients should only see example.org.

RFC7239 “Forwarded Headers” defines the Forwarded HTTP header; proxies can use this header to provide information about the original request. You can configure your application to read those headers and automatically use that information when creating links and sending them to clients in HTTP 302 responses, JSON documents or HTML pages. There are also non-standard headers, like X-Forwarded-Host, X-Forwarded-Port, X-Forwarded-Proto, X-Forwarded-Ssl, and X-Forwarded-Prefix.

If the proxy adds the commonly used X-Forwarded-For and X-Forwarded-Proto headers, setting server.forward-headers-strategy to NATIVE is enough to support those. With this option, the Web servers themselves natively support this feature; you can check their specific documentation to learn about specific behavior.

You need to make sure the following header is set in your reverse proxy configuration: X-Forwarded-Prefix

For example, using Apache 2, configuration:

RequestHeader set X-Forwarded-Prefix “/custom-path”
Then, in your Spring Boot application make sure your application handles this header: X-Forwarded-For. There are two ways to achieve this:

server.use-forward-headers=true
If this is not enough, Spring Framework provides a ForwardedHeaderFilter. You can register it as a Servlet Filter in your application by setting server.forward-headers-strategy is set to FRAMEWORK.

Since Spring Boot 3.2, this is the new property to handle reverse proxy headers:

server.forward-headers-strategy=framework

And you can add the following bean to your application:

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词