gpt4 book ai didi

node.js - Nodejs Swagger 无法向请求添加授权 header

转载 作者:行者123 更新时间:2023-12-05 08:49:18 25 4
gpt4 key购买 nike

我正在尝试使用 Node.js Express 服务器向 Swagger UI 添加授权 header 。请求需要将 x-auth-token 作为 API 的 header 之一才能获得身份验证。下面是我的 app.js 代码:

const swaggerDefinition = {
info: {
title: 'MySQL Registration Swagger API',
version: '1.0.0',
description: 'Endpoints to test the user registration routes',
},
host: 'localhost:8000',
basePath: '/api/v1',
securityDefinitions: {
bearerAuth: {
type: 'apiKey',
name: 'x-auth-token',
scheme: 'bearer',
in: 'header',
},
},
};

const options = {
// import swaggerDefinitions
swaggerDefinition,
// path to the API docs
apis: ['dist-server/docs/*.yaml'],
};
// initialize swagger-jsdoc
const swaggerSpec = swaggerJSDoc(options);


// use swagger-Ui-express for your app documentation endpoint
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));

但这并不是在 Swagger UI 中向请求添加 header 。如何解决这个问题?

最佳答案

将以下键添加到您的 swaggerDefinition:

  security: [ { bearerAuth: [] } ],

关于node.js - Nodejs Swagger 无法向请求添加授权 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64144328/

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