Jgit使用ssh免密认证

592 阅读1分钟

1、远端服务器配置git ssh免密

2、使用ssh免密认证需要自定义sshSessionFactory

public static void setSshSessionFactory() {
    //ssh session的工厂,用来创建密匙连接
    sshFactory = new JschConfigSessionFactory() {
        @override
        protected void configure(Host host, Session session) {
            session.setConfig("StrictHostKeyChecking", "no");
        }
        
        @override
        protected JSch createDefaultJSch(FS fs) throws JSchException {
            JSch sch = super.createDefaultJSch(fs);
            sch.addIdentity("~/.ssh/id_rsa");
            return sch;
        }
    };
}