当前位置:网站首页>Knife4j dynamically refreshes global parameters through JS
Knife4j dynamically refreshes global parameters through JS
2022-07-27 21:12:00 【code2roc】
background
Before, I was integrating for the framework knife4j Interface debugging viewing tool , Used for a while , The use experience is cumbersome , Because interfaces all need token, So I have to go every time f12 see token Copy and create global parameters , Maybe I only need to test one interface, but the steps are indispensable , The framework has made some optimizations for this problem
design analysis
The back-end of the framework adds a user generated interface for system administrators token The interface of , Will get token adopt js The method is directly attached to knife4j In the global parameters of , In this way, you only need to click to get token Button, we can directly debug any interface , adopt f12 Analysis found ,knife4j The global parameter variables of are stored in the browser database IndexedDB in , The data sheet is keyvaluepairs, Corresponding data row key by Knife4jOfficeParameter


Further analysis, the field name is SwaggerBootstrapUiInstance68c7b0eebe75b10d20003678a43730cb, The stored value is the array list of global parameter settings we added , The field name is defined by SwaggerBootstrapUiInstance+ Code named , So as long as we finish the generation of code, we can pass by ourselves js Assigned a value to
because knife4j Integrated doc.html Page is created by vue It's packaged ,js Compiled , So we need specific source code vue View in the project , After analysis and search SwaggerBootstrapUiInstance keyword
Code path :knife4j/knife4j-vue/src/core/Knife4jAsync.js
Generate rules : The generated code is generated by name( Group objects )+location(url Address )+version( Version number ) The generated string md5 After the value of

Aforementioned name,location,version The three values are by request swagger-resources Interface obtained , The return value is an array , selective group To match

Code implementation
After the analysis, you can operate the code , It involves IndexedDB It's easy to learn the operation of
- Get all the resources
function initResourceInfo() {
$.get(resourceUrl, function(data, status) {
data.forEach(element => {
pageData.resourceMap[element.name] = element;
});
});
}
- Set global parameters
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(' The database was opened successfully ');
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(" Error opening database ");
}
} else {
console.log(' Your browser does not support it IndexedDB');
}
}
边栏推荐
- Source Insight 4.0使用介绍
- Repeated DNA sequence [hash determination repetition + sliding window + bit operation of binary coding]
- CPDA | how to have data analysis thinking?
- 怎样实现文档协同?
- 电脑微软账户登录一直转圈怎么解决问题
- Dobot Magician 机器臂-简介
- 最新版web漏洞扫描工具AppScan\AWVS\Xray安装及使用教程
- Global styles and icons
- Automatic test solution based on ATX
- "Harvest" NFT: 200 yuan to buy pictures on Taobao, and 300000 yuan on the chain
猜你喜欢

Automated testing - unittest framework

Leetcode daily practice - 876. Intermediate node of linked list

北京/上海/广州/深圳DAMA-CDGA/CDGP数据治理认证报名条件

How to translate the address in the program?

Hexagon_ V65_ Programmers_ Reference_ Manual(7)

API Gateway介绍

LeetCode每日一练 —— 206. 反转链表

82. (cesium article) cesium points move on 3D models

如何对话CIO/CTO
![[numpy] array properties](/img/eb/a27c24deeb7951828cdfbaa88c059c.png)
[numpy] array properties
随机推荐
Source Insight 4.0使用介绍
自定义学习率
搭建discuz论坛并攻破盗取数据库
LeetCode每日一练 —— 链表中倒数第 k 个结点
SQL coding bug
QT OpenGL makes objects move under care to form animation
sscanf 导致地址越界
Vant component library
实名认证在文旅出行行业的应用场景有哪些?
二舅,为什么火了?
最新版web漏洞扫描工具AppScan\AWVS\Xray安装及使用教程
一文读懂Plato Farm的ePLATO,以及其高溢价缘由
Automatic test solution based on ATX
Beijing / Shanghai / Guangzhou / Shenzhen dama-cdga/cdgp data governance certification registration conditions
中地数码:融合创新国产GIS 乘风而上助推实景三维中国建设
IPv4/IPv6、DHCP、网关、路由
如何让个性化推荐即刻触达?云原生数据库GaussDB(for Redis)来助力
LeetCode每日一练 —— 203. 移除链表元素
Tips for file upload to bypass WAF
82.(cesium篇)cesium点在3d模型上运动