javascript - 如何在 Postman 中使用 SHA-1 动态 key

标签 javascript security sha postman

我正在尝试使用 Postman 发送一个 GET http 请求,该请求包含一个参数,该参数是通过采用完整的请求查询字符串动态生成的(URL 中问号右侧的所有内容,在 URL 编码后),连接先前分配的共享 key ,然后对结果字符串执行 SHA-1 散列。

我会使用预请求脚本来实现这一点。

谢谢。

最佳答案

我实际上找到了解决方案并愿意分享。

var params = [
    ["client_id", "222"]
    ,["account_id", ""]
];

// Build the request body string from the Postman request.data object
var requestBody = "";
var firstpass = true;
for(var i=0;i < params.length; i++) {
        if(!firstpass){
            requestBody += "&";
        }
        requestBody += params[i][0] + "=" + params[i][1];
        firstpass = false;
        postman.setGlobalVariable(params[i][0], params[i][1]);
}
requestBody += postman.getEnvironmentVariable("sharedSecretKey");
postman.setGlobalVariable("requestBody", requestBody);

var mac = "";
if(requestBody){
    // SHA1 hash
    mac = CryptoJS.SHA1(requestBody);
}

postman.setGlobalVariable("mac", mac);

然后我只需要在 URL 中设置参数: {{baseUrl}}/get?client_id={{client_id}}&account_id={{account_id}}&mac={{mac}}

其中 {{baseUrl}} 是一个环境变量 而{{client_id}}、{{account_id}}是全局变量

希望对大家有所帮助。

谢谢。

关于javascript - 如何在 Postman 中使用 SHA-1 动态 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37333994/

相关文章:

php - PHP 中的 JWT(JSON Web token ),无需使用第 3 方库。怎么签?

android - Android 设备的唯一 ID

javascript - 用字符串替换字符串中的多个字符重写字符

javascript - 每个内部的 Handlebars If 语句都失去了上下文

python - 允许客户查看Python代码,但安全修改

c# - 指南、查询字符串和身份验证

javascript - 具有绑定(bind)函数和非绑定(bind)函数的数组过滤器

javascript - 您如何管理您的 DojoX 代码?

javascript - 为 javascript/jQuery API 客户端进行身份验证时如何保护凭据?

python - 哈希字符串大小