对Swagger安全方案使用API密钥和秘密
问题描述:
Swagger支持安全性为api key,但似乎仅限于单个参数。对Swagger安全方案使用API密钥和秘密
有没有一种方法来定义一组参数(密钥和秘密),这些参数在请求中作为参数预期?
或者是跳过安全方案的唯一方法,只需将这些参数添加到每个请求中?
答
是的,OpenAPI(Swagger)2.0和3.0允许您定义多个安全定义并将操作标记为需要多个证券,例如一对API密钥。
在以下示例中,我定义了两个API密钥Key
和SecretKey
,这两个密钥都应出现在每个请求的标头中以便进行身份验证。
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
这意味着端点预计要么Key
或SecretKey
,但不能同时使用。
你试过这个吗? http://stackoverflow.com/questions/26742521/sending-dynamic-custom-headers-in-swagger-ui-try-outs – suresh2
@ suresh2这将工作,但正在寻找一个答案,如果可以做到这一点安全计划。据我所知,这只是一个必需的参数。哪些可能起作用,只是想尽可能使用安全方案。 –