• JGit与远程仓库链接使用的两种验证方式(ssh和https)


             JGit是使用JAVAAPI来操控Git仓库的库,由Eclipse公司维护。他提供的API分成两个层次,底层命令和高层命令。底层API是直接作用于低级的仓库对象,高层的API是一个面向普通用户级别功能友好的前端。

      JGit主要通过SSHHTTP(S)的方式与远程仓库进行交互,此外也可以用Git协议(只读)。通过这两种方式,必然是需要添加验证信息的。介绍如下:

    1HTTPS - https://example.com/repo.git

     

    CloneCommand cloneCommand = Git.cloneReposity();

     

    CloneCommand通过setCredentialsProvider()的方法,通过赋值一个UsernamePasswordCredentialsProvider对象,来提供用户名和密码登陆。

     

    (不建议使用HTTP的方式传送,但是也可行)

      (2)SSH with Public Key -

     

       其实通过公钥访问的链接:

     

     

        git@***.***.***/user/***.git

     

       而SSH利用公钥的访问方式的认证信息,通过JSCH库提供。而在JGit中提供了JschConfigSessionFactory的抽象类,代码如下:

     

    SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
    
        @Override
    
        protected void configure( Host host, Session session ) {
    
          /*
      
       解除HostKey检查,也就意味着可以接受未知的远程主机的文件,这是不安全的,这种模式只是用于测试为目的的。
    
        利用ssh-keyscan -t rsa hostname,收集主机数据。
    
        */
    session.setConfig("StrictHostKeyChecking","no");
    
      }
    
    };

     

    以下这是在command中注册认证信息:

     

    cloneCommand.setTransportConfigCallback( new TransportConfigCall back(){
    
    public void  configure(Transporttransport){
    
    SshTransport sshTransport=(SshTransport)transport;
    
    sshTransport.setSshSessionFactory(sshSessionFactory);
    
    }
    
    }

     

    (3)SSH  with Password - ssh://user@example.com/repo.git

     

     

    使用的是上述JschConfigSessionFactory重写的configure方法中,设置password,具体就不详述了。

     

     

  • 相关阅读:
    3.2 Program Encodings 程序编码
    Describe your home
    Building vs solution in command line
    找到适合自己的人生轨迹 Angkor:
    每个月总有那么几天不想学习,不想写代码 Angkor:
    Linux下的Memcache安装
    敏捷开发之 12条敏捷原则
    为什么要用NIO
    memcached server LRU 深入分析
    Linux 脚本编写基础
  • 原文地址:https://www.cnblogs.com/zhengruin/p/JGit.html
Copyright © 2020-2023  润新知