Java JGit:传输异常-拒绝主机密钥:bitbucket.org
我正在使用JGit创建和克隆一个存储库(远程存储库是一个bitbucket repo,是的,我添加了部署密钥)。 实质上,我:Java JGit:传输异常-拒绝主机密钥:bitbucket.org,java,eclipse,git,bitbucket,jgit,Java,Eclipse,Git,Bitbucket,Jgit,我正在使用JGit创建和克隆一个存储库(远程存储库是一个bitbucket repo,是的,我添加了部署密钥)。 实质上,我: 创建存储库 禁用JSch严格主机密钥检查 设置JSch凭据(我的ssh密钥受密码保护,因此如果我不指定密码,JSch将失败) 克隆存储库 我的代码如下: // Create repository File gitDir = new File(localPath); FileRepository repo = new FileRepos
// Create repository
File gitDir = new File(localPath);
FileRepository repo = new FileRepository(gitDir);
repo.create();
// Add remote origin
SshSessionFactory.setInstance(new JschConfigSessionFactory() {
public void configure(Host hc, Session session) {
session.setConfig("StrictHostKeyChecking", "no");
}
});
JschConfigSessionFactory sessionFactory = new JschConfigSessionFactory() {
@Override
protected void configure(OpenSshConfig.Host hc, Session session) {
CredentialsProvider provider = new CredentialsProvider() {
@Override
public boolean isInteractive() {
return false;
}
@Override
public boolean supports(CredentialItem... items) {
return true;
}
@Override
public boolean get(URIish uri, CredentialItem... items) throws UnsupportedCredentialItem {
for (CredentialItem item : items) {
if (item instanceof CredentialItem.StringType) {
((CredentialItem.StringType) item).setValue("myPassword");
}
}
return true;
}
};
UserInfo userInfo = new CredentialsProviderUserInfo(session, provider);
session.setUserInfo(userInfo);
}
};
SshSessionFactory.setInstance(sessionFactory);
git = org.eclipse.jgit.api.Git.cloneRepository()
.setURI(remote)
.setDirectory(new File(localPath + "/git"))
.call();
问题:克隆失败,出现以下错误
org.eclipse.jgit.api.errors.TransportException:git@bitbucket.org:username/blah.git:拒绝主机密钥:bitbucket.org
位于org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:137)
位于org.eclipse.jgit.api.CloneCommand.fetch(CloneCommand.java:178)
位于org.eclipse.jgit.api.CloneCommand.call(CloneCommand.java:125)
我也在寻找这个问题的答案,但参考文献很少。我想贡献出最终对我有用的东西。我试图使用jGit通过ssh命令控制台查询Gerrit。为此,需要提供密码短语和ssh私钥 要设置连接,首先必须配置JSch:
SshSessionFactory factory = new JschConfigSessionFactory() {
public void configure(Host hc, Session session) {
session.setConfig("StrictHostKeyChecking", "no");
}
@Override
protected JSch
getJSch(final OpenSshConfig.Host hc, FS fs) throws JSchException {
JSch jsch = super.getJSch(hc, fs);
jsch.removeAllIdentity();
//Where getSshKey returns content of the private key file
if (StringUtils.isNotEmpty(data.getSshKey())) {
jsch.addIdentity("identityName", data.getSshKey()
.getBytes(), null, data.getSshPassphrase()
.getBytes());
}
return jsch;
}
};
现在,我无法使用传统方法使用私钥会话。git.cloneRepository()将不起作用。您必须设置传输并将会话工厂分配给它:
String targetRevision = "refs/head/master"; //or "refs/meta/config", "refs/for/master"
Transport transport = null;
transport = Transport.open(git.getRepository(), url);
((SshTransport) transport).setSshSessionFactory(factory);
RefSpec refSpec = new RefSpec().setForceUpdate(true).setSourceDestination(
targetRevision, targetRevision);
transport.fetch(monitor, Arrays.asList(refSpec));
CheckoutCommand co = git.checkout();
co.setName(targetRevision);
co.call();
//Add and make a change:
git.add().addFilepattern("somefile.txt").call();
RevCommit revCommit = git.commit().setMessage("Change.").call();
//Last, push the update:
RemoteRefUpdate rru =new RemoteRefUpdate(git.getRepository(), revCommit.name(),
targetRevision, true, null, null);
List<RemoteRefUpdate> list = new ArrayList<RemoteRefUpdate>();
list.add(rru);
PushResult r = transport.push(monitor, list);
String targetRevision=“refs/head/master”//或“refs/meta/config”、“refs/for/master”
传输=空;
transport=transport.open(git.getRepository(),url);
((SshTransport)运输)。设置SshSessionFactory(工厂);
RefSpec RefSpec=new RefSpec().setForceUpdate(true).setSourceDestination(
targetRevision,targetRevision);
transport.fetch(监视器、数组.asList(refSpec));
CheckoutCommand co=git.checkout();
co.setName(targetRevision);
co.call();
//添加并进行更改:
git.add().addFilepattern(“somefile.txt”).call();
RevCommit RevCommit=git.commit().setMessage(“更改”).call();
//最后,推送更新:
RemoteRefUpdate rru=新的RemoteRefUpdate(git.getRepository(),revCommit.name(),
targetRevision,true,null,null);
列表=新的ArrayList();
列表。添加(rru);
PushResult r=传输。push(监视器、列表);
这是一个简短的小圆圈教程,介绍如何通过ssh连接到远程存储库、获取/签出、进行更改以及向上游推送。我希望这能节省其他人更好地理解jGit的时间。这类似于吗?谢谢-但似乎不起作用。我更改了部署密钥的标签,但问题仍然存在。