zoukankan      html  css  js  c++  java
  • 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,具体就不详述了。

     

     

  • 相关阅读:
    几个新角色:数据科学家、数据分析师、数据(算法)工程师
    人类投资经理再也无法击败电脑的时代终将到来了...
    Action Results in Web API 2
    Multiple actions were found that match the request in Web Api
    Routing in ASP.NET Web API
    how to create an asp.net web api project in visual studio 2017
    网站漏洞扫描工具
    How does asp.net web api work?
    asp.net web api history and how does it work?
    What is the difference between a web API and a web service?
  • 原文地址:https://www.cnblogs.com/zhengruin/p/JGit.html
Copyright © 2011-2022 走看看