java - 使用 Mina SSHD(客户端)使用私钥连接到 SFTP
问题描述
我正在尝试连接到需要私钥身份验证并想要使用 Mina 的 SFTP 服务器。查看文档,我可以看到如何使用密码身份验证而不是私钥身份验证来执行该身份验证。我没有看到任何示例代码可以演示如何使用 mina 执行私钥身份验证。
目前是否可以使用此库,如果可以,您能否提供有关如何加载密钥和执行连接的示例代码?
这是我想使用 SSHTools 做的一个示例,以供参考。
private static void authenticate(Ssh2Client ssh2, String host, Integer port, String username, InputStream privateKey) {
Ssh2PublicKeyAuthentication auth = createKeyAuthentication(privateKey);
try {
int result = ssh2.authenticate(auth);
if (result != SshAuthentication.COMPLETE) {
throw new AuthenticationIncomplete(host, port, username, result);
}
} catch (SshException ex) {
throw new UnableToAuthenticate(host, port, username, ex);
}
}
private static Ssh2PublicKeyAuthentication createKeyAuthentication(InputStream privateKey) {
try {
SshPrivateKeyFile privateKeyFile = SshPrivateKeyFileFactory.parse(StreamUtil.readIntoByteArray(privateKey));
SshKeyPair keyPair = privateKeyFile.toKeyPair("");
Ssh2PublicKeyAuthentication auth = new Ssh2PublicKeyAuthentication();
auth.setPrivateKey(keyPair.getPrivateKey());
auth.setPublicKey(keyPair.getPublicKey());
return auth;
} catch (IOException | InvalidPassphraseException ex) {
throw new ConfigurationIssue(ex);
}
}
解决方案
推荐阅读
- django - Django RF - 带有 ModelViewSet 的嵌套序列化程序
- ruby - 正在使用的 ruby 版本是否确定使用哪个 openssl 版本,以及是否可以使用 TLS 版本?
- php - PHP $_GET 参数给出问题
- regex - Matlab:查找字符串模式并将其替换为随机字符串
- postgresql - 42P07:关系“AspNetRoles”已经存在
- python - django中的重定向和传递参数
- vpn - TLS 密钥协商未能在 60 秒内与 tunnelblick 发生
- maxima - (wx)Maxima:确定表达式的部分数
- javascript - 为什么 Object.create( proto ) 在这个例子中似乎忽略了原型参数?
- reactjs - 如何将 React 项目上传到 Github,以便更多人可以使用它?