【问题标题】:Nodejs Swagger unable to add authorization header to requestsNodejs Swagger 无法向请求添加授权标头
【发布时间】:2020-09-30 19:15:25
【问题描述】:

我正在尝试使用 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 中的请求中。如何解决这个问题?

【问题讨论】:

    标签: node.js swagger swagger-ui


    【解决方案1】:

    将以下密钥添加到您的swaggerDefinition

      security: [ { bearerAuth: [] } ],
    

    【讨论】:

      猜你喜欢
      • 2014-12-28
      • 2020-03-29
      • 2015-05-17
      • 2020-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-04
      相关资源
      最近更新 更多