错误 - org.apache.jorphan.util.JMeterException:调用 bsh 方法时出错:eval 源文件:

Error - org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file:

我遇到类似“org.apache.jorphan.util.JMeterException 的错误:调用 bsh 方法时出错:eval 源文件:内联评估:``import android.util.Base64;import java.security.spec.X509EncodedKeySpec; import . . . '' : Typed variable declaration : Class: def not found in namespace " while 运行 Jmeter 中的以下代码片段:

import android.util.Base64;
import java.security.spec.X509EncodedKeySpec;
import java.security.Key;
import java.security.KeyFactory;
import java.security.PublicKey;
import javax.crypto.Cipher;

String Original_String= ctx.getPreviousResult().getResponseDataAsString();
String Trim1=Original_String.substring(0, Original_String.lastIndexOf(","));
String Trim2=Trim1.replaceFirst("-----BEGIN PUBLIC KEY-----", "");
String Trim3=Trim2.replaceFirst("-----END PUBLIC KEY-----", "");
String[] parts = Trim3.split(":");
String myString = parts[1].substring(1, parts[1].length()-1);
String final_string=myString.replaceAll("\\n", "");
log.info(final_string);
String input="4000221111111111";

try{
        byte[] byteKey = Base64.decode(final_string.getBytes(), Base64.DEFAULT);
        X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        Key k=kf.generatePublic(X509publicKey);
        //return (PublicKey) k;
        def cipher = Cipher.getInstance("RSA/NONE/OAEPPadding", "BC");
        cipher.init(Cipher.ENCRYPT_MODE, k);
        byte[] encryptedBytes = cipher.doFinal(input.getBytes());
        SampleResult.setResponseData( Base64.encodeToString(encryptedBytes, Base64.NO_WRAP));
}
    catch(Exception e){
        e.printStackTrace();
    }

我已将 android.util.Base64 jar 文件放在 Jmeter 的 lib 文件夹中。对此的帮助很有帮助!

当您尝试在 Beanshell 采样器中使用 Groovy def keyword 时出现此错误,该采样器在那里不受支持,选项位于:

  1. 完全删除它,在 Beanshell 中你可以初始化你的 cipher 变量而无需任何辅助关键字,如:

    cipher = Cipher.getInstance("RSA/NONE/OAEPPadding", "BC");
    
  2. (更好)切换到 JSR223 Sampler and make sure groovy is selected in the "Language" dropdown. Since JMeter 3.1 it is recommended to use Groovy for scripting as it has much better performance comparing to Beanshell

因为你使用的是 Beanshell,你脚本中的 def 应该是 var.

否则使用 JSR223 测试元素切换到 groovy