加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 安全访问GitHub时的UnknownHostKey异常

发布时间:2020-12-15 00:06:08 所属栏目:Java 来源:网络整理
导读:我正在使用jgit安全地访问 GitHub中的存储库.我做了以下操作来生成GitHub和我的客户端代码之间的安全通信密钥. 生成密钥对: ssh-keygen -t rsa 使用帐户设置 – 向GitHub帐户添加了公钥. SSH密钥 – 添加SSH密钥 将步骤1中生成的私钥添加到本地主机: ssh-a
我正在使用jgit安全地访问 GitHub中的存储库.我做了以下操作来生成GitHub和我的客户端代码之间的安全通信密钥.

>生成密钥对:

ssh-keygen -t rsa

>使用帐户设置 – >向GitHub帐户添加了公钥. SSH密钥 – >添加SSH密钥
>将步骤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();
        }

请告诉我这里的问题,我该怎么做才能纠正它.

谢谢.

解决方法

之所以发生这种情况,是因为?/ .ssh / known_hosts中没有github的条目,而jgit中使用的JSch默认拒绝会话.有关解决方案,请参阅此问题: 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读