使用JGit进行身份验证

JGit是实现Git版本控制系统的轻量级纯Java库。 您可以使用Java语言执行许多操作,例如创建或克隆Git存储库,创建分支,提交,重新设置基础或标记,您可以看到

仓库了解如何使用JGit以及如何编写不同的命令。

但是认证过程并不广泛。 在这篇文章中,我将向您展示如何使用以下方法对Git存储库进行身份验证
吉吉特

首先要做的是将JGit添加为依赖项:

dependency>
  <groupId>org.eclipse.jgit</groupId>
  <artifactId>org.eclipse.jgit</artifactId>
  <version>4.5.0.201609210915-r</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.21</version>
</dependency>

然后让我们看一个没有身份验证的简单克隆:

@Test
public void should_connect_to_public_repo() throws IOException, GitAPIException {
  final String REMOTE_URL = "https://github.com/lordofthejars/wildfly-example.git";

  // prepare a new folder for the cloned repository
  File localPath = File.createTempFile("TestGitRepository", "");
  localPath.delete();

  // then clone
  try (Git result = Git.cloneRepository()
            .setURI(REMOTE_URL)
            .setDirectory(localPath)
            .call()) {
        // Important to close the repo after being used
            System.out.println("Having repository: " + result.getRepository().getDirectory());
  }
}

在这种情况下,未设置身份验证方法。 现在,让我们看看在例如私人仓库的情况下如何添加用户名和密码:

@Test
public void should_connect_using_user_pass() throws IOException, GitAPIException {
  final String REMOTE_URL = "https://asotobu@bitbucket.org/asotobu/backup.git";

  // prepare a new folder for the cloned repository
  File localPath = File.createTempFile("TestGitRepository", "");
  localPath.delete();

  // then clone
  try (Git result = Git.cloneRepository()
            .setURI(REMOTE_URL)
            .setCredentialsProvider(new UsernamePasswordCredentialsProvider("username", "password"))
            .setDirectory(localPath)
            .call()) {
            
          System.out.println("Having repository: " + result.getRepository().getDirectory());
  }
}

在这种情况下,您只需要将UsernameAndPasswordCredentialsProvider设置为凭据提供者,并传递所需的用户名和密码。

我将在这里展示的最后一种情况是如何使用ssh密钥针对git存储库进行身份验证,即使用( 〜/ .ssh / id_rsa )并设置密码短语来对其进行访问。

@Test
public void should_connect_to_public_ssh() throws IOException, GitAPIException {
  final String REMOTE_URL = "git@github.com:lordofthejars/wildfly-example.git";

  SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
    @Override
    protected void configure(OpenSshConfig.Host host, Session session ) {
            session.setUserInfo(new UserInfo() {
              @Override
              public String getPassphrase() {
                return "passphrase";
              }

              @Override
              public String getPassword() {return null;}

              @Override
              public boolean promptPassword(String message) {return false;}

              @Override
              public boolean promptPassphrase(String message) {return true;}

              @Override
              public boolean promptYesNo(String message) {return false;}

              @Override
              public void showMessage(String message) {}
            });
        }
      };

  File localPath = File.createTempFile("TestGitRepository", "");
  localPath.delete();

  try (Git result = Git.cloneRepository()
          .setURI(REMOTE_URL)
          .setTransportConfigCallback(transport -> {
              SshTransport sshTransport = ( SshTransport )transport;
              sshTransport.setSshSessionFactory( sshSessionFactory );
          })
          .setDirectory(localPath)
          .call()) {
        System.out.println("Having repository: " + result.getRepository().getDirectory());
  }

}

在这种情况下,您需要扩展JSchConfigSessionFactory以便能够设置密码短语以访问私钥。 为此,您可以设置一个自定义UserInfo实现,其中getPassphrase方法返回要使用的密码,而hintPassphrase方法应该返回true。

之后,您只需要将传输配置设置为创建的配置即可。

我们不断学习,
亚历克斯

Chan eilinneal-ciùila ghleusar,“Dhùisgeassmuain mochléibhgu aoibh,Marnìduan o bheul nan caileag,Oidhche mhath leibh,beannachd leibh(Oidche Mhath Leibh – Ossian)

音乐: https//www.youtube.com/watch?v = mi4SCOYAdEk

翻译自: https://www.javacodegeeks.com/2016/09/authenticating-with-jgit.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值