NodeJS 在 Swagger 文档中添加授权按钮
我需要能够将以下按钮添加到 Swagger 的 UI 界面,以便测试人员可以添加“Bearer token”标头并测试 api。
我的swagger的选项定义是:
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"],
};
我的端点如下:
/**
* @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');
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用的是 OAS v3 吗?您的声明中有错误,例如
securityDefinitions
现在称为securitySchemes
并且位于components
内。请检查 https://swagger.io/docs/specification/authentication/
修复架构时, ,然后向您的路径添加一个
security
属性,以使用安全模式保护它,这样您将获得绿色的Authorize
按钮。Are you using OAS v3? You have errors in your declarations, for example
securityDefinitions
is now calledsecuritySchemes
and it is insidecomponents
.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 greenAuthorize
button.对于 Node 项目中的 swagger-ui-express 4.6.3,
*注意:bearerAuth 安全参数必须相同
2. 在 swagger.json 文件中的每个路径或 API 中添加安全参数
For swagger-ui-express 4.6.3 in Node project
*Note : bearerAuth must be same in security parameter
2.Add security parameter in every path or API in swagger.json file