java 安全访问 GitHub 时出现 UnknownHostKey 异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13396534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:33:49  来源:igfitidea点击:

UnknownHostKey Exception in Accessing GitHub Securely

javasslgithubjgit

提问by Izza

I'm using jgit to access a repository in GitHub securely. I did the following to generate keys for secure communication between GitHub and my client code.

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

  1. Generated the key pair:

    ssh-keygen -t rsa
    
  2. Added the public key to GitHub account with Account Settings -> SSH keys -> add SSH key

  3. Added the private key generated in step 1 to the local host with:

    ssh-add id_rsa
    
  1. 生成密钥对:

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

  3. 将步骤 1 中生成的私钥添加到本地主机:

    ssh-add id_rsa
    

After doing this, when I try to access GitHub and make a clone, I still get the following error:

这样做之后,当我尝试访问 GitHub 并进行克隆时,我仍然收到以下错误:

org.eclipse.jgit.api.errors.TransportException: [email protected]: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)

This is the code that I used:

这是我使用的代码:

    String localPath, remotePath;
    Repository localRepo;
    Git git;

    localPath = <path_to_local_repository>;
    remotePath = "[email protected]: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();
        }

Kindly let me know the issue here and what should I do to rectify it.

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

Thanks.

谢谢。

回答by Denis Tulskiy

It happens because you have no entry for github in ~/.ssh/known_hosts, and JSchused in jgit defaults to rejecting session in this case. Refer to this question for solutions: com.jcraft.jsch.JSchException: UnknownHostKey

发生这种情况是因为在 中没有 github 条目,在这种情况下~/.ssh/known_hostsJSch在 jgit 中使用默认拒绝会话。参考这个问题的解决方案:com.jcraft.jsch.JSchException: UnknownHostKey

To set the ssh session property, you need to create a session factory for jgit:

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

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

or add StrictHostKeyChecking=noto ~/.ssh/config

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

回答by Pelsce

As this thread is first result to :

由于此线程是第一个结果:

com.jcraft.jsch.JSchException: UnknownHostKey: gitservername. RSA key fingerprint"

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

and the only answer, if the problem persists, is to disable StrictHostKeyChecking, which is not acceptable for security purposes.

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

If the problem persists, you should have a look to this answer from another thread :

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

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

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

What solved the persisting problem is :

解决这个持续存在的问题的是:

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