2015-04-23 180 views
12

Swagger支持安全性爲api key,但似乎僅限於單個參數。對Swagger安全方案使用API​​密鑰和祕密

有沒有一種方法來定義一組參數(密鑰和祕密),這些參數在請求中作爲參數預期?

或者是跳過安全方案的唯一方法,只需將這些參數添加到每個請求中?

+0

你試過這個嗎? http://stackoverflow.com/questions/26742521/sending-dynamic-custom-headers-in-swagger-ui-try-outs – suresh2

+0

@ suresh2這將工作,但正在尋找一個答案,如果可以做到這一點安全計劃。據我所知,這只是一個必需的參數。哪些可能起作用,只是想盡可能使用安全方案。 –

回答

16

是的,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,但不能同時使用。

+0

謝謝!完全錯過了'可以有多種安全方案'以及規範中的實際內容。 –

+1

順便說一句,Swagger Editor現在支持多種證券。問題已解決。 – Mohsen

+10

那麼你在哪裏設置了祕密API密鑰? – ChrisRich