使用Swigger安全方案的API密钥和秘密

问题描述 投票:14回答:1

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

有没有办法定义一组预期作为请求中的参数的参数(密钥和秘密)?

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

swagger
1个回答
19
投票

是的,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
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,但不是两者。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.