为什么从 Swagger UI 发送的请求中缺少授权 header?
Why is the Authorization header missing in requests sent from Swagger UI?
我想向我的 Node.js API 添加一个文档,为此我有一个 YAML 文件,我在其中放置了我的定义,swagger 文档位于 localhost:5000/api-doc 并且工作正常。
现在我必须添加 Bearer 授权但 Swagger 具有以下定义:
swagger: "2.0"
info:
version: 1.0.0
title: My API documentation
description: >
My API documentation
host: localhost:5000
basePath: "/v1"
schemes:
- http
securityDefinitions:
Bearer:
type: apiKey
description: "Value: Bearer "
name: Authorization
in: header
paths:
/users:
get:
responses:
"200":
description: "Will send `Authenticated`"
"403":
description: "You do not have necessary permissions for the resource"
测试请求时(我点击了右上角的“授权”按钮并输入了我的令牌)我收到以下错误:
"error": "Authorization header not found.
为什么 Authorization
header 没有包含在请求中?
仅 securityDefinitions
是不够的,您还需要在根级别或操作级别添加 security
密钥才能真正应用安全性。
security:
- Bearer: []
扩展@helen 的答案,因为我无法编辑它,这个答案是为使用 Symfony 的人准备的如果您将 NelmioApiDocBundle 与 Symfony 一起使用,
您必须在以下位置添加配置
config/packages/nelmio_api_doc.yaml
所以它看起来像下面这样:
documentation:
info:
title: App name
description: This is an awesome app!
version: 1.0.0
securityDefinitions:
Bearer:
type: apiKey
description: 'Value: Bearer {jwt}'
name: Authorization
in: header
security:
- Bearer: []
我想向我的 Node.js API 添加一个文档,为此我有一个 YAML 文件,我在其中放置了我的定义,swagger 文档位于 localhost:5000/api-doc 并且工作正常。
现在我必须添加 Bearer 授权但 Swagger 具有以下定义:
swagger: "2.0"
info:
version: 1.0.0
title: My API documentation
description: >
My API documentation
host: localhost:5000
basePath: "/v1"
schemes:
- http
securityDefinitions:
Bearer:
type: apiKey
description: "Value: Bearer "
name: Authorization
in: header
paths:
/users:
get:
responses:
"200":
description: "Will send `Authenticated`"
"403":
description: "You do not have necessary permissions for the resource"
测试请求时(我点击了右上角的“授权”按钮并输入了我的令牌)我收到以下错误:
"error": "Authorization header not found.
为什么 Authorization
header 没有包含在请求中?
securityDefinitions
是不够的,您还需要在根级别或操作级别添加 security
密钥才能真正应用安全性。
security:
- Bearer: []
扩展@helen 的答案,因为我无法编辑它,这个答案是为使用 Symfony 的人准备的如果您将 NelmioApiDocBundle 与 Symfony 一起使用,
您必须在以下位置添加配置
config/packages/nelmio_api_doc.yaml
所以它看起来像下面这样:
documentation:
info:
title: App name
description: This is an awesome app!
version: 1.0.0
securityDefinitions:
Bearer:
type: apiKey
description: 'Value: Bearer {jwt}'
name: Authorization
in: header
security:
- Bearer: []