如何在 Java 代码中使用 jmeter 库添加密钥库文件

How to add keystore file by using jmeter library in Java code

就像我尝试通过转到 Options > 在 jmeter 应用程序中添加 keystore.jks 文件一样SSL 管理器 > 选择 .jks 文件,在 运行 时我们需要提供 password/passphrase对于那个文件,它会起作用,就像我试图在 Java 中使用 jmeter 库模仿类似的东西一样,但找不到任何替代方法。

有什么方法可以使用 jmeter 库通过 Java 代码添加 .jks 文件,从而实现与我在 jmeter 应用程序中所做的类似的事情?

这是我尝试使用 jmeter 库用于 java、ALIAS_VALUE、ENDPOINT_VALUE、CONTENT_PATH_VALUE、API_KEY_VALUE 的方法出于安全考虑屏蔽原始值

StandardJMeterEngine jmeterEngine = new StandardJMeterEngine();
        JMeterUtils.setJMeterHome("C:/Users/pmaiya/Downloads/apache-jmeter-5.2.1/apache-jmeter-5.2.1");
        JMeterUtils.loadJMeterProperties("C:/Users/pmaiya/Downloads/apache-jmeter-5.2.1/apache-jmeter-5.2.1/bin/jmeter.properties");
        JMeterUtils.initLocale();
        System.setProperty("javax.net.ssl.keyStore", "C:/Users/pmaiya/Documents/Zoom/identity.jks");
        System.setProperty("javax.net.ssl.keyStorePassword", "123456");

        HashTree testPlanHashTree = new HashTree();




        JmeterKeyStore.getInstance("JKS").load(new FileInputStream("C:/Users/pmaiya/Documents/Zoom/identity.jks"), "123456");
        JmeterKeyStore.DEFAULT_ALIAS_VAR_NAME.contentEquals("ALIAS_VALUE");

        JmeterKeyStore.getInstance("JKS", 0, -1, "test").load(new FileInputStream("C:/Users/pmaiya/Documents/Zoom/identity.jks"), "123456");


       SSLManager.getInstance().configureKeystore(true, 0, -1, "ALIAS_VALUE");
       SSLManager.isSSLSupported();





        // create http sampler
        HTTPSamplerProxy httpSampler = new HTTPSamplerProxy();
        httpSampler.setProtocol("HTTPS");
        httpSampler.setDomain("ENDPOINT_VALUE");
        httpSampler.setPath("CONTEXT_PATH_VALUE");
        httpSampler.setMethod("GET");
        httpSampler.setName("get call");
        httpSampler.setFollowRedirects(true);
        httpSampler.setAutoRedirects(false);
        httpSampler.setUseKeepAlive(true);
        httpSampler.setEnabled(true);
        httpSampler.setProperty(TestElement.TEST_CLASS, HTTPSampler.class.getName());
        httpSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());


        HeaderManager manager = new HeaderManager();
        manager.setEnabled(true);
        manager.add(new Header("apikey", "API_KEY_VALUE"));
        manager.add(new Header("Content-Type", "application/json"));
        manager.setName("headermanager"); 
        manager.setProperty(TestElement.TEST_CLASS, HeaderManager.class.getName());
        manager.setProperty(TestElement.GUI_CLASS, HeaderPanel.class.getName());





        LoopController loopController = new LoopController();
        loopController.setLoops(1);
        loopController.setFirst(true);
        loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
        loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
        loopController.initialize();

        //create thread group
        ThreadGroup threadGroup = new ThreadGroup();
        threadGroup.setNumThreads(1);
        threadGroup.setRampUp(1);
        threadGroup.setSamplerController(loopController);
        threadGroup.setName("get");
        threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
        threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());

        //create test plan
        TestPlan testPlan = new TestPlan();
        testPlan.setEnabled(true);
        testPlan.setName("Test Plan");
        testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
        testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
        testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());



        //Add sampler to the thread group


        HashTree threadGroupHashTree = testPlanHashTree.add(testPlan, threadGroup);
        threadGroupHashTree.add(httpSampler);
       threadGroupHashTree.add(manager);



        // Generating the JMX file
        SaveService.saveTree(testPlanHashTree, new FileOutputStream("C:/Users/pmaiya/Desktop/TestSSL.jmx"));
        jmeterEngine.configure(testPlanHashTree);
        jmeterEngine.run();            
         System.out.println("executed successfully");

如果您的密钥库仅包含一个您需要用于客户端加密的证书 - 设置 javax.net.ssl.keyStorejavax.net.ssl.keyStorePassword,只需确保调用这些函数:

System.setProperty("javax.net.ssl.keyStore", "C:/Users/pmaiya/Documents/Zoom/identity.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "123456");

这一行之前

JMeterUtils.loadJMeterProperties("C:/Users/pmaiya/Downloads/apache-jmeter-5.2.1/apache-jmeter-5.2.1/bin/jmeter.properties");

如果您的密钥库包含 >1 个证书,并且您希望每个 JMeter 虚拟用户使用自己的证书 - 选择 Keystore Configuration (the relevant JMeter API class name is org.apache.jmeter.config.KeystoreConfig) like it's described in How to Use Multiple Certificates When Load Testing Secure Websites article