jmeter实现带RSA的sign签名

实现开放接口  我们通常需要根据参数  及开发提供的密钥 生成一个签名 

在根据解签签证成功后 请求接口

接口名称:/api-open-center/open/gateway

请求方式:post

参数类型:form-data

根据正常的添加线程添加http请求设置好

添加默认线程组

在添加一个http请求,设置好url及参数及请求方式,请求类型

再添加http请求头 比如说Content-Type参数之类的头部参数

由于我们需要的签名需要根据参数生成,所以我们需要加一个BeanShell 预处理程序

右键-添加前置处理器-BeanShell PreProcessor

 写你的代码

这里生成的RSA是用阿里的方法的 你可以自己下一个 也可让开发生成个jar包给你 都可

在测试计划中导入jar包,即可在后面引用了

 以下为前置处理器中生成签名的代码 如下

import com.alipay.api.internal.util.AlipaySignature;
import org.apache.jmeter.threads.JMeterVariables;

import java.util.*;
import java.lang.*;
log.info("--------------------------------------"+ vars);
log.info("--------------------------------------"+ vars.get("version"));
String PRIVATE_KEY ="这里是密钥,你问开发要";
//   Map pmMap = new HashMap();

   List list = new ArrayList();
   list.add("api_request_id");
   list.add("app_id");
   list.add("charset");
   list.add("content");
   list.add("method");
   list.add("timestamp");
   list.add("version");
//
//   Iterator it = vars.getIterator();
//   while(it.hasNext()){
//           Map.Entry v = it.next();
//           log.info("====++++++++++++===="+v.getKey()+ "============" + v.getValue());
           pmMap.put( v.getKey(), v.getValue());
//   }
//   pmMap.remove("sign");
//   pmMap.remove("sign_type");
   

     String content = "";
//        List keys = new ArrayList(pmMap.keySet());
//        Collections.sort(keys);
        Iterator var4 = list.iterator();
       int index = 0;
        while(var4.hasNext()) {
            String key = var4.next();
            log.info("========"+key);
            if(!list.contains(key)){
            	continue;
            }
            Object value = vars.get(key);
             content += (index == 0 ? "" : "&") + key+"=" + value;
           ++index;
        }

//
   String content = AlipaySignature.getSignContent(pmMap);

//String cc=StringEscapeUtils.unescapeJavaScript(StringEscapeUtils.unescapeHtml(StringEscapeUtils.escapeHtml(vars.get("content"))));

//log.info("ggggggggggggg"+cc);

//String content = "api_request_id="+(vars.get("api_request_id"))+"&app_id="+(vars.get("app_id"))+"&charset="+vars.get("charset")+"&content="+cc+"&method="+(vars.get("method"))+"&version="+(vars.get("version"))+"&timestamp="+(vars.get("timestamp"));
   String content = "123456";
   log.info("-----------------"+content);
            //签名
   String signResult = AlipaySignature.rsaSign(content, PRIVATE_KEY, "utf-8", "RSA");
   
//log.info("ggggggggggggg"+signResult);
   vars.put("sign",signResult);

此时你调试,会发现取不到值

需要添加一个用户自定义的变量,右键添加-配置元件-用户自定义的变量

 把参数在这里定义一遍 

然后再http请求里取值即可

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JMeter支持RSA公钥加密算法,可以使用Java Cryptography Extension (JCE)提供的RSA算法。以下是使用JMeter进行RSA公钥加密的步骤: 1. 首先,需要将公钥文件导入到JMeter中。在JMeter的“Test Plan”中,右键单击“Add”->“Config Element”->“Keystore Configuration”,然后在“Keystore Configuration”中选择“JKS”作为“Keystore Type”,并指定公钥文件的路径和密码。 2. 接下来,在JMeter的“Test Plan”中,右键单击“Add”->“Sampler”->“Debug Sampler”,然后在“Debug Sampler”中输入要加密的明文。 3. 在“Debug Sampler”中,添加一个“JSR223 Sampler”,并在“Script Language”中选择“groovy”。然后在“Script”中输入以下代码: ```groovy import java.security.KeyFactory; import java.security.spec.RSAPublicKeySpec; import javax.crypto.Cipher; import java.nio.charset.StandardCharsets; import java.util.Base64; String publicKey = vars.get("publicKey"); // 从变量中获取公钥 String plainText = vars.get("plainText"); // 从变量中获取明文 byte[] publicKeyBytes = Base64.getDecoder().decode(publicKey); // 将公钥字符串解码为字节数组 RSAPublicKeySpec keySpec = new RSAPublicKeySpec(publicKeyBytes, new byte[]{1,0,1}); // 构造公钥规范 KeyFactory keyFactory = KeyFactory.getInstance("RSA"); // 获取RSA密钥工厂 Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); // 获取RSA加密器 cipher.init(Cipher.ENCRYPT_MODE, keyFactory.generatePublic(keySpec)); // 初始化加密器 byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8)); // 加密明文 String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes); // 将加密后的字节数组编码为字符串 vars.put("encryptedText", encryptedText); // 将加密后的密文保存到变量中 ``` 4. 在“JSR223 Sampler”中,添加一个“View Results Tree”,并运行测试计划。在“View Results Tree”中,可以查看加密后的密文。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值