通过ssh使用用户名和密码通过Java克隆git存储库 [英] Clone git repository over ssh with username and password by Java

查看:1714
本文介绍了通过ssh使用用户名和密码通过Java克隆git存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过ssh使用Java克隆一个git项目。我有一个git-shell用户的用户名和密码作为凭据。我可以使用以下命令在终端中克隆项目,没有问题。 (当然,它首先要求输入密码)

  git clone user @ HOST:/path/Example.git 

但是,当我尝试使用JGIT API的以下代码时:

 文件localPath = new File(TempProject); 
.git.cloneRepository()
.setURI(ssh://HOST/path/example.git)
.setDirectory(localPath)
.setCredentialsProvider(new UsernamePasswordCredentialsProvider( ***,***))
.call();

我得到了

 线程main中的异常org.eclipse.jgit.api.errors.TransportException:ssh://HOST/path/example.git:身份验证失败

我该怎么办?有任何想法吗? (我使用OSX 10.9.4和JDK 1.8)

解决方案

对于SSH验证,JGit使用 JSch 。 JSch提供了一个 SshSessionFactory 来创建和配置SSH连接。告诉JGit应该使用哪个SSH会话工厂的最快方法是通过 SshSessionFactory.setInstance()来全局设置它。



JGit提供了一个抽象的 JschConfigSessionFactory ,它的 configure 方法可以被覆盖以提供密码:



$ p $ SshSessionFactory.setInstance(new JschConfigSessionFactory(){
@Override
protected void configure(Host host,Session session){
session.setPassword(password);
}
});
Git.cloneRepository()
.setURI(ssh://username@host/path/repo.git)
.setDirectory(/ path / to / local / repo)
.call();

设置 SshSessionFactory 更合理方式稍微复杂一些。 CloneCommand - 像所有可能打开连接的JGit命令类 - 从 TransportCommand 继承。这个类有一个 setTransportConfigCallback()方法,它也可以用来为实际命令指定SSH会话工厂。

  CloneCommand cloneCommand = Git.cloneRepository(); 
cloneCommand.setTransportConfigCallback(new TransportConfigCallback(){
@Override
public void configure(Transport transport){$ b $ if(transport instanceof SshTransport){
SshTransport sshTransport =( SshTransport)transport;
sshTransport.setSshSessionFactory(...);
}
}
});


I am trying to clone a git project with Java over ssh. I have username and password of a git-shell user as credentials. I can clone the project in terminal using the following command with no problem. (Of course, it asks for the password first)

git clone user@HOST:/path/Example.git

However when I try the following code using JGIT api

File localPath = new File("TempProject");
Git.cloneRepository()
    .setURI("ssh://HOST/path/example.git")
    .setDirectory(localPath)
    .setCredentialsProvider(new UsernamePasswordCredentialsProvider("***", "***"))
    .call();

I got

Exception in thread "main" org.eclipse.jgit.api.errors.TransportException: ssh://HOST/path/example.git: Auth fail

What should I do? Any ideas? (I am using OSX 10.9.4 and JDK 1.8)

解决方案

For authentication with SSH, JGit uses JSch. JSch provides an SshSessionFactory to create and dispose SSH connections. The quickest way to tell JGit which SSH session factory should be used is to set it globally through SshSessionFactory.setInstance().

JGit provides an abstract JschConfigSessionFactory, whose configure method can be overridden to provide the password:

SshSessionFactory.setInstance( new JschConfigSessionFactory() {
    @Override
    protected void configure( Host host, Session session ) {
      session.setPassword( "password" );
    }
} );
Git.cloneRepository()
  .setURI( "ssh://username@host/path/repo.git" )
  .setDirectory( "/path/to/local/repo" )
  .call();

To set the SshSessionFactory in a more sensible way is slightly more complex. The CloneCommand - like all JGit command classes that may open a connection - inherits from TransportCommand. This class has a setTransportConfigCallback() method that can also be used to specify the SSH session factory for the actual command.

CloneCommand cloneCommand = Git.cloneRepository();
cloneCommand.setTransportConfigCallback( new TransportConfigCallback() {
  @Override
  public void configure( Transport transport ) {
    if( transport instanceof SshTransport ) {
      SshTransport sshTransport = ( SshTransport )transport;
      sshTransport.setSshSessionFactory( ... );
    }
  }
} );

这篇关于通过ssh使用用户名和密码通过Java克隆git存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆