git - 如何使用带有 ssh 的 java api 推送到 gitlab

标签 git jgit java

我正在使用版本 GitLab Community Edition 9.1.1 d3123f6 的 gitlab。
我可以使用 http 协议(protocol)将我的代码推送到 gitlab,但是当我尝试使用 git 协议(protocol)时,它显示以下错误。

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添加 in the comments :

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

关于git - 如何使用带有 ssh 的 java api 推送到 gitlab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44346552/

相关文章:

java - 在代码中删除XML文件中的内容

java - 在 IntelliJ 中运行 JBullet 演示

Git pull 或 git fetch 会忽略一些文件

git - 在某个提交后删除分支中的所有提交

java - 如何使用 JGit API 从父提交中获取树?

java - socketio.emit 不起作用 netty socketio

ruby-on-rails - Git 工作流两个开发人员远程,最佳实践?

git - 执行 git apply 时出现错误 "git diff header lacks filename information when removing 1 leading pathname component "

java - 如何使用 JGit 获取存储库中每个文件的更改次数?

git - 使用私有(private) BitBucket 存储库进行身份验证以使用 JGit 进行克隆