处理使用1时调用的多个Swagger SecurityHandler定义?

时间:2018-01-18 19:25:10

标签: node.js express swagger swagger-2.0

我已将Swagger 2.0配置为具有多个可选的安全定义。当用户调用和支持多个定义并使用其中一个定义的端点时,将调用所有已配置的处理程序 如何处理没有参数的处理程序的请求,并确保使用参数调用至少1个安全处理程序?

我在swagger.yaml

中有两个单独的API密钥定义
securityDefinitions:
  apiKeyQuery:
    type: "apiKey"
    name: "api_key"
    in: "query"
  apiKeyHeader:
    type: "apiKey"
    name: "API-KEY"
    in: "header"

我希望其中任何一个都可以工作,所以这是我的端点安全性定义:

security:
  - apiKeyQuery: []
  - apiKeyHeader: []

我的app.js有两个处理程序:

appRoot: __dirname,
swaggerSecurityHandlers: {
  apiKeyQuery: function(req, res, next) { console.log('Query Called!');},
  apiKeyHeader: function(req, res, next) {console.log('Header Called!');}
}

预期:

Input: curl -X GET --header 'Accept: application/json' 'http://localhost:3000/api/v1/users?api_key=asdfg'
Output: Query Called!

实际值:

Input: curl -X GET --header 'Accept: application/json' 'http://localhost:3000/api/v1/users?api_key=asdfg'
Output: Query Called!  Header Called!

似乎两个安全处理程序请求都是异步的,并且swagger首先使用哪个回调触发来确定下一步。

1 个答案:

答案 0 :(得分:0)

似乎swagger调用这两个函数,但是因为我使用了'OR'(docs:https://swagger.io/docs/specification/2-0/authentication/#multiple)如果接受了它,那么它应该转移到控制器代码。

相关问题