请求在JMeter中的签名

huangapple go评论71阅读模式
英文:

Request signing in JMeter

问题

我需要在HTTP请求中附带一个SHA256十六进制请求签名。

从其他问题和答案中可以看出,建议使用BeanShell预处理器,但这似乎不再起作用。

还有其他的方法来实现吗?

这是我的BeanShell预处理器中的代码:

import org.apache.commons.codec.digest.DigestUtils;

String api_key = "";
String shared_secret = "";
long timestamp = System.currentTimeMillis()/1000;

String sig = DigestUtils.md5Hex(api_key + shared_secret + timestamp);

vars.put("sig", sig);

log.info("Signature: " + sig);

以下是错误信息:

2020-07-23 10:18:55,694 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval	Sourced file: inline evaluation of: "import org.apache.commons.codec.digest.DigestUtils;  String api_key = "35yNeSe37 . . . '' : Typed variable declaration
2020-07-23 10:18:55,694 WARN o.a.j.m.BeanShellPreProcessor: Problem in BeanShell script. org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval	Sourced file: inline evaluation of: "import org.apache.commons.codec.digest.DigestUtils;  String api_key = "35 . . . '' : Typed variable declaration
2020-07
英文:

I need to send a sha256 hex request signature along with http requests.

From other questions and answers, there were suggestions to use bean shell preprocessor, but this does not seem to work anymore.

Are there any other ways to do this?

This is my code in the bean shell preprocessor

import org.apache.commons.codec.digest.DigestUtils;

String api_key = "";
String shared_secret = "";
long timestamp = System.currentTimeMillis()/1000;

String sig = DigestUtils.md5(api_key + shared_secret + timestamp);

vars.put("sig", sig);

log.info("Signature: " + sig);

And this is the error

2020-07-23 10:18:55,694 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval	Sourced file: inline evaluation of: ``import org.apache.commons.codec.digest.DigestUtils;  String api_key = "35yNeSe37 . . . '' : Typed variable declaration
2020-07-23 10:18:55,694 WARN o.a.j.m.BeanShellPreProcessor: Problem in BeanShell script. org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval	Sourced file: inline evaluation of: ``import org.apache.commons.codec.digest.DigestUtils;  String api_key = "35 . . . '' : Typed variable declaration
2020-07

答案1

得分: 1

  1. 为什么你在提到需要 "sha256 十六进制" 的情况下使用 DigestUtils.md5()
  2. 为什么你在 自 JMeter 3.1 起应使用 Groovy,却在使用 Beanshell?

我认为你应该将代码更改为类似以下的形式:

vars.put("sig", org.apache.commons.codec.digest.DigestUtils.sha256Hex(api_key + shared_secret + timestamp))

有关在 JMeter 中使用 Groovy 脚本的更多信息,请参阅文章 Apache Groovy - 为什么以及如何使用它

顺便说一下,还有一个 __digest() 函数 可以让你的工作变得更加轻松:

请求在JMeter中的签名

英文:
  1. Why you're using DigestUtils.md5() if you're stating that you need "sha256 hex"?
  2. Why you're using Beanshell if since JMeter 3.1 you should be using Groovy?

I think you should change you code to something like:

vars.put("sig", org.apache.commons.codec.digest.DigestUtils.sha256Hex(api_key + shared_secret + timestamp))

See Apache Groovy - Why and How You Should Use It article for more information on Groovy scripting in JMeter.

By the way, there is __digest() function which can make your life even easier:

请求在JMeter中的签名

huangapple
  • 本文由 发表于 2020年7月23日 08:48:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63045183.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定