首页 > 解决方案 > 如何在 JMeter 中使用 JGit 添加 ssh 密钥

问题描述

我正在尝试使用 JMeter 在 Bitbucket 服务器上进行负载测试。我提到了这个链接Jmeter and Bitbucket server load testing,但我不确定如何在 JMeter 的 JSR223 Sampler 中使用 JGit 传递 ssh-key 详细信息。

谢谢您的帮助!

标签: jmeterjgitjsr223

解决方案


JGit 使用JSch库通过 SSH 通道进行 Git 连接,因此您需要调用JSch.addIdentity() 函数并提供您的私钥/公钥/密码的路径。

示例代码:

import com.jcraft.jsch.JSch
import com.jcraft.jsch.JSchException
import com.jcraft.jsch.Session
import org.eclipse.jgit.api.CloneCommand
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.api.TransportConfigCallback
import org.eclipse.jgit.transport.*
import org.eclipse.jgit.util.FS

CloneCommand cloneCommand = Git.cloneRepository()
cloneCommand.setURI("your Git SSH URL")
cloneCommand.setDirectory(new File('/path/to/local/folder'))
SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
    @Override
    protected JSch createDefaultJSch(FS fs) throws JSchException {
        JSch defaultJSch = super.createDefaultJSch(fs);
        defaultJSch.addIdentity("/path/to/your/private/key");
        return defaultJSch;
    }

    @Override
    protected void configure(OpenSshConfig.Host host, Session session) {
        SshTransport sshTransport = (SshTransport) transport;
        sshTransport.setSshSessionFactory(sshSessionFactory);
    }
}
cloneCommand.setTransportConfigCallback(new TransportConfigCallback() {
    @Override
    void configure(Transport transport) {
        SshTransport sshTransport = (SshTransport) transport;
        sshTransport.setSshSessionFactory(sshSessionFactory);
    }
})
cloneCommand.call()

更多信息:


推荐阅读