【问题标题】:Using an API Key & Secret for Swagger Security Scheme为 Swagger 安全方案使用 API 密钥和秘密
【发布时间】:2015-04-23 08:04:27
【问题描述】:

Swagger 支持api key 的安全性,但这似乎仅限于单个参数。

有没有办法在请求中定义一组参数(密钥和秘密)?

或者是跳过安全方案的唯一方法,只是将这些参数添加到每个请求中?

【问题讨论】:

  • @suresh2 这会起作用,但正在寻找是否可以将其作为安全方案的答案。据我所知,这只是一个必需的参数。这可能有效,如果可能的话,只想使用安全方案。

标签: swagger


【解决方案1】:

是的,OpenAPI (Swagger) 2.0 和 3.0 允许您定义多个安全定义并将一个操作标记为需要多个安全性,例如一对 API 密钥。

在下面的示例中,我定义了两个 API 密钥,KeySecretKey,这两个密钥都应该出现在每个请求的标头中才能通过身份验证。

swagger: '2.0'
info:
  version: 0.0.0
  title: Simple API
securityDefinitions:
  key:
    type: apiKey
    in: header
    name: Key
  secret_key:
    type: apiKey
    in: header
    name: SecretKey

# Or if you use OpenAPI 3.0:
# components:
#   securitySchemes:
#     key:
#       type: apiKey
#       in: header
#       name: Key
#     secret_key:
#       type: apiKey
#       in: header
#       name: SecretKey

paths:
  /:
    get:
      # Both 'Key' and 'SecretKey' must be used together
      security:
        - key: []
          secret_key: []
      responses:
        200:
          description: OK

请注意,这与

不同
      security:
        - key: []
        - secret_key: []  # <-- Note the leading dash here

这意味着端点需要KeySecretKey,但不能同时使用两者。

【讨论】:

  • 谢谢!完全错过了can have multiple security schemes 以及规范中的实际含义。
  • 顺便说一句,Swagger 编辑器现在支持多种证券。问题已解决。
  • 那么你在哪里设置秘密 API 密钥?
  • @ChrisRich 在swagger-ui.html 文件中,您的API URL 旁边的标题栏中会出现一个按钮,名称为“授权”。单击该按钮以使用您的 API 密钥登录,并从现在开始使用该密钥发送请求。
  • 问题已解决,github issue 已关闭;) 看来我们可以使用多个键
猜你喜欢
  • 2012-07-18
  • 2014-02-27
  • 2014-01-05
  • 1970-01-01
  • 2018-07-04
  • 2014-05-25
  • 1970-01-01
  • 2020-04-15
  • 2019-11-29
相关资源
最近更新 更多