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

我正在尝试使用 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/

相关文章:

r - 制作数据框每一列的向量并返回列表中的向量

css - 防止在显示 : flex 时出现空白

reactjs - @apollo/client error-React Hook "useQuer

arrays - 使用 jq 将两个 JSON 数组合并到一个文件中

javascript - 使用 React.js 时未定义 Web Speech API Speec

r - 如何在保留 R 中的某些变量的同时将长整形为宽

python - 排序函数中 x%2==0 的排序列表给出了我不希望的结果

kubernetes - 使用 Persistent Volume Claim 时是否必须显式创建

angular - 如何将参数传递给 Angular 中的模态

python - 如何划分字典值?