使用mina sshd(客户端)使用私钥连接到sftp

myss37ts  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(349)

我正在尝试连接到需要私钥身份验证的sftp服务器,并希望使用mina。通过阅读文档,我可以看到如何使用密码身份验证而不是私钥身份验证来执行身份验证。我没有看到任何示例代码演示如何使用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);
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题