NodeJS 在 Swagger 文档中添加授权按钮

发布于 2025-01-11 16:46:29 字数 1267 浏览 0 评论 0原文

我需要能够将以下按钮添加到 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.

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑凤梨 2025-01-18 16:46:29

您使用的是 OAS v3 吗?您的声明中有错误,例如 securityDefinitions 现在称为 securitySchemes 并且位于 components 内。

请检查 https://swagger.io/docs/specification/authentication/

修复架构时, ,然后向您的路径添加一个 security 属性,以使用安全模式保护它,这样您将获得绿色的 Authorize 按钮。

components:
  securitySchemes:

    BearerAuth:
      type: http
      scheme: bearer
paths:
   /api/users/test:
     post:
       security: 
         - BearerAuth: []

Are you using OAS v3? You have errors in your declarations, for example securityDefinitions is now called securitySchemes and it is inside components.

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.

components:
  securitySchemes:

    BearerAuth:
      type: http
      scheme: bearer
paths:
   /api/users/test:
     post:
       security: 
         - BearerAuth: []
乜一 2025-01-18 16:46:29

对于 Node 项目中的 swagger-ui-express 4.6.3,

  1. 在 swagger.json 文件中添加 securityDefinitions,如下所示。

*注意:bearerAuth 安全参数必须相同

2. 在 swagger.json 文件中的每个路径或 API 中添加安全参数

 "securityDefinitions": {
  "bearerAuth": {
    "type": "apiKey",
    "in": "header",
    "name": "Authorization",
    "description": "Bearer token to access these api endpoints",
    "scheme": "bearer"
  }
}

"security": [
      {
        "bearerAuth": []
      }
    ]

For swagger-ui-express 4.6.3 in Node project

  1. 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": []
      }
    ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文