jgit 指定ssh key 的保存路径

SShSessionFactory.java

import org.eclipse.jgit.transport.JschConfigSessionFactory;
import org.eclipse.jgit.transport.OpenSshConfig;
import org.eclipse.jgit.transport.OpenSshConfig.Host;
import org.eclipse.jgit.util.FS;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class MySShSessionFactory extends JschConfigSessionFactory {

    private String sshKeyFilePath;

    @Override
    protected JSch getJSch(final OpenSshConfig.Host hc, FS fs) throws JSchException {
        JSch jsch = new JSch();
        jsch.removeAllIdentity();
        jsch.addIdentity(sshKeyFilePath);
        jsch.setKnownHosts("C:\\known_hosts");
        return jsch;
    }

    @Override
    protected void configure(Host hc, Session session) {
        TODO Auto-generated method stub
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);

    }

    public String getSshKeyFilePath() {
        return sshKeyFilePath;
    }

    public void setSshKeyFilePath(String sshKeyFilePath) {
        this.sshKeyFilePath = sshKeyFilePath;
    }
}

登录发表评论 注册

blue_cliff

大佬厉害

新建主题 记录代码

学员动态

  • l**2添加了笔记:1、Integer是...
  • j**h评论了:您好 源码点击进去显...
  • s**a完成了课程的作业
反馈意见