使用 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,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);
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...