Shi*_*bde 4 javascript security authentication node.js swagger
我已经在 node 中集成了 swagger,它可以在 http://localhost:3002/api-docs 上访问。但是 swagger ui 是可公开访问的。我想添加身份验证/安全性以访问此路由。当用户点击 http://localhost:3002/api-docs 时,它应该显示弹出窗口/提示输入用户名/密码。如果用户名和密码正确,则只有用户才能看到 swagger UI。
可能如下图所示
我正在使用swagger-ui-express,这是我使用的代码
import swaggerUi from 'swagger-ui-express';
import * as swaggerDocument from './swagger.json'
....
....
app.use("/api-docs",swaggerUi.serve,swaggerUi.setup(swaggerDocument));
Run Code Online (Sandbox Code Playgroud)
我在互联网上搜索,但没有得到任何解决方案。我找到了一个解决方案,但那是在春天。
提前致谢 !!
您可以插入一个 basic-auth 中间件(例如https://github.com/LionC/express-basic-auth)来保护 swagger-ui 路由。如果使用express-basic-auth,请确保设置challenge option以强制浏览器打开提示:
const basicAuth = require('express-basic-auth');
app.use("/api-docs",basicAuth({
users: {'yourUser': 'yourPassword'},
challenge: true,
}), swaggerUi.serve, swaggerUi.setup(swaggerDocument));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1404 次 |
| 最近记录: |