如何使用 ssh 使用 java api 推送到 gitlab

How to push to gitlab with java api with ssh

我正在使用 git版本 GitLab Community Edition 9.1.1 d3123f6.

lab

我可以使用 http 协议将我的代码推送到 gitlab,但是当我尝试使用 git 协议时,它显示以下错误。

Exception in thread "main" 
org.eclipse.jgit.errors.UnsupportedCredentialItem: 
ssh://git@192......: 
org.eclipse.jgit.transport.CredentialItem$YesNoType:The authenticity 
of host '192......' can't be established.

RSA key fingerprint is 
99:...............
Are you sure you want to continue connecting?
at org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider.get(UsernamePasswordCredentialsProvider.java:119)
at org.eclipse.jgit.transport.CredentialsProviderUserInfo.promptYesNo(CredentialsProviderUserInfo.java:124)
at com.jcraft.jsch.Session.checkHost(Session.java:785)
at com.jcraft.jsch.Session.connect(Session.java:342)
at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:117)
at org.eclipse.jgit.transport.SshTransport.getSession(SshTransport.java:137)
at org.eclipse.jgit.transport.TransportGitSsh$SshFetchConnection.<init>(TransportGitSsh.java:264)
at org.eclipse.jgit.transport.TransportGitSsh.openFetch(TransportGitSsh.java:162)
at org.eclipse.jgit.transport.FetchProcess.executeImp(FetchProcess.java:136)
at org.eclipse.jgit.transport.FetchProcess.execute(FetchProcess.java:122)
at org.eclipse.jgit.transport.Transport.fetch(Transport.java:1201)
at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:128)
at org.eclipse.jgit.api.CloneCommand.fetch(CloneCommand.java:203)
at org.eclipse.jgit.api.CloneCommand.call(CloneCommand.java:136)
at org.dstadler.jgit.unfinished.PushToRemoteRepository.main(PushToRemoteRepository.java:100)

我的密码是

private static final String REMOTE_URL = "git@192.....:ash/test.git";
File localPath = new File("/home/user/Git", "");
localPath.getParentFile().mkdirs();
localPath.createNewFile();
if(!localPath.delete()) {
    throw new IOException("Could not delete temporary file " + localPath);
}

Git git = Git.cloneRepository()
        .setURI( REMOTE_URL )
        .setDirectory(localPath)
        .setCredentialsProvider( cp )
        .call();
System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);


System.out.println("please edit the file and then press \"Enter\" here to push ");
System.in.read();


File file = new File( git.getRepository().getWorkTree(), "file" + new Object().hashCode() );

git.add().addFilepattern( file.getName() ).call();

git.commit().setAll(true).setMessage("a message").call();

git.push() .setCredentialsProvider( cp ) .call();

System.out.println("Pushed from repository: " + localPath + " to remote repository at " + REMOTE_URL);

首先尝试从命令行建立连接:

ssh git@...

那应该问你同样的问题:回答 'y'。

完成后,意味着一旦 ~/.ssh/known_hosts 已通过远程服务器的键扫描更新,您可以再次尝试相同的 Java JGit 程序。

我以前发过(5 年前)a solution, but for EGit, not JGit

查看更多信息“JGit Authentication Explained

JGit provides an abstract JSchConfigSessionFactory that uses JSch to establish SSH connections and requires its configure() to be overridden

OP Ash adds :

The problem was in .ssh/config file where needed lines are commented including private key identity.