我正在使用以下代码在Java应用程序中使用Git.我有一个有效的密钥(一直使用它),这个特定的代码以前使用相同的密钥和git存储库,但现在我得到以下异常:无效的私钥:[B @ 59c40796.
jSch.addIdentity("<key_path>/private_key.pem");
Run Code Online (Sandbox Code Playgroud)
在线搜索后,我将createDefaultJSch更改为使用pemWriter:
String remoteURL = "ssh://git@<git_repository>";
TransportConfigCallback transportConfigCallback = new SshTransportConfigCallback();
File gitFolder = new File(workingDirectory);
if (gitFolder.exists()) FileUtils.delete(gitFolder, FileUtils.RECURSIVE);
Git git = Git.cloneRepository()
.setURI(remoteURL)
.setTransportConfigCallback(transportConfigCallback)
.setDirectory(new File(workingDirectory))
.call();
}
private static class SshTransportConfigCallback implements TransportConfigCallback {
private final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
@Override
protected void configure(OpenSshConfig.Host hc, Session session) {
session.setConfig("StrictHostKeyChecking", "no");
}
@Override
protected JSch createDefaultJSch(FS fs) throws JSchException {
JSch jSch = super.createDefaultJSch(fs);
jSch.addIdentity("<key_path>/private_key.pem");
return jSch;
}
};
Run Code Online (Sandbox Code Playgroud)
但仍然得到无效的privateKey异常.