如何在 Swagger 规范中使用“授权:持有者

2023-11-26

我试图传达身份验证/安全方案需要设置标头,如下所示:

Authorization: Bearer <token>

这就是我基于的招摇文档:

securityDefinitions:
  APIKey:
    type: apiKey
    name: Authorization
    in: header
security:
  - APIKey: []

也许这可以帮助:

swagger: '2.0'
info:
  version: 1.0.0
  title: Bearer auth example
  description: >
    An example for how to use Bearer Auth with OpenAPI / Swagger 2.0.

host: basic-auth-server.herokuapp.com
schemes:
  - http
  - https
securityDefinitions:
  Bearer:
    type: apiKey
    name: Authorization
    in: header
    description: >-
      Enter the token with the `Bearer: ` prefix, e.g. "Bearer abcde12345".
paths:
  /:
    get:
      security:
        - Bearer: []
      responses:
        '200':
          description: 'Will send `Authenticated`'
        '403': 
          description: 'You do not have necessary permissions for the resource'

您可以将其复制粘贴到https://editor.swagger.io查看结果。

Swagger Editor Web 中还有几个具有更复杂安全配置的示例可以为您提供帮助。

重要的:在此示例中,API 使用者必须包含“Bearer”前缀作为令牌值的一部分。例如,当使用Swagger UI的“授权”对话框时,您需要输入Bearer your_token而不仅仅是your_token.

Swagger UI's Authorization dialog

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Swagger 规范中使用“授权:持有者 ” 的相关文章

随机推荐