【问题标题】:Adding authentication documentation to Swagger in NelmioApiDocBundle在 NelmioApiDocBundle 中向 Swagger 添加身份验证文档
【发布时间】:2021-04-05 06:11:10
【问题描述】:
我正在使用 Symfony (5) 与 NelmioApiDocBundle (4.0) 和 LexikJWTAuthenticationBundle 来创建一个带有 Swagger 的 API。
我已经设置了端点和身份验证,一切都按预期工作。
我有带有身份验证的功能齐全的 API,有一个文档,我可以成功生成 Open API 规范。
规范中缺少一件事:“身份验证端点”,我找不到将其添加到生成的规范和文档的方法(覆盖整个身份验证除外)
因为我使用 Symfony 安全层和防火墙,并且它与 LexikJWTAuthenticationBundle 集成,所以没有地方设置 Swagger 注释,而且似乎 bundle 本身不处理生成“安全部分”
前段时间我使用了一个 ApiPlatform 并且在那里它是由一个“装饰器”完成的
任何人都知道有没有办法(注释?)来生成文档的安全部分,或者我必须从头开始创建身份验证保护(?)
【问题讨论】:
标签:
symfony
authentication
swagger
lexikjwtauthbundle
nelmioapidocbundle
【解决方案1】:
没关系。分钟我发布它我找到了一个解决方案。
在 packages/nelmio_bundle_api.yaml 中,您可以配置不是由注释创建的附加文档(swagger 规范)。
基本上,您必须添加一个指向您的身份验证路由的新路径(在我的情况下为 /api/login_check),并在“组件”部分定义凭据和令牌对象。
所以有了 symfony 授权(这只是 yaml 文件的安全部分):
nelmio_api_doc:
documentation:
paths:
/api/login_check:
post:
tags:
- Token
operationId: postCredentialsItem
summary: Get JWT token to login.
requestBody:
description: Create new JWT Token
content:
application/json:
schema:
$ref: '#/components/schemas/Credentials'
responses:
'200':
description: Get JWT token
content:
application/json:
schema:
$ref: '#/components/schemas/Token'
components:
schemas:
Token:
type: object
properties:
token:
type: string
readOnly: true
Credentials:
type: object
properties:
username:
type: string
password:
type: string
securitySchemes:
bearerAuth:
type: apiKey
scheme: bearer
bearerFormat: JWT
security:
- bearerAuth: []