如何保护 Swagger.yaml 文件?

How do I secure Swagger.yaml file?

我正在为我的项目构建 OpenAPI 支持,但我需要 swagger.yaml 文件仅供授权用户使用。

Swagger UI 和 Swagger Editor 似乎都希望 swagger.yaml 文件可以公开访问。

我想过各种绕过它的方法。可以使用 "signed" URL,类似于 S3 的做法——这样只有 link 的人才能访问它。

什么是好的做法?

我明白了。在 Swagger-UI github page 上有对 authorizations 参数的描述,可以在 index.html:

中设置

An authorization object to be passed to swagger-js. Setting it here will trigger inclusion of any authorization or custom signing logic when fetching the swagger description file. Note the object structure should be { key: AuthorizationObject }

我所做的是设置一个 Node.js 服务器来保护 Swagger-UI。如果未在 cookie 中设置令牌,它会通过 OAuth 2.0 流程引导用户,设置 cookie,然后才定向到 swagger-ui.

Swagger-ui依次有authorizations参数设置如下:

authorizations: {
      "_auth": new SwaggerClient.ApiKeyAuthorization(
          "Authorization", "Bearer "+ Cookies.get("MyAccessToken"),
           "header")
    }

然后在下载 swagger.yaml 文件时将 header 传递给服务器。