• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Postman通用接口加密解决方案

武飞扬头像
阿里测试君
帮助1

前言:

很对小伙伴对于psotman接口加密不知道如何解决,这里给大家出了一个全网最详细的解决方案,希望能帮助到大家

学新通

点我领取全套自动化测试资料

问题

  1. postman内置加密Api,但不支持RSA加解密码。如何用postman进入rsa加解密?
  2. postman中request对象属性皆为只读,如何把提交时的明文变为密文?

解决问题一

  • postman支持eval函数,我们只要将rsa代码存入环境变量中,在需要的时候调用eval函数就可以解决

解决问题二

  • postman在每次请求时都会先执行pre-request scripts 中的脚本,在此处我们可以通过request对象拿到
    此次请求的参数,但request中的参数只可读。
    所以我们只能通过环境变量去占位然后在去加密变量中的值,于是在请求时的内容就会变成加密的内容。
    针对postman对{{}}读取的方式,我们可以先在请求参数中将要加密的内容包裹进来,然后在动态创建此变量,
    并将变量的加密内容写入此环境变量中,最后在执行请求完毕后将此变量清除

例子

学新通

AES加密参数

  1. 必需用{{}}将内容包起来,因为在进行请求后postman遇到{{}}时会从环境变量中读取,如果有该环境变量则会动态替换。
  2. $符号后面是我们要加密的内容,可以直接填写内容或写入环境变量的名称

     

    学新通

    动态生成的环境变量

     

    如果不想在环境变量夹中显示动态生成的环境变量可以将下方tests中的脚本加入到tests中

  3. 点我领取全套自动化测试资料

相关脚本

  • 注意:将脚本加入到collections中会更好
  • Pre-request Scripts
  1.  
    // ------ 通用方法 ------
  2.  
    // 提取{{}}中内容
  3.  
    function getBracketStr(text) {
  4.  
    let result = ''
  5.  
    let regex = /\{\{(. ?)\}\}/g;
  6.  
    let options = text.match(regex);
  7.  
    if (options && options.length > 0) {
  8.  
    let option = options[0];
  9.  
    if (option) {
  10.  
    result = option.substring(2, option.length - 2)
  11.  
    }
  12.  
    }
  13.  
     
  14.  
    return result
  15.  
    }
  16.  
     
  17.  
     
  18.  
    // ------ 导入RSA ------
  19.  
    if(!pm.globals.has("forgeJS")){
  20.  
    pm.sendRequest("https://raw.githubusercontent.com/loveiset/RSAForPostman/master/forge.js", (err, res) => {
  21.  
    if (!err) {
  22.  
    pm.globals.set("forgeJS", res.text())
  23.  
    }
  24.  
    })}
  25.  
     
  26.  
    eval(postman.getGlobalVariable("forgeJS"));
  27.  
     
  28.  
     
  29.  
    // ------------ AES 加密 ------------
  30.  
    function aesEncrypt(content){
  31.  
    //console.log('AES: ' content);
  32.  
    const key = CryptoJS.enc.Utf8.parse("Y5MUIOM7BUWI7BQR");
  33.  
    const iv = CryptoJS.enc.Utf8.parse('S41AXIPFRFVJL73Z');
  34.  
    const encrypted = CryptoJS.AES.encrypt(content, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7});
  35.  
    return encrypted.toString();
  36.  
    }
  37.  
     
  38.  
     
  39.  
    // ------------ RSA 加密 ------------
  40.  
    function rsaEncrypt(content){
  41.  
    const pubKey = pm.environment.get("RSA_Public_Key");
  42.  
    if(pubKey){
  43.  
    const publicKey = forge.pki.publicKeyFromPem(pubKey);
  44.  
    const encryptedText = forge.util.encode64(
  45.  
    publicKey.encrypt(content, 'RSAES-PKCS1-V1_5', {
  46.  
    md: forge.md.sha1.create(),
  47.  
    mgf: forge.mgf.mgf1.create(forge.md.sha1.create())
  48.  
    }));
  49.  
    return encryptedText;
  50.  
    }
  51.  
    }
  52.  
     
  53.  
     
  54.  
    // ------ 存储所有未加密环境变量 ------
  55.  
    if(!pm.environment.has('localStore')){
  56.  
    pm.environment.set('localStore', '{}');
  57.  
    }
  58.  
    let localStore = JSON.parse(pm.environment.get('localStore'));
  59.  
    // 获取当前请求中的加密变量
  60.  
    let requestData;
  61.  
    if((typeof request.data) === 'string'){
  62.  
    requestData = JSON.parse(request.data)
  63.  
    } else {
  64.  
    requestData = request.data;
  65.  
    }
  66.  
     
  67.  
    requestData = Object.assign(requestData, request.headers);
  68.  
    Object.keys(requestData).map(key => {
  69.  
    let value = requestData[key] ''; // 内容
  70.  
    if (value.indexOf('{{') !== -1) { // 是否为变量
  71.  
    let content = getBracketStr(value);
  72.  
    // 判断是否加密
  73.  
    if (content.indexOf('aes$') !== -1) {
  74.  
    let c = content.split('aes$')[1];
  75.  
    let encryptedContent = pm.environment.get(c); // 加密内容
  76.  
    encryptedContent = encryptedContent ? encryptedContent : c;
  77.  
    pm.environment.set(content, aesEncrypt(encryptedContent));
  78.  
    localStore[content] = aesEncrypt(encryptedContent);
  79.  
    } else if (content.indexOf('rsa$') !== -1) {
  80.  
    let c = content.split('rsa$')[1];
  81.  
    let encryptedContent = pm.environment.get(c); // 加密内容
  82.  
    encryptedContent = encryptedContent ? encryptedContent : c;
  83.  
    pm.environment.set(content, rsaEncrypt(encryptedContent));
  84.  
    localStore[content] = rsaEncrypt(encryptedContent);
  85.  
    }
  86.  
    }
  87.  
    });
  88.  
     
  89.  
    pm.environment.set('localStore', JSON.stringify(localStore));
学新通
  • Tests scripts
  1.  
    // 还原变量
  2.  
    if(!pm.environment.has('localStore')){
  3.  
    pm.environment.set('localStore', '{}');
  4.  
    }
  5.  
    let localStore = JSON.parse(pm.environment.get('localStore'));
  6.  
    Object.keys(localStore).map(key => {
  7.  
    pm.environment.unset(key)
  8.  
    });
  9.  
     
  10.  
    pm.environment.unset('localStore')

学新通

文章到这里就结束了,各位铁汁如果有什么觉得不对的可以发在评论区咱们来讨论哈,
听说关注我并三连的铁汁都已经升职加薪暴富了哦!!!! 

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfifjka
系列文章
更多 icon
同类精品
更多 icon
继续加载