node.js - Nodejs Swagger 无法向请求添加授权标头
问题描述
我正在尝试使用 Node.js Express 服务器向 Swagger UI 添加授权标头。请求需要x-auth-token
作为 API 的标头之一才能获得身份验证。下面是我的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 中的请求中。如何解决这个问题?
解决方案
将以下密钥添加到您的swaggerDefinition
:
security: [ { bearerAuth: [] } ],
推荐阅读
- python - Kivy screenmanager:超时后切换屏幕与信号
- spring-boot - 动态设置缓存 expireAfterWrite 属性 - Caffeine 和 Spring WebFlux
- javascript - 如何在 videojs 中使用徽标插件
- delphi - 如何在 Pascal 中初始化和填充 TWideStringDynArray?
- java - 使用 mapr 集群提交 mapreduce 作业时遇到问题
- sqlite - 无法在 Windows 上通过 TDM-GCC 使用 cgo 编译 Go 应用程序
- javafx - 如何在arcGIS JAVA SDK中围绕指定航点创建1公里的圆圈或缓冲区?
- scikit-learn - 为什么 sklearn 的 kNN 分类器运行速度如此之快,而我的训练样本和测试样本的数量很大
- sql-server - 将 datetimeoffset 转换为 varchar
- instagram - instagram api 授权失败?