java - USERAUTH 无法使用 JGit 为 PullCommand() 安全地访问 git 存储库

标签 java git authentication jsch jgit

我正在尝试使用 JGit 的 API 和以下代码进行 git pull

public class gitHubTest {
    JSch jsch = new JSch();

    // Defining the private key file location explicitly
    String userHome = System.getProperty("user.home");
    String privateKey = userHome + "/.ssh/id_rsa";
    String knownHostsFile = userHome + "/.ssh/known_hosts";
    Repository localRepo = new FileRepository("/LocalPath/.git");

    public gitHubTest() throws Exception {
        jsch.setConfig("StrictHostKeyChecking", "no");
        jsch.setKnownHosts(knownHostsFile);
        jsch.addIdentity(privateKey);
        System.out.println("privateKey :" + privateKey);
        Git git = new Git(localRepo);
        PullCommand pullcmd = git.pull();
        pullcmd.call();
    }
}

错误堆栈跟踪:

org.eclipse.jgit.api.errors.TransportException: git@github.example.com:remote.git: USERAUTH fail
at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:245)
at org.eclipse.jgit.api.PullCommand.call(PullCommand.java:288)
at gitHubTest.<init>(gitHubTest.java:47)
at WebhooksServer.main(WebhooksServer.java:13)
Caused by: org.eclipse.jgit.errors.TransportException: git@github.example.com:remote.git: USERAUTH fail
at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:160)
at org.eclipse.jgit.transport.SshTransport.getSession(SshTransport.java:137)
at org.eclipse.jgit.transport.TransportGitSsh$SshFetchConnection.<init>(TransportGitSsh.java:274)
at org.eclipse.jgit.transport.TransportGitSsh.openFetch(TransportGitSsh.java:169)
at org.eclipse.jgit.transport.FetchProcess.executeImp(FetchProcess.java:136)
at org.eclipse.jgit.transport.FetchProcess.execute(FetchProcess.java:122)
at org.eclipse.jgit.transport.Transport.fetch(Transport.java:1236)
at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:234)
... 3 more

Caused by: com.jcraft.jsch.JSchException: USERAUTH fail
at com.jcraft.jsch.UserAuthPublicKey.start(UserAuthPublicKey.java:119)
at com.jcraft.jsch.Session.connect(Session.java:470)
at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:117)
... 10 more

我检查过的一些建议表明,我们需要实例化 JschConfigSessionFactory 然后重写 configure() 方法来传递 passphrase 。我已经尝试这样做了。然后它显示一个错误。我已经提到了http://www.codeaffine.com/2014/12/09/jgit-authentication/读起来恰到好处,但不适合我的 PullCommand。

有人可以帮忙吗?我已经阅读并尝试了这里的很多帖子,但没有任何内容能够准确解决我的问题。

使用configure()方法的代码实现:

public class gitHubTest {
JSch jsch = new JSch();
String userHome = System.getProperty("user.home");
String privateKey = userHome + "/.ssh/id_rsa";
String knownHostsFile = userHome + "/.ssh/known_hosts";

public gitHubTest() throws IOException, JSchException, GitAPIException {
    Repository localRepo = new FileRepository("/LocalPath/branch.git");
    final String remoteURL = "git@github.example.com:remote.git";
    JSch.setConfig("StrictHostKeyChecking", "no");
    jsch.setKnownHosts(knownHostsFile);
    jsch.addIdentity(privateKey);
    JschConfigSessionFactory sessionFactory = new JschConfigSessionFactory() {
    @Override
    protected void configure(OpenSshConfig.Host host, Session session) {
        CredentialsProvider cp = new CredentialsProvider() {
            @Override
            public boolean isInteractive() {
                return false;
            }
            @Override
            public boolean supports(CredentialItem... credentialItems) {
                return false;
            }
            @Override
            public boolean get(URIish urIish, CredentialItem... credentialItems) throws UnsupportedCredentialItem {
                return false;
            }
        };
        UserInfo userInfo = new CredentialsProviderUserInfo(session,cp);
        session.setUserInfo(userInfo);
    }
    };
SshSessionFactory.setInstance(sessionFactory);
Git git = new Git(localRepo);
PullCommand pullcmd = git.pull();
pullcmd.call();
}}

这会产生相同的错误。

最佳答案

我可以找出我面临的一些问题。解决办法如下:

  1. 如果必须在没有密码的情况下进行身份验证,则生成没有密码的 id_rsa

  2. 我们需要覆盖getJSch(final OpenSshConfig.Host hc, FS fs)在 SshSessionfactory 的配置中使用 addIdentity:

    SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
        @Override
        protected void configure(OpenSshConfig.Host host, Session sess ion) {
            session.setConfig("StrictHostKeyChecking", "no");
        }
    
        @Override
        protected JSch getJSch(final OpenSshConfig.Host hc, FS fs) throws JSchException {
            JSch jsch = super.getJSch(hc, fs);
            jsch.removeAllIdentity();
            jsch.addIdentity("/path/to/private/key");
            return jsch;
        }
    };
    
  3. 我们需要调用需要以不同方式实例化:

    PullCommand pull = git.pull().setTransportConfigCallback(new TransportConfigCallback() {
    
        @Override
        public void configure(Transport transport) {
            SshTransport sshTransport = (SshTransport) transport;
            sshTransport.setSshSessionFactory(sshSessionFactory);
        }
    });
    

然后调用pull实例:

PullResult pullResult = pull.call();

我希望这会有所帮助。

关于java - USERAUTH 无法使用 JGit 为 PullCommand() 安全地访问 git 存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45889326/

相关文章:

java - Future.get 无法阻塞 forkjoinpool/workstealingpool 中的线程

linux - 'etckeeper' 使用哪个用户名进行推送?

apache - 允许 paypal ipn 绕过 .htaccess 中的 Basic Auth

ios - Swift - 在面部识别运行后,使用 Apple 登录始终会导致 'Sign up not completed'

tomcat 配置为不遵守证书的扩展 key 使用

java - 如何自动化重复过程以在项目中添加新字段?

java - 当解码列表数据时,jaxb 是否只需要定义一个类

Java 8 - 在 lambda 中抛出多个通用检查异常

git fetch --quiet 正在写入标准输出而不是保持沉默

macos - 使用 Homebrew 升级 Git 时出现问题