如何使用JWT令牌授权Swagger jsdoc?

时间:2018-06-07 08:44:50

标签: node.js jwt swagger swagger-ui swagger-2.0

我定义了我的招摇定义,如下所示:

const swaggerDefinition = {
  info: {
    title: 'Node Swagger API',
    version: '1.0.0',
    description: 'Describing a RESTful API with Swagger',
  },
  host: 'localhost:8000',
  basePath: '/',
  securityDefinitions: {
    JWT: {
      type: 'apiKey',
      description: 'JWT authorization of an API',
      name: 'Authorization',
      in: 'header',
    },
  },
};

但我无法在swagger UI中获得任何授权,甚至无法在swagger.json中获得任何授权

    info: {
       title: "Node Swagger API",
       version: "1.0.0",
       description: "Describing a RESTful API with Swagger"
    },
    host: "localhost:8000",
    basePath: "/",
    securityDefinitions: { },
    swagger: "2.0",

在swagger.json文件中,securitydefinitions块仍然为空,而我已经在server.js中的swagger定义中添加了

Swagger UI

任何人都可以建议如何启用授权,或者如果我在" securitydefinitions"中错误地使用了模式。 ?

3 个答案:

答案 0 :(得分:2)

只是要明确一件事: 安全定义定义了api操作的安全性,而不是文档本身。

docs说明以下内容:

  

API使用的所有安全方案必须在global components / securitySchemes部分中定义。此部分包含命名安全方案的列表,其中每个方案可以是

...

  

在securitySchemes部分中定义安全方案后,您可以通过分别在根级别或操作级别添加安全性部分将它们应用于整个API或单个操作。

... I.E您需要全局应用定义(如果这是api的工作方式)或安全性下的操作"标记"

示例:

paths:
  /billing_info:
    get:
      summary: Gets the account billing info
      security:
        - OAuth2: [admin]   # Use OAuth with a different scope
      responses:
        '200':
          description: OK
        '401':
          description: Not authenticated
        '403':
          description: Access token does not have the required scope
  /ping:
    get:
      summary: Checks if the server is running
      security: []   # No security
      responses:
        '200':
          description: Server is up and running
        default:
          description: Something is wrong

答案 1 :(得分:2)

在计算node_modules时获得解决方案。

只需更新到最新版本的swagger-jsdoc即可。它会做到这一点。

答案 2 :(得分:0)

要执行此操作,您需要将openapi属性添加到swaggerDefinition对象中。

this Github issue中,您可以看到通过添加openapi: 3.0.1,jsdoc现在可以识别安全性定义。

const swaggerOptions = {
  swaggerDefinition: {
    openapi: '3.0.1', // YOU NEED THIS
    info: {
      title: 'Your API title',
      version: '1.0.0',
      description: 'Your API description'
    },
    basePath: '/',
    components: {
      securitySchemes: {
        bearerAuth: {
          type: 'http',
          scheme: 'bearer',
          bearerFormat: 'JWT',
        }
      }
    },
    security: [{
      bearerAuth: []
    }]
  },
  apis: ['/some/path.js|yml'],
};

如果您不想使用全局安全性定义,则需要从swaggerDefinition

删除以下内容
security: [{
  bearerAuth: []
}]

您现在可以将其添加到单个API请求中:

/**
 * @swagger
 * /users:
 *  get:
 *    security:              # <--- ADD THIS
 *      - bearerAuth: []     # <--- ADD THIS
 *    tags:
 *      - Users
 *    description: Returns a single person based on their JWT token
 *    produces:
 *      - application/json
 *    responses:
 *      200:
 *        description: A single person
 *        schema:
 *            $ref: '#/definitions/PersonSimple'
 */
router.get('/users', passport.authenticate('jwt', {session: false}), (req, res, next) => {

    /**
    * Just using the `passport-jwt` middleware as an example here.
    * If the JWT is valid, it attaches the user from the database
    * to the request object
    */
    res.status(200).json({ success: true, user: req.user });
});
相关问题