如何防止 Swagger UI 在浏览器重新加载时丢失身份验证

时间:2021-03-15 15:22:00

标签: symfony jwt swagger-ui api-platform.com

虽然我的最终目标是防止 Swagger UI 在浏览器重新加载时丢失身份验证,但我相信我可能已经找到了一个解决方案,假设在使用 api-platform 时可以更改 swagger-ui 参数,并在本文末尾进行了描述.

REST API 使用 Symfony、API 平台并使用 JWT 进行身份验证,文档由 swagger-ui 提供。在swagger-ui页面,提交apiKey后,以后的请求会把它包含在header中,但是如果刷新浏览器,授权token就会丢失。 enter image description here

关于这个话题已经有一些讨论,主要是关于这个 github post 和一些关于这个 stackoverflow post,并且普遍的共识似乎是 swagger-ui 没有“官方”的方式来持久化令牌.

<块引用>

整体 Swagger UI 不存储令牌,并且可能是故意的。 没有启用此功能的开关,但看起来很少 可以通过 cookie、本地存储来记住令牌的事情, indexdb 等,当页面重新加载时,重新填充令牌

然而,swagger configuration documentation 似乎有一个授权参数,允许在浏览器刷新时保留授权数据。

  • 参数名称:persistAuthorization
  • Docker 变量:PERSIST_AUTHORIZATION
  • 描述:Boolean=false。如果设置为 true,它会保留授权数据,并且不会在浏览器关闭/刷新时丢失

假设我正确解释了 Swagger 文档,如何将 persistAuthorization 参数设置为 true?

在修改 config/api_platform.yaml 以设置 persistAuthorization 时,我收到错误 Unrecognized option "persistAuthorization" under "api_platform.swagger.api_keys.apiKey". Available options are "name", "type".Unrecognized option "persistAuthorization" under "api_platform.swagger". Available options are "api_keys", "versions".

api_platform:
    mapping:
        paths: ['%kernel.project_dir%/src/Entity']
    patch_formats:
        json: ['application/merge-patch+json']
    swagger:
        api_keys:
            apiKey:
                name: Authorization
                type: header
                # persistAuthorization: true
        versions: [3]
        # persistAuthorization: true

然后我尝试添加一个 config/swagger-conf.yaml 文件,但收到错误 There is no extension able to load the configuration for "persistAuthorization"

---
persistAuthorization: true

如何防止 Swagger UI 在浏览器重新加载时丢失身份验证?使用 API-Platform 时如何更改 swagger-ui 参数?

编辑 - 新信息

根据 Tarun Lalwani 的评论,我找到了一种解决方法,但仍然不理想。 API-Platform 基于一些 symfony/api-platform 配置设置创建 public/bundles/apiplatform/init-swagger-ui.js 或将设置传递给它(我目前不这样做)。我预计这些会在 api_platform.swagger 下,但是在查看源代码和 api-platform configuration document 之后,我仍然不知道如何完成它(甚至是否可能)。我的解决方法是直接编辑此文件。由于这些包文件通常不会被 git 跟踪,因此需要确保手动更新。另外,我担心 api-platform 是否会修改文件。

'use strict';

window.onload = function() {
    const ui = SwaggerUIBundle({
        ...
        persistAuthorization: true, <= Line 49
        ...
    });
};

api_platform.yaml 如下,但令牌没有被持久化。

api_platform:
    mapping:
        paths: ['%kernel.project_dir%/src/Entity']
    patch_formats:
        json: ['application/merge-patch+json']
    show_webby: false
    swagger:
        api_keys:
            apiKey:
                name: Authorization
                type: header
        versions: [3]
        swagger_ui_extra_configuration:
            persistAuthorization: true,

composer.json 现在显示 "api-platform/core": "2.7.x-dev",但 composer recipes api-platform/core 仍然表示正在使用 api-platform 2.5。我试过同时使用 PHP7.4 和 PHP8。 composer depends api-platform/core 显示 2.7.x-dev,我现在 swagger_ui_extra_configuration 存在于源代码中,这很好,但仍然没有持久化令牌。

[michael@devserver api_platform_test]$ composer recipes api-platform/core
name             : api-platform/core
version          : 2.5
status           : up to date
installed recipe : https://github.com/symfony/recipes/tree/7df6db4/api-platform/core/2.5
files            :

├──config
│  ├──packages
│  │  └──api_platform.yaml
│  └──routes
│     └──api_platform.yaml
└──src
   └──Entity
      └──.gitignore
[michael@devserver api_platform_test]$ composer show --tree api-platform/core
api-platform/core dev-main Build a fully-featured hypermedia or GraphQL API in minutes!
├──doctrine/inflector ^1.0 || ^2.0
│  └──php ^7.2 || ^8.0
├──fig/link-util ^1.0
│  ├──php >=8.0.0
│  └──psr/link ^1.1.0 | ^2.0.0
│     └──php >=8.0.0
├──php >=7.1
├──psr/cache ^1.0
│  └──php >=5.3.0
├──psr/container ^1.0
│  └──php >=7.2.0
├──symfony/http-foundation ^4.4 || ^5.1
│  ├──php >=7.2.5
│  ├──symfony/deprecation-contracts ^2.1
│  │  └──php >=7.1
│  ├──symfony/polyfill-mbstring ~1.1
│  │  └──php >=7.1
│  └──symfony/polyfill-php80 ^1.15
│     └──php >=7.1
├──symfony/http-kernel ^4.4 || ^5.1
│  ├──php >=7.2.5
│  ├──psr/log ~1.0
│  │  └──php >=5.3.0
│  ├──symfony/deprecation-contracts ^2.1
│  │  └──php >=7.1
│  ├──symfony/error-handler ^4.4|^5.0
│  │  ├──php >=7.2.5
│  │  ├──psr/log ^1.0
│  │  │  └──php >=5.3.0
│  │  ├──symfony/polyfill-php80 ^1.15
│  │  │  └──php >=7.1
│  │  └──symfony/var-dumper ^4.4|^5.0
│  │     ├──php >=7.2.5
│  │     ├──symfony/polyfill-mbstring ~1.0
│  │     │  └──php >=7.1
│  │     └──symfony/polyfill-php80 ^1.15
│  │        └──php >=7.1
│  ├──symfony/event-dispatcher ^5.0
│  │  ├──php >=7.2.5
│  │  ├──symfony/deprecation-contracts ^2.1
│  │  │  └──php >=7.1
│  │  ├──symfony/event-dispatcher-contracts ^2
│  │  │  ├──php >=7.2.5
│  │  │  └──psr/event-dispatcher ^1
│  │  │     └──php >=7.2.0
│  │  └──symfony/polyfill-php80 ^1.15
│  │     └──php >=7.1
│  ├──symfony/http-client-contracts ^1.1|^2
│  │  └──php >=7.2.5
│  ├──symfony/http-foundation ^4.4|^5.0
│  │  ├──php >=7.2.5
│  │  ├──symfony/deprecation-contracts ^2.1
│  │  │  └──php >=7.1
│  │  ├──symfony/polyfill-mbstring ~1.1
│  │  │  └──php >=7.1
│  │  └──symfony/polyfill-php80 ^1.15
│  │     └──php >=7.1
│  ├──symfony/polyfill-ctype ^1.8
│  ├──symfony/polyfill-php73 ^1.9
│  │  └──php >=7.1
│  └──symfony/polyfill-php80 ^1.15
│     └──php >=7.1
├──symfony/property-access ^3.4.19 || ^4.4 || ^5.1
│  ├──php >=7.2.5
│  ├──symfony/deprecation-contracts ^2.1
│  │  └──php >=7.1
│  ├──symfony/polyfill-php80 ^1.15
│  │  └──php >=7.1
│  └──symfony/property-info ^5.2
│     ├──php >=7.2.5
│     ├──symfony/deprecation-contracts ^2.1
│     │  └──php >=7.1
│     ├──symfony/polyfill-php80 ^1.15
│     │  └──php >=7.1
│     └──symfony/string ^5.1
│        ├──php >=7.2.5
│        ├──symfony/polyfill-ctype ~1.8
│        ├──symfony/polyfill-intl-grapheme ~1.0
│        │  └──php >=7.1
│        ├──symfony/polyfill-intl-normalizer ~1.0
│        │  └──php >=7.1
│        ├──symfony/polyfill-mbstring ~1.0
│        │  └──php >=7.1
│        └──symfony/polyfill-php80 ~1.15
│           └──php >=7.1
├──symfony/property-info ^3.4 || ^4.4 || ^5.2.1
│  ├──php >=7.2.5
│  ├──symfony/deprecation-contracts ^2.1
│  │  └──php >=7.1
│  ├──symfony/polyfill-php80 ^1.15
│  │  └──php >=7.1
│  └──symfony/string ^5.1
│     ├──php >=7.2.5
│     ├──symfony/polyfill-ctype ~1.8
│     ├──symfony/polyfill-intl-grapheme ~1.0
│     │  └──php >=7.1
│     ├──symfony/polyfill-intl-normalizer ~1.0
│     │  └──php >=7.1
│     ├──symfony/polyfill-mbstring ~1.0
│     │  └──php >=7.1
│     └──symfony/polyfill-php80 ~1.15
│        └──php >=7.1
├──symfony/serializer ^4.4 || ^5.1
│  ├──php >=7.2.5
│  ├──symfony/polyfill-ctype ~1.8
│  └──symfony/polyfill-php80 ^1.15
│     └──php >=7.1
├──symfony/web-link ^4.4 || ^5.1
│  ├──php >=7.2.5
│  └──psr/link ^1.0
│     └──php >=8.0.0
└──willdurand/negotiation ^2.0.3 || ^3.0
   └──php >=7.1.0
[michael@devserver api_platform_test]$ php80 /usr/local/bin/composer show --tree api-platform/core
api-platform/core dev-main Build a fully-featured hypermedia or GraphQL API in minutes!
├──doctrine/inflector ^1.0 || ^2.0
│  └──php ^7.2 || ^8.0
├──fig/link-util ^1.0
│  ├──php >=8.0.0
│  └──psr/link ^1.1.0 | ^2.0.0
│     └──php >=8.0.0
├──php >=7.1
├──psr/cache ^1.0
│  └──php >=5.3.0
├──psr/container ^1.0
│  └──php >=7.2.0
├──symfony/http-foundation ^4.4 || ^5.1
│  ├──php >=7.2.5
│  ├──symfony/deprecation-contracts ^2.1
│  │  └──php >=7.1
│  ├──symfony/polyfill-mbstring ~1.1
│  │  └──php >=7.1
│  └──symfony/polyfill-php80 ^1.15
│     └──php >=7.1
├──symfony/http-kernel ^4.4 || ^5.1
│  ├──php >=7.2.5
│  ├──psr/log ~1.0
│  │  └──php >=5.3.0
│  ├──symfony/deprecation-contracts ^2.1
│  │  └──php >=7.1
│  ├──symfony/error-handler ^4.4|^5.0
│  │  ├──php >=7.2.5
│  │  ├──psr/log ^1.0
│  │  │  └──php >=5.3.0
│  │  ├──symfony/polyfill-php80 ^1.15
│  │  │  └──php >=7.1
│  │  └──symfony/var-dumper ^4.4|^5.0
│  │     ├──php >=7.2.5
│  │     ├──symfony/polyfill-mbstring ~1.0
│  │     │  └──php >=7.1
│  │     └──symfony/polyfill-php80 ^1.15
│  │        └──php >=7.1
│  ├──symfony/event-dispatcher ^5.0
│  │  ├──php >=7.2.5
│  │  ├──symfony/deprecation-contracts ^2.1
│  │  │  └──php >=7.1
│  │  ├──symfony/event-dispatcher-contracts ^2
│  │  │  ├──php >=7.2.5
│  │  │  └──psr/event-dispatcher ^1
│  │  │     └──php >=7.2.0
│  │  └──symfony/polyfill-php80 ^1.15
│  │     └──php >=7.1
│  ├──symfony/http-client-contracts ^1.1|^2
│  │  └──php >=7.2.5
│  ├──symfony/http-foundation ^4.4|^5.0
│  │  ├──php >=7.2.5
│  │  ├──symfony/deprecation-contracts ^2.1
│  │  │  └──php >=7.1
│  │  ├──symfony/polyfill-mbstring ~1.1
│  │  │  └──php >=7.1
│  │  └──symfony/polyfill-php80 ^1.15
│  │     └──php >=7.1
│  ├──symfony/polyfill-ctype ^1.8
│  ├──symfony/polyfill-php73 ^1.9
│  │  └──php >=7.1
│  └──symfony/polyfill-php80 ^1.15
│     └──php >=7.1
├──symfony/property-access ^3.4.19 || ^4.4 || ^5.1
│  ├──php >=7.2.5
│  ├──symfony/deprecation-contracts ^2.1
│  │  └──php >=7.1
│  ├──symfony/polyfill-php80 ^1.15
│  │  └──php >=7.1
│  └──symfony/property-info ^5.2
│     ├──php >=7.2.5
│     ├──symfony/deprecation-contracts ^2.1
│     │  └──php >=7.1
│     ├──symfony/polyfill-php80 ^1.15
│     │  └──php >=7.1
│     └──symfony/string ^5.1
│        ├──php >=7.2.5
│        ├──symfony/polyfill-ctype ~1.8
│        ├──symfony/polyfill-intl-grapheme ~1.0
│        │  └──php >=7.1
│        ├──symfony/polyfill-intl-normalizer ~1.0
│        │  └──php >=7.1
│        ├──symfony/polyfill-mbstring ~1.0
│        │  └──php >=7.1
│        └──symfony/polyfill-php80 ~1.15
│           └──php >=7.1
├──symfony/property-info ^3.4 || ^4.4 || ^5.2.1
│  ├──php >=7.2.5
│  ├──symfony/deprecation-contracts ^2.1
│  │  └──php >=7.1
│  ├──symfony/polyfill-php80 ^1.15
│  │  └──php >=7.1
│  └──symfony/string ^5.1
│     ├──php >=7.2.5
│     ├──symfony/polyfill-ctype ~1.8
│     ├──symfony/polyfill-intl-grapheme ~1.0
│     │  └──php >=7.1
│     ├──symfony/polyfill-intl-normalizer ~1.0
│     │  └──php >=7.1
│     ├──symfony/polyfill-mbstring ~1.0
│     │  └──php >=7.1
│     └──symfony/polyfill-php80 ~1.15
│        └──php >=7.1
├──symfony/serializer ^4.4 || ^5.1
│  ├──php >=7.2.5
│  ├──symfony/polyfill-ctype ~1.8
│  └──symfony/polyfill-php80 ^1.15
│     └──php >=7.1
├──symfony/web-link ^4.4 || ^5.1
│  ├──php >=7.2.5
│  └──psr/link ^1.0
│     └──php >=8.0.0
└──willdurand/negotiation ^2.0.3 || ^3.0
   └──php >=7.1.0
[michael@devserver api_platform_test]$ php80 /usr/local/bin/composer recipes api-platform/core
name             : api-platform/core
version          : 2.5
status           : up to date
installed recipe : https://github.com/symfony/recipes/tree/7df6db4/api-platform/core/2.5
files            :

├──config
│  ├──packages
│  │  └──api_platform.yaml
│  └──routes
│     └──api_platform.yaml
└──src
   └──Entity
      └──.gitignore
[michael@devserver api_platform_test]$

1 个答案:

答案 0 :(得分:2)

您现在可以使用 dev 版本

composer require "api-platform/core:2.7.x-dev"

然后使用作为以下拉取请求的一部分添加的 swagger_ui_extra_configuration

https://github.com/api-platform/core/pull/3731