当前位置:网站首页>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数据平台报告
- Kingbasees heterogeneous database migration guide (2. Overview)
- 【阿里安全 × ICDM 2022】20万奖金池!大规模电商图上的风险商品检测赛火热报名中!...
- 未定义变量 “Lattice“ 或类 “Lattice.latticeEasy“(Matlab)
- 走马灯案例
- 一周活动速递|深入浅出第8期;Meetup成都站报名进行中
- Xdc 2022 Intel technology special session: Intel Software and hardware technology builds the cornerstone of cloud computing architecture
- [deep learning] video classification technology sorting
- MYSQL设计优化生成列
- 如何解决tp6控制器不存在:app\controller\Index
猜你喜欢

UE5使用DLSS(超级采样)提升场景的 FPS 远离卡顿的优化方案
![[Alibaba security × ICDM 2022] 200000 bonus pool! The risk commodity inspection competition on the large-scale e-commerce map is in hot registration](/img/38/9fadea0d37053a3ebb73806a9963f1.jpg)
[Alibaba security × ICDM 2022] 200000 bonus pool! The risk commodity inspection competition on the large-scale e-commerce map is in hot registration
![[benefit activity] stack a buff for your code! Click](/img/2d/dabf0ad5d7bd553dada5921abf6c06.png)
[benefit activity] stack a buff for your code! Click "tea" to receive the gift

做测试, 就得去大厂,内部披露BAT大厂招聘“潜规则”

用户登录切换案例

leetcode:1498. 满足条件的子序列数目【排序 + 二分 + 幂次哈希表】

一周活动速递|深入浅出第8期;Meetup成都站报名进行中

A layered management method of application layer and hardware layer in embedded system

Interviewer: what is the abstract factory model?

LabVIEW学习笔记五:按钮按下后无法返回原状
随机推荐
Software test interview question: string "axbyczdj", if you get the result "ABCD"
sql编码bug
一文了解Pycharm快捷键
用户登录切换案例
JVs privatization deployment start failure handling scheme
说透缓存一致性与内存屏障
What are the apps of regular futures trading platforms in 2022, and are they safe?
Users and permissions create ordinary users
Swiftui view onReceive method receives "redundant" event resolution
《SRE:Google运维解密》读后有感
One week activity express | in simple terms, issue 8; Meetup Chengdu station registration in progress
学术分享 | 清华大学 康重庆:电力系统碳计量技术与应用(Matlab代码实现)
[dart] a programming language for cross end development
Flask Mdict builds online MDICT Dictionary Service
Babbitt | metauniverse daily must read: Tencent News suspended the sales service of digital collections, users left messages asking for a "refund", and phantom core also fell into the dilemma of "unsa
How to improve the picture transmission speed and success rate in the development of IM instant messaging under the mobile network
[benefit activity] stack a buff for your code! Click "tea" to receive the gift
Onion group joined hands with oceanbase to realize distributed upgrading, and global data has achieved cross cloud integration for the first time
MySQL string function
Software test interview question: if a string is known as "hello_world_yoyo", how to get a queue ["hello", "world", "yoyo"]