Java 为 JGit 指定 SSH 密钥

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23692747/
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-08-14 00:44:17  来源:igfitidea点击:

Specifying SSH key for JGit

javagitsshjschjgit

提问by nstbayless

I'm wondering how I can use jgit to connect to github using a specified ssh key file (i.e. one not in ~/.ssh/).

我想知道如何使用 jgit 使用指定的 ssh 密钥文件(即不在~/.ssh/.

Unfortunately, I'm not sure how to use JschConfigSessionFactoryproperly. I've tried creating a setup just like the one in this article: Using Keys with JGit to Access a Git Repository Securely

不幸的是,我不确定如何JschConfigSessionFactory正确使用。我已经尝试创建一个类似于本文中的设置:使用密钥和 JGit 安全地访问 Git 存储库

I call git using git.push().setRemote(remotePath).call();However, I get this error (specific repository omitted from log):

我使用 git 调用git.push().setRemote(remotePath).call();但是,我收到此错误(日志中省略了特定存储库):

org.eclipse.jgit.api.errors.TransportException: https://github.com/user/repo: not authorized
    at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:160)
    at gitio.GitInterface.pushToRemote(GitInterface.java:145)
    at engine.GitInterfaceTester.main(GitInterfaceTester.java:25)
Caused by: org.eclipse.jgit.errors.TransportException: https://github.com/user/repo: not authorized
    at org.eclipse.jgit.transport.TransportHttp.connect(TransportHttp.java:479)
    at org.eclipse.jgit.transport.TransportHttp.openPush(TransportHttp.java:396)
    at org.eclipse.jgit.transport.PushProcess.execute(PushProcess.java:154)
    at org.eclipse.jgit.transport.Transport.push(Transport.java:1173)
    at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:156)
    ... 2 more

I've noticed that the custom overriden methods in JschConfigSessionFactoryaren't ever actually invoked. This is almost certainly the cause of the problem... but don't know why they're not invoked; I pass the custom JschConfigSessionFactoryto SshSessionFactoryusing SshSessionFactory.setInstance(sessionFactory);

我注意到自定义覆盖方法JschConfigSessionFactory实际上从未被调用过。这几乎可以肯定是问题的原因……但不知道为什么没有调用它们;我将自定义传递JschConfigSessionFactorySshSessionFactory使用SshSessionFactory.setInstance(sessionFactory);

Does anybody know what I'm doing wrong?

有谁知道我做错了什么?

采纳答案by Rüdiger Herrmann

GitHub uses password authentication for https URLs and public-key authentication for SSH URLs (see https://gist.github.com/grawity/4392747).

GitHub 对 https URL 使用密码身份验证,对 SSH URL 使用公钥身份验证(请参阅https://gist.github.com/grawity/4392747)。

The SshSessionFactoryis only consulted for SSH URLs like [email protected]:user/repo.git. To authenticate for an https URL you can use the CredentialsProvider. The base class for commands that may establish connections is TransportCommandand has a setCredentialsProvider()method. If you equip the PushCommandwith a CredentialsProviderthat supplies user name and password, you should be able to successfully establish a connection.

SshSessionFactory只征询SSH的URL像[email protected]:用户/ repo.git。要对 https URL 进行身份验证,您可以使用CredentialsProvider. 可以建立连接的命令的基类是TransportCommand并且有一个setCredentialsProvider()方法。如果装备PushCommandCredentialsProvider该设备的用户名和密码,你应该能够成功建立连接。

For further details about authenticating with JGit you may want to read this article: http://www.codeaffine.com/2014/12/09/jgit-authentication/

有关使用 JGit 进行身份验证的更多详细信息,您可能需要阅读这篇文章:http: //www.codeaffine.com/2014/12/09/jgit-authentication/