gpt4 book ai didi

node.js - 如何在 Swagger 中添加 API key

转载 作者:太空宇宙 更新时间:2023-11-04 03:06:38 27 4
gpt4 key购买 nike

我尝试了几种方法来向Swagger(express swagger)添加基本的API key 授权。以下是我的 list 中的相关信息。

{
...
"main": "app.js",
"dependencies": {
"express": "^4.12.3",
"swagger-express-mw": "^0.1.0"
},
...
}

我相信我已经在 YAML 配置中注册了安全定义。

swagger: "2.0"
info:
...

# Set up security for the API
securityDefinitions:
defaultApiKey:
type: apiKey
in: header
name: defaultApiKey

security:
- defaultApiKey: []

为了完成此配置,我认为我只需要在 SwaggerExpress.create() 函数中注册中间件即可。下面是我目前的整个 app.js。

'use strict';

var SwaggerExpress = require('swagger-express-mw');
var app = require('express')();
module.exports = app; // for testing

var config = {
appRoot: __dirname // required config
};

SwaggerExpress.create(config, function(err, swaggerExpress) {
if (err) { throw err; }

// Serve the Swagger documents and SwaggerUi
app.use(swaggerExpress.runner.swaggerTools.swaggerUi());

// Install security
app.use(swaggerExpress.swaggerSecurity({
defaultApiKey: function(req, def, scopes, callback) {
console.log("hit");
}
}));

// install middleware
swaggerExpress.register(app);

var port = process.env.PORT || 10010;
app.listen(port);
});

当我尝试使用它时,我看到以下内容:

Error initializing middleware
TypeError: swaggerExpress.swaggerSecurity is not a function

我尝试了几种不同的方式进行配置,但没有成功。我是否在 YAML 中正确定义了 API key ?如果是,我应该如何在 app.js 文件中注册 API key 处理程序?

最佳答案

有点晚了,但供以后引用。使用 swagger-express-mw,您可以这样做(它对我有用):

config: {
appRoot: __dirname,
swaggerSecurityHandlers: {
defaultApiKey: function(req, authOrSecDef, scopesOrApiKey, callback) {
// code here
}
}
}

我希望这对某人有帮助。

关于node.js - 如何在 Swagger 中添加 API key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39078919/

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