网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
- 必需用{{}}将内容包起来,因为在进行请求后postman遇到{{}}时会从环境变量中读取,如果有该环境变量则会动态替换。
- $符号后面是我们要加密的内容,可以直接填写内容或写入环境变量的名称
动态生成的环境变量
如果不想在环境变量夹中显示动态生成的环境变量可以将下方tests中的脚本加入到tests中
3. 点我领取全套自动化测试资料
相关脚本
- 注意:将脚本加入到collections中会更好
- Pre-request Scripts
// ------ 通用方法 ------
// 提取{{}}中内容
function getBracketStr(text) {
let result = ''
let regex = /\{\{(.+?)\}\}/g;
let options = text.match(regex);
if (options && options.length > 0) {
let option = options[0];
if (option) {
result = option.substring(2, option.length - 2)
}
}
return result
}
// ------ 导入RSA ------
if(!pm.globals.has("forgeJS")){
pm.sendRequest("https://raw.githubusercontent.com/loveiset/RSAForPostman/master/forge.js", (err, res) => {
if (!err) {
pm.globals.set("forgeJS", res.text())
}
})}
eval(postman.getGlobalVariable("forgeJS"));
// ------------ AES 加密 ------------
function aesEncrypt(content){
//console.log('AES: ' + content);
const key = CryptoJS.enc.Utf8.parse("Y5MUIOM7BUWI7BQR");
const iv = CryptoJS.enc.Utf8.parse('S41AXIPFRFVJL73Z');
const encrypted = CryptoJS.AES.encrypt(content, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7});
return encrypted.toString();
}
// ------------ RSA 加密 ------------
function rsaEncrypt(content){
const pubKey = pm.environment.get("RSA_Public_Key");
if(pubKey){
const publicKey = forge.pki.publicKeyFromPem(pubKey);
const encryptedText = forge.util.encode64(
publicKey.encrypt(content, 'RSAES-PKCS1-V1_5', {
md: forge.md.sha1.create(),
mgf: forge.mgf.mgf1.create(forge.md.sha1.create())
}));
return encryptedText;
}
}
// ------ 存储所有未加密环境变量 ------
if(!pm.environment.has('localStore')){
pm.environment.set('localStore', '{}');
}
let localStore = JSON.parse(pm.environment.get('localStore'));
// 获取当前请求中的加密变量
let requestData;
if((typeof request.data) === 'string'){
requestData = JSON.parse(request.data)
} else {
requestData = request.data;
}
requestData = Object.assign(requestData, request.headers);
Object.keys(requestData).map(key => {
let value = requestData[key] + ''; // 内容
if (value.indexOf('{{') !== -1) { // 是否为变量
let content = getBracketStr(value);
// 判断是否加密
if (content.indexOf('aes$') !== -1) {
let c = content.split('aes$')[1];
let encryptedContent = pm.environment.get(c); // 加密内容
encryptedContent = encryptedContent ? encryptedContent : c;
pm.environment.set(content, aesEncrypt(encryptedContent));
localStore[content] = aesEncrypt(encryptedContent);
} else if (content.indexOf('rsa$') !== -1) {
let c = content.split('rsa$')[1];
let encryptedContent = pm.environment.get(c); // 加密内容
encryptedContent = encryptedContent ? encryptedContent : c;
pm.environment.set(content, rsaEncrypt(encryptedContent));
localStore[content] = rsaEncrypt(encryptedContent);
}
}
});
pm.environment.set('localStore', JSON.stringify(localStore));
- Tests scripts
// 还原变量
if(!pm.environment.has('localStore')){
pm.environment.set('localStore', '{}');
}
let localStore = JSON.parse(pm.environment.get('localStore'));
Object.keys(localStore).map(key => {
pm.environment.unset(key)
});
pm.environment.unset('localStore')
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
戳这里获取](https://bbs.youkuaiyun.com/topics/618631832)**
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!