虽然我的最终目标是防止Swagger UI在浏览器重新加载时丢失身份验证,但我相信我可能已经找到了一个解决方案,假设在使用api-platform时可以更改swagger-ui参数,并在本文的末尾对此进行了描述。
REST API使用Symfony、API平台和使用JWT进行身份验证,文档由swagger-ui提供。在swagger-ui页面上,提交apiKey后,未来的请求会将其包含在标头中,但是,如果浏览器被刷新,授权令牌将丢失。x1c 0d1x
关于这个主题已经有了一些讨论,主要是关于这个github post和这个stackoverflow post,并且普遍的共识似乎是swagger-ui没有“官方”的方式来持久化令牌。
总的来说,Swagger UI不存储令牌,而且可能是故意的。没有开关来启用这个功能,但看起来有一些小事情可以通过cookie、本地存储、indexdb等来记住令牌,当页面重新加载时,重新填充令牌。
但是,swagger configuration documentation似乎有一个Authorization参数,允许在浏览器刷新时保留授权数据。
- 参数名称:持久授权
- Docker变量:持久授权
- 说明:布尔值=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平台时更改swagger-ui参数?
编辑-新信息
基于Tarun Lalwani的评论,我找到了一个解决方案,但是,它仍然不理想。API-Platform要么创建public/bundles/apiplatform/init-swagger-ui.js
,要么将设置传递给它(我目前不知道)基于symfony/api平台的一些配置设置。我希望这些设置在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和PHP 8。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]$
2条答案
按热度按时间pwuypxnk1#
现在可以使用
dev
版本然后使用
swagger_ui_extra_configuration
,它是作为以下拉取请求的一部分添加的https://github.com/api-platform/core/pull/3731
qni6mghb2#
您可以像这样更新您的配置,以便在重新加载期间保持jwt标记:
配置文件名: