admin管理员组文章数量:1310237
I need to be able to add the following button to Swagger's UI interface so that the testers can add the "Bearer token" header and test the apis.
My swagger's option definition is:
module.exports = {
definition: {
openapi: "3.0.3",
info: {
title: "APIs",
version: "1.0.0",
},
servers: [
{
url: `http://localhost:${process.env.PORT}`
}
],
securityDefinitions: {
bearerAuth: {
type: 'apiKey',
name: 'Authorization',
scheme: 'bearer',
in: 'header',
},
}
},
apis: ["./routes/*.js", "app.js"],
};
and my endpoint is as follows:
/**
* @swagger
* /api/users/test:
* post:
* security:
* - Bearer: []
* summary: test authorization
* tags: [User]
* description: use to test authorization JWT
* responses:
* '200':
* description: success
* '500':
* description: Internal server error
*/
router.post('/test', verifyJWT(), async (req, res) => {
res.send('hi');
})
I need to be able to add the following button to Swagger's UI interface so that the testers can add the "Bearer token" header and test the apis.
My swagger's option definition is:
module.exports = {
definition: {
openapi: "3.0.3",
info: {
title: "APIs",
version: "1.0.0",
},
servers: [
{
url: `http://localhost:${process.env.PORT}`
}
],
securityDefinitions: {
bearerAuth: {
type: 'apiKey',
name: 'Authorization',
scheme: 'bearer',
in: 'header',
},
}
},
apis: ["./routes/*.js", "app.js"],
};
and my endpoint is as follows:
/**
* @swagger
* /api/users/test:
* post:
* security:
* - Bearer: []
* summary: test authorization
* tags: [User]
* description: use to test authorization JWT
* responses:
* '200':
* description: success
* '500':
* description: Internal server error
*/
router.post('/test', verifyJWT(), async (req, res) => {
res.send('hi');
})
Share
Improve this question
asked Mar 3, 2022 at 8:24
Nour MawlaNour Mawla
3511 gold badge3 silver badges16 bronze badges
2 Answers
Reset to default 5Are you using OAS v3? You have errors in your declarations, for example securityDefinitions
is now called securitySchemes
and it is inside ponents
.
Check https://swagger.io/docs/specification/authentication/
When you fix your schema, then you add a security
property to your path to protect it with a security schema so that you'll get the green Authorize
button.
ponents:
securitySchemes:
BearerAuth:
type: http
scheme: bearer
paths:
/api/users/test:
post:
security:
- BearerAuth: []
For swagger-ui-express 4.6.3 in Node project
- Add securityDefinitions in swagger.json file as below.
*Note : bearerAuth must be same in security parameter
2.Add security parameter in every path or API in swagger.json file
"securityDefinitions": {
"bearerAuth": {
"type": "apiKey",
"in": "header",
"name": "Authorization",
"description": "Bearer token to access these api endpoints",
"scheme": "bearer"
}
}
"security": [
{
"bearerAuth": []
}
]
本文标签: javascriptNodeJS add the authorization button to Swagger DocumentationStack Overflow
版权声明:本文标题:javascript - NodeJS add the authorization button to Swagger Documentation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741837558a2400308.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论