Nginx代理背后的SpringDoc/Swagger

kxkpmulp  于 5个月前  发布在  Nginx
关注(0)|答案(3)|浏览(79)

我们在nginx代理后面运行一个服务,以便:
http://service-post:8080/swagger-ui.html路由到公共地址https://host.com/services/post/swagger-ui.html
或者从另一个Angular 来定义:
当nginx接收到https://host.com/services/post/swagger-ui.html上的请求时,它会剥离/services/post/前缀,并将请求传递给/swagger-ui.html路径上的post服务。
在设置任何东西之前(使用默认的SpringDoc配置),我可以正确地看到http://service-post:8080/swagger-ui.html上的swagger文档。
为了在host.com上设置公共地址的路径,我使用:

springdoc.api-docs.path:        /services/post/api-docs
springdoc.swagger-ui.path:      /services/post/swagger-ui.html
springdoc.swagger-ui.configUrl: /services/post/v3/api-docs/swagger-config

字符串
然而,这似乎完全阻止了它:
/swagger-ui.html/api-docs/v3/api-docs/swagger-config都返回404,用于service-post:8080/*https://host.com/services/post/*
似乎唯一工作的是https://host.com/services/post/swagger-ui/index.html,它显示了petstore文档。

我们没有使用Sping Boot ,只使用5.3.1版的Spring MVC。

那么,如何设置以保持对原始路径(例如/api-docs)的处理,但在前缀路径(/services/post/api-docs)上执行查找?

uhry853o

uhry853o1#

最后,我完全忽略了默认的重定向:

  • swagger-ui.html -> `swagger-ui/index.html?url=/v3/api-docs

并实现了我自己的一个:

  • docs -> swagger-ui/index.html?url=MY_PREFIX/v3/api-docs

这样我就不需要改变任何东西,一切都与默认设置。

yv5phkfx

yv5phkfx2#

我找到的解决方案没有一个对我有用,至少一开始是这样。所以这里是最终点击的完整解决方案:

  • 第一个月
# === SPRINGDOC CONFIG ===
server.forward-headers-strategy=framework

springdoc.api-docs.enabled=true
springdoc.api-docs.path=/api-docs/json

springdoc.swagger-ui.path=/api-docs/ # <==== mind the trailing slash
springdoc.swagger-ui.disable-swagger-default-url=true

字符串

  • 完整的nginx配置,我用docker测试了设置,并在/api/user上提供了我的API。
server {
    server_name host.docker.internal;

    location /api/user/ {
        client_max_body_size 128M;
        proxy_pass http://host.docker.internal:3002/;
    }

    location /api/user/api-docs/ {
        rewrite /api/user/(.*) /$1 break;
        proxy_set_header X-Forwarded-Prefix /api/user/;
        proxy_pass http://host.docker.internal:3002/;
    }
}

  • 默认子路径
  • http://localhost:3000/api-docs/json
  • http://localhost:3002/api-docs/swagger-ui/index.html
  • http://localhost:3002/api-docs/json/swagger-config
  • 反向代理路径
  • http://localhost:4000/api/user/api-docs/json
  • http://localhost:4000/api/user/api-docs/swagger-ui/index.html
  • http://localhost:4000/api/user/api-docs/json/swagger-config

这个设置的唯一问题是你不能依赖swagger的重定向。你必须写完整的URL才是安全的(如果有的话,包括尾随的斜杠)。
使用的版本:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.5</version>
    <relativePath />
    <!-- lookup parent from repository -->
</parent>

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.7.0</version>
</dependency>
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-javadoc</artifactId>
    <version>1.7.0</version>
</dependency>

zmeyuzjn

zmeyuzjn3#

都记录在这里:

  • https://springdoc.org/index.html#how-can-i-deploy-springdoc-openapi-ui-behind-a-reverse-proxy

如果你没有使用spring-boot,你可以添加ForwardedHeaderFilter bean:

相关问题