我想将远程私有(private) Atlassian BitBucket 存储库克隆到我的计算机。
能否请您描述一下如何完成的过程。
我对身份验证过程特别感兴趣。我是否需要使用(对于 Atlassian BitBucket)UsernamePasswordCredentialsProvider
作为 CredentialsProvider
,仅此而已,还是应该执行其他一些步骤才能做到这一点?
已更新
我尝试了以下解决方案:
SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
@Override
protected void configure(Host host, Session session) {
session.setPassword( "password" );
session.setConfig("StrictHostKeyChecking", "no");
}
};
CloneCommand cloneCommand = Git.cloneRepository();
cloneCommand.setURI("ssh://user@bitbucket.org/reponame.git");
//cloneCommand.setURI("git@bitbucket.org:user/reponame.git");
cloneCommand.setDirectory(new File("reponame"));
cloneCommand.setTransportConfigCallback(new TransportConfigCallback() {
@Override
public void configure(Transport transport) {
SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(sshSessionFactory);
}
});
cloneCommand.call();
但它失败了,出现以下异常:
Exception in thread "main" org.eclipse.jgit.api.errors.TransportException: git@bitbucket.org:user/reponame.git: Auth fail
at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:248)
at org.eclipse.jgit.api.CloneCommand.fetch(CloneCommand.java:306)
at org.eclipse.jgit.api.CloneCommand.call(CloneCommand.java:200)
at com.bitbucket.BitBucketTest.cloneViaSsh(BitBucketTest.java:63)
at com.bitbucket.BitBucketTest.main(BitBucketTest.java:39)
Caused by: org.eclipse.jgit.errors.TransportException: git@bitbucket.org:user/reponame.git: Auth fail
at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:172)
at org.eclipse.jgit.transport.SshTransport.getSession(SshTransport.java:140)
at org.eclipse.jgit.transport.TransportGitSsh$SshFetchConnection.<init>(TransportGitSsh.java:280)
at org.eclipse.jgit.transport.TransportGitSsh.openFetch(TransportGitSsh.java:170)
at org.eclipse.jgit.transport.FetchProcess.executeImp(FetchProcess.java:137)
at org.eclipse.jgit.transport.FetchProcess.execute(FetchProcess.java:123)
at org.eclipse.jgit.transport.Transport.fetch(Transport.java:1269)
at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:237)
... 4 more
Caused by: com.jcraft.jsch.JSchException: Auth fail
at com.jcraft.jsch.Session.connect(Session.java:519)
at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:126)
... 11 more
最佳答案
根据 this page , Bitbucket 允许通过 SSH 进行身份验证。在这种情况下,您不需要 CredentialsProvider
.
但是,您可能需要提供 SshSessionFactory
.
SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
@Override
protected void configure( Host host, Session session ) {
// ...
}
};
CloneCommand cloneCommand = Git.cloneRepository();
cloneCommand.setURI( "ssh://user@example.com/repo.git" );
cloneCommand.setTransportConfigCallback( new TransportConfigCallback() {
@Override
public void configure( Transport transport ) {
SshTransport sshTransport = ( SshTransport )transport;
sshTransport.setSshSessionFactory( sshSessionFactory );
}
} );
请注意,在 TransportConfigCallback::configure
中, transport 参数被盲目转换为 SshTransport
, 意思是这个 TransportConfigCallback
仅适用于 SSH URL。
如果您的 Bitbucket 服务器需要密码保护的 SSH 连接,您可以在 SshSessionFactory
中提供密码的 configure
方法,例如
SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
@Override
protected void configure( Host host, Session session ) {
session.setPassword( "password" );
}
} );
私钥是从<user-home>/.ssh
加载的.如果您的私钥文件名称不同或位于其他地方,我建议覆盖 createDefaultJSch()
.调用基本方法后,可以像这样添加自定义私钥:
@Override
protected JSch createDefaultJSch( FS fs ) throws JSchException {
JSch defaultJSch = super.createDefaultJSch( fs );
defaultJSch.addIdentity( "/path/to/private_key" );
return defaultJSch;
}
有关更多详细信息,您可能需要阅读 JGit Authentication Explained ,我几年前写的一篇文章。
关于git - 使用私有(private) BitBucket 存储库进行身份验证以使用 JGit 进行克隆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48125848/