jmeter中http请求键值对参数签名

jmeter中http键值对请求参数签名

HTTP请求参数如下图,其中sign是需要通过其他参数根据一定规则(ROP)计算而来

鼠标放到HTTP请求上右键打开Add->Pre Processors->Bean Shell PreProcessor(新建)

Bean Shell PreProcessor的Script如下

import org.apache.jmeter.config.Argument;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.http.util.HTTPArgument;
import java.util.List;
import java.util.ArrayList;
import java.security.MessageDigest;

log.info("----------------- pre processor begin");
Arguments arguments = sampler.getArguments();
Map inMap=new HashMap();
int len=arguments.getArgumentCount();
Argument signArg = null;
for(int i=0;i<len;i++){
    Argument jpro=arguments.getArgument(i);
    String key=jpro.getName();
    if (!"sign".equals(key)) {
        String ret=jpro.getValue();
        log.info("----------------- " + key+"="+ret);
        inMap.put(key,ret);
    } else {
        signArg = jpro;
        log.info("----------------- sign arg exists");
    }
}
if (signArg != null) {
	arguments.removeArgument("sign");
}
// log.info(Objects.toString(inMap));
// https://github.com/itstamen/rop/blob/master/rop/src/main/java/com/rop/utils/RopUtils.java
String secret = "909b3b419ad337ee1b04b5f899d51275";
StringBuilder sb = new StringBuilder();
sb.append(secret);
List paramNames = new ArrayList(inMap.size());
paramNames.addAll(inMap.keySet());
Collections.sort(paramNames);
for (String paramName : paramNames) {
 sb.append(paramName).append(inMap.get(paramName));
}
sb.append(secret);
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] bytes = md.digest(sb.toString().getBytes("UTF-8"));
StringBuilder signSb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
  String hex = Integer.toHexString(bytes[i] & 0xFF);
  if (hex.length() == 1) {
      signSb.append("0");
  }
  signSb.append(hex.toUpperCase());
}
log.info("----------------- sign="+signSb.toString());
arguments.addArgument(new HTTPArgument("sign", signSb.toString()));
log.info("----------------- pre processor end");

运行结果如下图

0

评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注

点击更换