有没有朋友使用过swagger的?在node里使用时,怎么配置它的authorize按钮的?
6条回答 默认 最新
檀越@新空间 2024-05-06 14:09关注下午好🌅🌅🌅
本答案参考ChatGPT-3.5首先需要说明的是Swagger是API文档生成器,并不提供身份认证、授权等功能,所以在Swagger UI中看到的Authorize按钮仅仅是UI控件,只有点击该按钮才会触发一些配置设定的操作。
那么如何配置Swagger UI中的Authorize按钮呢?
- 注入自定义JS文件
可以在Swagger UI的HTML模板中添加自定义的JS文件,代码如下:
<!-- index.html --> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>{{title}}</title> <link rel="stylesheet" href="{{{swaggerUiCss}}}" type="text/css" /> <link rel="stylesheet" href="{{{customCss}}}" type="text/css" /> <!-- inject custom js --> <script src="./custom.js"></script> </head> <body class="swagger-section"> <div id="swagger-ui"></div> <!-- inject swagger-ui.js --> <script src="{{{swaggerUiBundle}}}" charset="UTF-8"> </script> <!-- inject swagger-ui-bundle.js --> <script src="{{{customBundle}}}" charset="UTF-8"> </script> <!-- inject swagger-ui-standalone-preset.js --> <script src="{{{standalonePresetBundle}}}" charset="UTF-8"> </script> </body> </html>在custom.js中可以通过以下代码隐藏或者修改Authorize按钮的显示文本:
const authorizeDiv = document.querySelector('.authorize-wrapper'); authorizeDiv.style.display = 'none'; // 隐藏Authorize按钮 // 修改Authorize按钮的显示文本 const authorizeBtn = document.querySelector('.authorize-button'); authorizeBtn.innerText = '登录';- 使用第三方插件
在使用Swagger UI时,可以使用第三方的认证插件,比如可以使用Swagger UI OAuth插件来扩展Swagger UI的认证功能。具体用法可以参考该插件的文档:https://github.com/swagger-api/swagger-ui/tree/v3.49.0/docs/usage/oauth2
总之,Swagger本身提供的Authorize按钮只是一个UI控件,要实现真正的身份认证和授权功能需要使用第三方插件或者开发者自行开发。
解决 无用评论 打赏 举报