我正在尝试使用 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: [] } ],
https://stackoverflow.com/questions/64144328/