当前位置: 首页 > 知识库问答 >
问题:

如何在nodejs中的Swagger UI(swagger.json)头中表示自定义令牌

别浩漫
2023-03-14

我正在用ExpressJs创建一个Restful服务器。我已经集成了swagger和jsdoc。以下是相关文件。下面(header.png)是我期望我的头在swagger UI中的样子。但是,当我打开我的招摇过市UI时(http://localhost:3000/api-docs/),我无法在标题中看到令牌标记(令牌和身份验证)。

{
    "swagger": "2.0",
    "info": {
        "version": "1.0.0",
        "title": "Viswa API"
    },
    "host": "localhost:3000",
    "basePath": "/api",
    "tags": [{
        "name": "Customers",
        "description": "API for customers in the system"
    }],
    "schemes": [
        "http"
    ],
    "consumes": [
        "application/json"
    ],
    "produces": [
        "application/json"
    ],
    "securityDefinitions": {
        "Bearer": {
            "type": "apiKey",
            "name": "Authorization",
            "in": "header"
        },
        "JWT": {
            "type": "apiKey",
            "name": "token",
            "in": "header"
        }
    },
    "paths": {
        "/customer": {
            "post": {
                "tags": [
                    "Customers"
                ],
                "description": "Create new customer in system",
                "parameters": [{
                    "name": "customer",
                    "in": "body",
                    "description": "Customer that we want to create",
                    "schema": {
                        "$ref": "#/definitions/Customer"
                    }
                }],
                "produces": [
                    "application/json"
                ],
                "responses": {
                    "201": {
                        "description": "New customer is created",
                        "schema": {
                            "$ref": "#/definitions/Customer"
                        }
                    }
                }
            }
        }
    },
    "definitions": {
        "Customer": {
            "required": [
                "email"
            ],
            "properties": {
                "customer_name": {
                    "type": "string"
                },
                "customer_email": {
                    "type": "string"
                }
            }
        }
    }
}

app.route

var apiRoutes = express.Router();
app.use('/api', apiRoutes);
// swagger definition
var swaggerUi = require('swagger-ui-express'),
swaggerDocument = require('../swagger.json');

app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.use('/api/v1', apiRoutes);

当前Swagger用户界面:

共有3个答案

卜飞鸣
2023-03-14

您可以在路径定义中定义标头参数。喜欢这个

 "paths": {
    "/customer": {
                parameters: [{ name: "Authorization", in: "header", type: "string", description: "auth token" }]

                }
        }
顾涵衍
2023-03-14
"security": [ { "Bearer": [] } ], 
"paths": {
        "/customer": {
            "post": {
                "security": [ { "Bearer": [] } ],
                "tags": [
                    "Customers"
                ],
                "description": "Create new customer in system",
                "parameters": [{
                    "name": "customer",
                    "in": "body",
                    "description": "Customer that we want to create",
                    "schema": {
                        "$ref": "#/definitions/Customer"
                    }
                }],
                "produces": [
                    "application/json"
                ],
                "responses": {
                    "201": {
                        "description": "New customer is created",
                        "schema": {
                            "$ref": "#/definitions/Customer"
                        }
                    }
                }
            }
        }
    },
裴俊迈
2023-03-14

您丢失了安全标签。您可以在securityDefinitions标记下方全局定义它,也可以为每个APIendpoint定义一个。

看看这个问题。

 类似资料: