【问题标题】:JGit - ssh connectivityJGit - ssh 连接
【发布时间】:2016-02-24 19:39:07
【问题描述】:

我正在使用 JGit 连接到远程 Git 存储库。我尝试在下面执行相同的操作。

public class Main{
    public static void main(String args[]) throws Exception{
        JschConfigSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
              @Override
              protected void configure( Host host, Session session ) {
                session.setPassword( "XXXX" );
              }
            };

            String REMOTE_URL="ssh://git@url/test.git";
            CloneCommand cloneCommand = Git.cloneRepository();
            cloneCommand.setURI( "ssh://git@url/test.git" );
            cloneCommand.setTransportConfigCallback( new TransportConfigCallback() {
              @Override
              public void configure( Transport transport ) {
                SshTransport sshTransport = ( SshTransport )transport;
                sshTransport.setSshSessionFactory( sshSessionFactory );
              }
            } );

            System.out.println("Listing remote repository " + REMOTE_URL);
            Collection<Ref> refs = Git.lsRemoteRepository()
                    .setHeads(true)
                    .setTags(true)
                    .setRemote(REMOTE_URL)
                    .call();

            for (Ref ref : refs) {
                System.out.println("Ref: " + ref);
            }
    }
}

错误:

Listing remote repository ssh://git@url/test.git
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.eclipse.jgit.api.errors.TransportException: ssh://git@url/test.git: UnknownHostKey: url. RSA key fingerprint is XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX
    at org.eclipse.jgit.api.LsRemoteCommand.execute(LsRemoteCommand.java:223)
    at org.eclipse.jgit.api.LsRemoteCommand.call(LsRemoteCommand.java:159)
    at Jgit.Jgit.Main.main(Main.java:94)
Caused by: org.eclipse.jgit.errors.TransportException: ssh://git@url/test.git: UnknownHostKey: url. RSA key fingerprint is XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX
    at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:159)
    at org.eclipse.jgit.transport.SshTransport.getSession(SshTransport.java:136)
    at org.eclipse.jgit.transport.TransportGitSsh$SshFetchConnection.<init>(TransportGitSsh.java:262)
    at org.eclipse.jgit.transport.TransportGitSsh.openFetch(TransportGitSsh.java:161)
    at org.eclipse.jgit.api.LsRemoteCommand.execute(LsRemoteCommand.java:202)
    ... 2 more
Caused by: com.jcraft.jsch.JSchException: UnknownHostKey: ssh://git@url/test.git: UnknownHostKey: url. RSA key fingerprint is XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX
    at com.jcraft.jsch.Session.checkHost(Session.java:797)
    at com.jcraft.jsch.Session.connect(Session.java:342)
    at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:116)
    ... 6 more

我浏览了互联网;发现需要添加以下内容:

com.jcraft.jsch.JSch.setConfig ( "StrictHostKeyChecking", "no" );

但是,在上面的代码 sn-p 中放置它的位置。是否有可用的示例工作代码 sn-p。

【问题讨论】:

  • 您是否尝试在JschConfigSessionFactory 的实现中更改主机检查设置,即session.setConfig( "StrictHostKeyChecking", "no" );
  • 嗨 Rudiger,我已经把session.setconfig ( "StrictHostKeyChecking", "no" ); ssh 密钥错误消失了。我收到“身份验证错误”
  • 嗨 Rudiger,请在下面找到代码/研究
  • 您当然需要为远程存储库提供合适的凭据。请参阅此处了解更多信息:codeaffine.com/2014/12/09/jgit-authentication

标签: ssh jgit


【解决方案1】:

您可以使用 Session 对象调用 setConfig("","") 方法,如下所示:-

JSch jsch = new JSch();
Session session = jsch.getSession(username, hostname, 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 2017-06-01
    相关资源
    最近更新 更多