Java 安全访问GitHub时出现未知HostKey异常

Java 安全访问GitHub时出现未知HostKey异常,java,ssl,github,jgit,Java,Ssl,Github,Jgit,我正在使用jgit安全地访问GitHub中的存储库。为了在GitHub和我的客户机代码之间生成安全通信的密钥,我执行了以下操作 生成密钥对: ssh-keygen -t rsa 使用帐户设置->SSH密钥->添加SSH密钥,将公钥添加到GitHub帐户 将步骤1中生成的私钥添加到本地主机,其中包含: ssh-add id_rsa 执行此操作后,当我尝试访问GitHub并进行克隆时,仍然会出现以下错误: org.eclipse.jgit.api.errors.TransportExcept

我正在使用jgit安全地访问GitHub中的存储库。为了在GitHub和我的客户机代码之间生成安全通信的密钥,我执行了以下操作

  • 生成密钥对:

    ssh-keygen -t rsa
    
  • 使用帐户设置->SSH密钥->添加SSH密钥,将公钥添加到GitHub帐户

  • 将步骤1中生成的私钥添加到本地主机,其中包含:

    ssh-add id_rsa
    
  • 执行此操作后,当我尝试访问GitHub并进行克隆时,仍然会出现以下错误:

    org.eclipse.jgit.api.errors.TransportException: git@github.com:test/test_repo.git: UnknownHostKey: github.com. RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48
    at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:137)
    at org.eclipse.jgit.api.CloneCommand.fetch(CloneCommand.java:178)
    at org.eclipse.jgit.api.CloneCommand.call(CloneCommand.java:125)
    
    这是我使用的代码:

        String localPath, remotePath;
        Repository localRepo;
        Git git;
    
        localPath = <path_to_local_repository>;
        remotePath = "git@github.com:test/test_repo.git";
    
        try {
            localRepo = new FileRepository(localPath + "/.git");
        } catch (IOException e) {
            e.printStackTrace();
        }
        git = new Git(localRepo);
    
        CloneCommand cloneCmd =  git.cloneRepository().
                    setURI(remotePath).
                    setDirectory(new File(localPath));
            try {
                cloneCmd.call();
            } catch (GitAPIException e) {
                log.error("git clone operation failed");
                e.printStackTrace();
            }
    
    字符串localPath,remotePath;
    存储库localRepo;
    吉特吉特;
    localPath=;
    远程路径=”git@github.com:test/test_repo.git”;
    试一试{
    localRepo=newfilerepository(localPath+“/.git”);
    }捕获(IOE异常){
    e、 printStackTrace();
    }
    git=新git(localRepo);
    CloneCommand cloneCmd=git.cloneRepository()。
    setURI(远程路径)。
    setDirectory(新文件(localPath));
    试一试{
    cloneCmd.call();
    }捕获(Gitapie异常){
    日志错误(“git克隆操作失败”);
    e、 printStackTrace();
    }
    
    请让我知道这里的问题,我应该怎么做来纠正它


    谢谢。

    之所以发生这种情况,是因为在
    ~/.ssh/known_hosts
    中没有github的条目,在这种情况下,jgit中使用的
    JSch
    默认为拒绝会话。有关解决方案,请参阅此问题:

    要设置ssh会话属性,需要为jgit创建会话工厂:

    SshSessionFactory.setInstance(new JschConfigSessionFactory() {
      public void configure(Host hc, Session session) {
        session.setConfig("StrictHostKeyChecking", "no");
      }
    })
    

    或者将
    strichhostkeychecking=no
    添加到
    ~/.ssh/config

    ,因为此线程是第一个结果:

    com.jcraft.jsch.JSchException:UnknownHostKey:gitservername。RSA密钥 “指纹”

    如果问题仍然存在,唯一的答案是禁用StricHostKeyChecking,这是出于安全目的不可接受的

    如果问题仍然存在,您应该从另一个线程查看此答案:

    解决长期存在的问题的办法是:

    ssh-keyscan -H -t rsa gitservername >> ~/.ssh/known_hosts
    

    我确实有一个条目,但仍然得到相同的错误,我使用StrictHostKeyChecking=no作为解决方法,但不想让我的代码容易受到攻击。有什么想法吗?通过在~/.ssh下添加一个配置文件,其中包含两行Host*和StrictHostKeyChecking no,解决了这个问题