【发布时间】:2020-08-26 06:09:06
【问题描述】:
我正在 jmeter 中设置一个测试计划,其中 Sampler 是一个 HTTP POST 请求。 我必须在请求正文中发送 JSON 有效负载。 现在,出于身份验证的目的,我必须使用将在请求标头中传递的秘密创建一个 HMAC sha256 代码。 如何在 preProcessor 脚本中创建 HMAC?
【问题讨论】:
标签: java rest groovy jmeter hmac
我正在 jmeter 中设置一个测试计划,其中 Sampler 是一个 HTTP POST 请求。 我必须在请求正文中发送 JSON 有效负载。 现在,出于身份验证的目的,我必须使用将在请求标头中传递的秘密创建一个 HMAC sha256 代码。 如何在 preProcessor 脚本中创建 HMAC?
【问题讨论】:
标签: java rest groovy jmeter hmac
我以前做过类似的事情:
public static Mac SHARED_MAC;
static {
try {
SHARED_MAC = Mac.getInstance("HmacSHA256");
} catch (NoSuchAlgorithmException nsaEx) {
nsaEx.printStackTrace();
}
}
private String secretKey; // set this in a constructor or pass to the method below
public String generateSignature(String requestPath, String method, String body, String timestamp) {
try {
String prehash = timestamp + method.toUpperCase() + requestPath + body;
byte[] secretDecoded = Base64.getDecoder().decode(secretKey);
SecretKeySpec keyspec = new SecretKeySpec(secretDecoded, SHARED_MAC.getAlgorithm());
Mac sha256 = (Mac) SHARED_MAC.clone();
sha256.init(keyspec);
return Base64.getEncoder().encodeToString(sha256.doFinal(prehash.getBytes()));
} catch (CloneNotSupportedException | InvalidKeyException e) {
e.printStackTrace();
throw new RuntimeErrorException(new Error("failed")); // fatal error - exits program
}
}
某种意义上的HTH
【讨论】: