[英]Nodejs Swagger unable to add authorization header to requests
我正在嘗試使用 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: [] } ],
如果您在 swagger 文件中使用 Node 和 Express js,例如在 swagger.json 中添加這樣的授權標頭
"securityDefinitions": {
"AuthToken": {
"type": "apiKey",
"name": "auth-token",
"in": "header",
"description": "The token for authentication"
}
},
"security": [
{
"AuthToken": []
}
],
注意:安全屬性在 securityDefinitions 對象之外。
請投票此答案以幫助他人
這將在 ExpressJS 應用程序中為您提供幫助。
add this into swagger.json file
"basePath": "/",
"securityDefinitions": {
"Authorization": {
"type": "apiKey",
"name": "authorization",
"in": "header",
"description": "Authentication token"
}
},
add this line into "paths", which API you want to use the token.
"security": [
{
"Authorization": []
}
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.