gpt4 book ai didi

node.js - 处理使用 1 时调用的多个 Swagger SecurityHandler 定义?

转载 作者:太空宇宙 更新时间:2023-11-03 22:24:41 24 4
gpt4 key购买 nike

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

我在 swagger.yaml 中有 2 个单独的 API key 定义

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 使用首先触发的回调来确定下一步。

最佳答案

似乎 swagger 调用了这两个函数,但由于我使用了“OR”(文档:https://swagger.io/docs/specification/2-0/authentication/#multiple),如果一个函数接受,那么它应该移动到 Controller 代码。

关于node.js - 处理使用 1 时调用的多个 Swagger SecurityHandler 定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48328894/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com