PA-*_*-GW 2 node.js swagger swagger-ui openapi
我正在运行express/node 应用程序并使用"swagger-ui-express": "^4.5.0",. 我已经设置了一个要求,即需要将jsonwebtoken不记名令牌与所有请求一起发送到我的 api 中的任何端点。
我已经加载了 swagger 文档并正常工作,但现在当试图弄清楚如何将其传递Authorization: Bearer <token>到我的所有端点时,它似乎不起作用。我可以添加securitySchemes+ 子选项,并在我的 swagger 文档中获得绿色Authorize按钮,但是当我输入不记名令牌并发送请求时,加载旋转器会继续旋转并且从不发送请求。我morgan在我的应用程序中设置了日志记录,因此我可以看到对我的端点的请求永远不会被发送或记录。
如何向从 swagger UI 发送的请求发送不记名令牌?
在 app.js 中,我有一条可以在 localhost 中正确加载的路由
// Single entry point for swagger docs
router.use(
'/swaggerDocs',
swaggerDoc.serve,
swaggerDoc.setup(swaggerDocumentation),
);
Run Code Online (Sandbox Code Playgroud)
swaggerDocumentation来自上面的代码片段(配置文件)。
import getCountryRegions from './getCountryRegions.doc.js';
export default {
openapi: '3.0.3',
info: {
title: 'Node/express rest api app',
version: '0.0.1',
},
components: {
securitySchemes: {
bearerAuth: {
type: 'http',
in: 'header',
name: 'Authorization',
description: 'Bearer Token',
scheme: 'bearer',
bearerFormat: 'JWT',
},
},
},
security: {
bearerAuth: [],
},
servers: [
{
url: 'http://localhost:3010/api',
description: 'Local server',
},
],
paths: {
...getCountryRegions,
},
};
Run Code Online (Sandbox Code Playgroud)
我发现了这个问题......该security属性必须有一个[]包裹在它的对象周围。
components: {
securitySchemes: {
bearerAuth: {
type: 'http',
in: 'header',
name: 'Authorization',
description: 'Bearer token to access these api endpoints',
scheme: 'bearer',
bearerFormat: 'JWT',
},
},
},
security: [
{
bearerAuth: [],
},
],
Run Code Online (Sandbox Code Playgroud)
这段代码有效。