postman请求参数进行加密处理-以aes为例

postman Pre-request Script配置

将以下代码添加到collection或者接口的Pre-request Script

var aesKey = "key";
var aesIv = "iv";

let json = []
// 获取请求参数,加密后再放入json串中
for(key in request.data) {
    if(pm.environment.get(key)) {
        val = encrypt(pm.environment.get(key), aesKey, aesIv)
    } else {
        val = encrypt(request.data[key], aesKey, aesIv)
    }
    json.push({ "key": key, "value": val})
}

// formdata方式请求更新
pm.request.body.update({
    mode: 'formdata',
    formdata: json
});

// 其他请求方式示例
// Raw
// pm.request.body.update('Hello World!');

// URL Encoded
// pm.request.body.update({
//   mode: 'urlencoded',
//   urlencoded: [{
//        key: 'foo',
//        value: 'bar'
//    }]
//});

// postman自带CryptoJS模块进行aes加密
function encrypt(word, keyStr, ivStr) {
    var key = CryptoJS.enc.Utf8.parse(keyStr);
    var iv = CryptoJS.enc.Utf8.parse(ivStr);
    var encrypted = CryptoJS.AES.encrypt(word, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.ZeroPadding
    });
    // encrypted = encodeURIComponent(encrypted)
    return encrypted.toString();
}

// postman自带CryptoJS模块进行aes解密
function aesDecrypt(word, keyStr, ivStr){
    var key = CryptoJS.enc.Utf8.parse(keyStr);
    var iv = CryptoJS.enc.Utf8.parse(ivStr);
    var srcs = CryptoJS.enc.Utf8.parse(word);
    var decrypted = CryptoJS.AES.decrypt(srcs, key, { 
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding:CryptoJS.pad.ZeroPadding 
    });
    return decrypted.toString(CryptoJS.enc.Utf8);
}

部分引用:https://github.com/postmanlabs/postman-app-support/issues/4808

Visits: 22418

问题记录

Q:

An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: https://cli-assets.heroku.com/apt ./ InRelease: The following sig
natures couldn’t be verified because the public key is not available: NO_PUBKEY 5DC22404A6F9F1CA

A:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 5DC22404A6F9F1CA

Q:

Invalid command ‘RewriteEngine’, perhaps misspelled or defined by a module not included in the server configuration.

A:

RewriteEngine命令需要rewrite mod的支持,
$>cd /etc/apache2/mods-enabled 切换到apache下的mods-enabled目录
$>sudo ln -s ../mods-available/rewrite.load rewrite.load 启用rewrite mod
$>sudo /etc/init.d/apache2 restart 重启apache服务器。
或者在apache的配置文件httpd.conf中将#LoadModule rewrite_module modules/mod_rewrite.so前的#去掉,再重启服务器。

Visits: 1155