gpt4 book ai didi

node.js - 如何在 nodejs 中的 Swagger UI(swagger.json)中的 header 中表示自定义 token

转载 作者:行者123 更新时间:2023-12-05 05:13:17 35 4
gpt4 key购买 nike

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

enter image description here

Swagger .json

{
"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"
}
}
}
}
}

应用程序路由

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 用户界面:

最佳答案

您缺少安全标签。您可以在 securityDefinitions 标记下方全局定义它,也可以为每个 API 端点定义一个。

看看this问题。

关于node.js - 如何在 nodejs 中的 Swagger UI(swagger.json)中的 header 中表示自定义 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53724820/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com