【问题标题】:JGit clone repositoryJGit 克隆仓库
【发布时间】:2023-03-06 22:15:01
【问题描述】:

我正在尝试使用 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)....

是否可以对该问题输入“是”或直接跳过它?

谢谢!

【问题讨论】:

    标签: repository clone jgit


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      如果在 ssh 中使用用户名/密码,这将完成(就像 @michals,只有更少的代码)

      public void gitClone() throws GitAPIException {
          final File localPath = new File("./TestRepo");
          Git.cloneRepository()
              .setURI(REMOTE_URL)
              .setDirectory(localPath)
              .setCredentialsProvider(new UsernamePasswordCredentialsProvider("***", "***"))
              .call();
      }
      

      【讨论】:

        【解决方案3】:

        我想你会想查看 github 帮助:

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

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

        【讨论】:

          【解决方案4】:

          我遇到了同样的问题。原因是为 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();
          

          它对我有用;)

          【讨论】:

            【解决方案5】:

            我遇到了类似的问题,但我的设置有点不同。将其留在这里以防其他人遇到类似情况。根据本教程,我已经覆盖了我的 configure 方法和 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

            【讨论】:

              【解决方案6】:

              我不清楚您是要进行用户名/密码认证还是公钥/私钥认证。无论哪种方式,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 );
              

              【讨论】:

                【解决方案7】:

                如果存储库是私有的并且需要身份验证,您 (@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());
                        }
                
                    }
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2015-06-12
                  • 2014-02-26
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2013-07-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多