10

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

  1. 生成密钥对:

    ssh-keygen -t rsa
    
  2. 使用 Account Settings -> SSH keys -> add SSH key 将公钥添加到 GitHub 帐户

  3. 将步骤 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();
        }

请让我知道这里的问题以及我应该怎么做才能纠正它。

谢谢。

4

2 回答 2

22

发生这种情况是因为您在 中没有 github 条目~/.ssh/known_hosts,并且JSch在这种情况下,在 jgit 中使用默认为拒绝会话。有关解决方案,请参阅此问题:com.jcraft.jsch.JSchException: UnknownHostKey

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

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

或添加StrictHostKeyChecking=no~/.ssh/config

于 2012-11-15T11:55:56.247 回答
4

因为这个线程是第一个结果:

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

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

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

https://stackoverflow.com/a/44777270/13184312

解决持续存在的问题是:

ssh-keyscan -H -t rsa gitservername >> ~/.ssh/known_hosts
于 2020-04-03T16:48:25.867 回答