Kong后面如何配置Spring OpenAPI UI

How to configure Spring OpenAPI UI behind Kong

我有一个 Spring 引导应用程序,我正在使用 Kong 作为 API 网关。

我想用 SpringDoc OpenAPI.

记录我的应用程序的 REST API

当我 运行 我的 Spring 独立启动应用程序时,在本地一切正常,但是在访问后面的 Swagger/OpenAPI UI 时我遇到了问题孔.

这是我的 kong.yml:

services:
 - name: foo
   url: http://localhost:9000/foo
   routes:
   - name: foo-route
     paths:
      - /local/api/foo
     methods:
     - GET
     - POST
     - PUT
     - OPTIONS
     - DELETE
     - PATCH
     - HEAD

假设kong在8000端口,我的Spring App在9000端口。

当我在 http://localhost:8000/local/api/foo/swagger-ui.html 上点击 Kong 时,我被重定向到 http://localhost:8000/foo/swagger-ui/index.html?configUrl=/foo/v3/api-docs/swagger-config,这是错误的路径。

我该如何解决这个问题?

找到解决方案!

在 Spring 引导端,因为我使用的是高于 2.2 的版本,所以只需要以下 bean:

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

和这个配置:

server:
  forward-headers-strategy: framework 

在 Kong 方面,由于我使用的是过时的版本 (2.0.3),我需要将以下插件添加到我的 kong.yml 配置文件中:

plugins:     
 - name: request-transformer
   service: foo
   config:
    add:
     headers:
      - x-forwarded-prefix:/local/api/foo

根据我的理解,在较新的 Kong 版本中不需要这样做:只要主机有可信 ip,header x-forwarded-prefix 就会自动添加。