9

我正在尝试使用 JGit 克隆 Git 存储库,但 UnsupportedCredentialItem 有问题。

我的代码:

FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder.setGitDir(PATH).readEnvironment().findGitDir().build();

Git git = new Git(repository);              
CloneCommand clone = git.cloneRepository();
clone.setBare(false);
clone.setCloneAllBranches(true);
clone.setDirectory(PATH).setURI(url);
UsernamePasswordCredentialsProvider user = new UsernamePasswordCredentialsProvider(login, password);                
clone.setCredentialsProvider(user);
clone.call();   

会出现异常:

 org.eclipse.jgit.errors.UnsupportedCredentialItem: ssh://git@github.com:22: Passphrase for C:\Users\Marek\.ssh\id_rsa at
 org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider.get(UsernamePasswordCredentialsProvider.java:110)....

但是如果我在 .ssh\ 中删除文件 known_hosts 会出现不同的异常

org.eclipse.jgit.errors.UnsupportedCredentialItem: ssh://git@github.com:22: The authenticity of host 'github.com' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting?
at org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider.get(UsernamePasswordCredentialsProvider.java:110)....

是否有可能对该问题输入“是”或跳过它?

谢谢!

4

7 回答 7

5

我认为如果您使用用户名和密码登录,则需要 https。对于 ssh,您需要一个与 github 记录的公钥相匹配的公钥。

于 2012-04-06T01:15:38.803 回答
3

如果将用户名/密码与 ssh 一起使用,这将做到这一点(就像@michals,只有更少的代码)

public void gitClone() throws GitAPIException {
    final File localPath = new File("./TestRepo");
    Git.cloneRepository()
        .setURI(REMOTE_URL)
        .setDirectory(localPath)
        .setCredentialsProvider(new UsernamePasswordCredentialsProvider("***", "***"))
        .call();
}
于 2014-01-12T20:00:23.807 回答
3

我想你会想检查 github 帮助:

http://help.github.com/win-set-up-git/

尤其是关于生成 ssh 密钥的部分(ssh-keygen -t rsa -C "your_email@youremail.com")。阅读适合您环境的文章,您将了解如何获得更好的配置。

于 2011-12-19T22:51:00.557 回答
2

我有同样的问题。原因是为 rsa 私钥设置了密码。当我删除此密钥的密码时,它开始工作而没有任何CredentialsProvider.

UsernamePasswordCredentialsProvider可能不支持密码。如果您想设置密码,您可以定义自己的 CredentialProvider,它将支持它,例如:

CloneCommand clone = Git.cloneRepository()
    .setURI("...")
    .setCredentialsProvider(new CredentialsProvider() {

        @Override
        public boolean supports(CredentialItem... items) {
            return true;
        }

        @Override
        public boolean isInteractive() {
            return true;
        }

        @Override
        public boolean get(URIish uri, CredentialItem... items)
                throws UnsupportedCredentialItem {

            for (CredentialItem item : items) {
                    if (item instanceof CredentialItem.StringType) {
                        ((CredentialItem.StringType) item).
                            setValue(new String("YOUR_PASSPHRASE"));
                        continue;
                    }
                }
                return true;
            }
        });

clone.call();

这个对我有用 ;)

于 2013-06-14T12:51:53.097 回答
2

我有一个类似的问题,虽然我的设置有点不同。将其留在这里以防其他人遇到类似情况。根据本教程,我已经覆盖了我的配置方法和 createDefaultJSch 方法:https ://www.codeaffine.com/2014/12/09/jgit-authentication/

我有类似的东西:

@Override
public void configure( Transport transport ) {
  SshTransport sshTransport = ( SshTransport )transport;
  sshTransport.setSshSessionFactory( sshSessionFactory );
}

@Override
protected JSch createDefaultJSch( FS fs ) throws JSchException {
  JSch defaultJSch = super.createDefaultJSch( fs );
  defaultJSch.addIdentity( "/path/to/private_key" );
  return defaultJSch;
}

我最终将 createdDefaultJSch 方法更改为 getSch(添加适当的参数)并添加 removeAllIdentity():

@Override
public 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
} 

不知道为什么会这样,但我从这个答案中找到了 getSch 的东西(巧合的是写教程的那个人):Using Keys with JGit to Access a Git Repository Securely

于 2019-01-24T09:54:16.647 回答
1

我不清楚您是要进行用户名/密码身份验证还是公钥/私钥身份验证。无论哪种方式,CredentialsProvider都不会被使用,据此。您需要配置传输。首先,创建一个传输配置回调:

SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
  @Override
  protected void configure( Host host, Session session ) {
    // If you are using username/password authentication, add the following line
    session.setPassword( "password" );
  }
} );

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

然后用它配置命令:

clone.setTransportConfigCallback( transportConfigCallback );
于 2020-02-27T18:35:49.790 回答
0

如果存储库是私有的并且需要身份验证,您(@Scruger)将使用用户名/密码和 ssh 来克隆存储库。

private UsernamePasswordCredentialsProvider configAuthentication(String user, String password) {
            return new UsernamePasswordCredentialsProvider(user, password ); 
        }

    public void clonneRepositoryWithAuthentication(String link, String directory,String branch,String user, String password){
         System.out.println("cloning repository private from bitcketebuk");
        try {
            Git.cloneRepository()//function responsible to clone repository
                                       .setURI(link)// set link to repository git
                                       .setDirectory(new File(Constants.PATH_DEFAULT + directory))//Defined the path local the cloning
                                       .setCredentialsProvider(configAuthentication(user, password))
                                       .setCloneAllBranches(true)//Defined clone all branch exists on repository
                                       .call();//execute call the clone repository git
            System.out.println("Cloning sucess.....");
        } catch (GitAPIException e) {
            System.err.println("Error Cloning repository " + link + " : "+ e.getMessage());
        }

    }
于 2018-05-20T10:00:39.047 回答