当前位置:网站首页>knife4j通过js动态刷新全局参数
knife4j通过js动态刷新全局参数
2022-07-27 18:20:00 【code2roc】
背景
之前在为框架集成knife4j接口调试查看工具,使用了一段时间,使用体验上比较繁琐,因为接口都需要token,所以每次都要去f12查看token复制再创建全局参数,可能我只需要测试一个接口但是步骤少不了,针对此问题框架做了一些优化
设计分析
框架后端针对系统管理员增加一个根据用户直接生成token的接口,将获取到的token通过js方式直接附加到knife4j的全局参数中,这样就只需要点击获取token按钮我们就可以直接进行任意接口的调试工作,通过f12分析发现,knife4j的全局参数变量是存储在浏览器数据库IndexedDB中,数据表为keyvaluepairs,对应的数据行key为Knife4jOfficeParameter


进一步分析字段名称为SwaggerBootstrapUiInstance68c7b0eebe75b10d20003678a43730cb,存储值就是我们添加的全局参数设置的数组列表,字段名是由SwaggerBootstrapUiInstance+编码命名的,所以我们只要搞定编码的生成就可以自己通过js赋值了
因为knife4j集成的doc.html页面是由vue打包生成的,js做过编译处理,所以源码我们需要具体的vue工程中查看,经过分析查找SwaggerBootstrapUiInstance关键字
代码路径:knife4j/knife4j-vue/src/core/Knife4jAsync.js
生成规则:生成的编码由name(分组对象)+location(url地址)+version(版本号)生成的字符串md5后的值

上述的name,location,version三个值是通过请求swagger-resources接口获取的,返回值为一个数组,根据选择的group去匹配

代码实现
分析完毕后就可以进行代码操作了,其中涉及到IndexedDB的操作简单学习一下即可
- 获取所有资源
function initResourceInfo() {
$.get(resourceUrl, function(data, status) {
data.forEach(element => {
pageData.resourceMap[element.name] = element;
});
});
}
- 设置全局参数
function refreshKnife4jConfig(token) {
var selectApiName = window.knife4jFrame.contentWindow.document.getElementsByClassName("ant-select-selection-selected-value")[0].innerText;
var resource = pageData.resourceMap[selectApiName];
if ('indexedDB' in window) {
var req = indexedDB.open("localforage");
req.onupgradeneeded = function(event) {
}
req.onsuccess = function(event) {
console.log('数据库开启成功');
var db = event.target.result;
var table = db.transaction(['keyvaluepairs'], 'readwrite').objectStore('keyvaluepairs')
var key = resource.name + resource.location + resource.swaggerVersion;
var id = hpMD5(key).toLowerCase();
var configData = {
}
configData["SwaggerBootstrapUiInstance" + id] = [
{
in: "header",
name: "token",
pkid: "tokenheader",
value: token
},
{
in: "header",
name: "Content-Type",
pkid: "Content-Typeheader",
value: "application/json"
}, ]
table.put(configData, "Knife4jOfficeParameter");
}
req.onerror = function() {
console.log("数据库开启出错");
}
} else {
console.log('你的浏览器不支持IndexedDB');
}
}
边栏推荐
- 国际权威认可!OceanBase入选Forrester Translytical数据平台报告
- Best practices for Oracle kingbasees migration of Jincang database (2. Overview)
- After working for bytek for two years, he got 15 offers at one go
- Analysis on the optimization of login request in IM development of instant messaging mobile terminal
- Can tonghuashun open an account on weekends? Is it safe to open an account
- 用户和权限撤销用户权限
- MySQL basic queries and operators
- People call this software testing engineer. You're just making a living (with HR interview Dictionary)
- Onion group joined hands with oceanbase to realize distributed upgrading, and global data has achieved cross cloud integration for the first time
- 软件测试面试题:已知一个队列,如: [1, 3, 5, 7], 如何把第一个数字,放到第三个位置,得到:[3, 5, 1, 7]
猜你喜欢
![[deep learning] pytoch torch Autograd automatic differential engine](/img/c8/2ce1e5c02283965f8690ac5a9971a9.png)
[deep learning] pytoch torch Autograd automatic differential engine

【深度学习】Pytorch Tensor 张量

Using dataX to realize efficient synchronization of MySQL data

一文了解Pycharm快捷键

Flask Mdict builds online MDICT Dictionary Service

vi工作模式(3种)以及模式切换(转换)
![[hierarchical reinforcement learning] HAC paper and code](/img/f5/d5f791d0f9da749eafa912be9dfbb5.png)
[hierarchical reinforcement learning] HAC paper and code
![[Numpy] 数组索引和切片](/img/ce/34db7aef3fefe8a03e638d0838492f.png)
[Numpy] 数组索引和切片

Introduction to JVs Foundation

浅析即时通讯移动端 IM 开发中登录请求的优化
随机推荐
Best practices for Oracle kingbasees migration of Jincang database (4. Oracle database migration practice)
【Dart】一门为跨端开发而生的编程语言
Innovative cases | the growth strategy of digitalization of local life services and upgrading of Gaode brand
SQL高级技巧CTE和递归查询
DJI内推码(一码一用,2022年7月26日更新)
一文了解Pycharm快捷键
认识传输介质网络通信的介质
After working for bytek for two years, he got 15 offers at one go
Some contents related to cmsis-rtos
openresty lua-resty-core 使用
Software test interview question: string "axbyczdj", if you get the result "ABCD"
【深度学习】Pytorch Tensor 张量
程序中的地址如何转换?
【数据集显示标注】VOC文件结构+数据集标注可视化+代码实现
Write bootloader from 0
Onion group joined hands with oceanbase to realize distributed upgrading, and global data has achieved cross cloud integration for the first time
People call this software testing engineer. You're just making a living (with HR interview Dictionary)
认识网络模型数据的封装和解封装
【效率】弃用 Notepad++,这款开源替代品更牛逼!
未定义变量 “Lattice“ 或类 “Lattice.latticeEasy“(Matlab)