PushCommand抛出JSchException:UnknownHostKey

时间:2016-07-04 12:13:28

标签: java git ssh jgit

我使用JGit调用推送命令时未将StrictHostKeyChecking设置为NO,但我收到以下错误:

UnknownHostKey: <server hostname>. RSA key fingerprint is: <...>

SSH密钥在服务器上设置,known_hosts文件包含服务器主机名。 我可以从命令行进行推送,但不能从代码中进行推送。

这是我的代码:

SshSessionFactory sshSessionFactory = new JschConfigSessionFactory()
    {
        @Override
        protected void configure(final OpenSshConfig.Host hc, Session session) 
        {
            session.setPassword(passphrase);
        }

        @Override
        protected JSch createDefaultJSch(FS fs) throws JSchException
        {
            JSch jsch = super.createDefaultJSch(fs);
            jsch.removeAllIdentity();
            jsch.addIdentity("path to id_rsa");
            jsch.setKnownHosts("path to known_hosts");
            return jsch;
        }

    };

    //SshSessionFactory.setInstance(sshSessionFactory);

    TransportConfigCallback tcc = new TransportConfigCallback()
    {
        @Override
        public void configure(Transport transport) 
        {
            SshTransport sshTransport = (SshTransport) transport;
            sshTransport.setSshSessionFactory(sshSessionFactory);
        }
    };

    pushCommand.setTransportConfigCallback(tcc);
    pushCommand.setPushTags().call();

0 个答案:

没有答案