我正在尝试连接到需要私钥身份验证并希望使用 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);
}
}
Run Code Online (Sandbox Code Playgroud)
vud*_*goc -1
我从集成测试中找到了一个示例,希望有所帮助
@BeforeClass
public static void setupClientAndServer() throws Exception {
sshd = CoreTestSupportUtils.setupTestServer(RSAVariantsAuthPublicKeyTest.class);
sshd.setSignatureFactories(RSA_FACTORIES);
sshd.setKeyPairProvider(KEYS_PROVIDER);
sshd.setPasswordAuthenticator(RejectAllPasswordAuthenticator.INSTANCE);
sshd.setHostBasedAuthenticator(RejectAllHostBasedAuthenticator.INSTANCE);
sshd.setPublickeyAuthenticator((username, key, session) -> {
String keyType = KeyUtils.getKeyType(key);
outputDebugMessage("authenticate(%s) keyType=%s session=%s", username, keyType, session);
return KeyPairProvider.SSH_RSA.equals(keyType);
});
sshd.start();
port = sshd.getPort();
client = CoreTestSupportUtils.setupTestClient(RSAVariantsAuthPublicKeyTest.class);
client.setServerKeyVerifier((session, peerAddress, key) -> {
String keyType = KeyUtils.getKeyType(key);
outputDebugMessage("verifyServerKey - keyType=%s session=%s", keyType, session);
return KeyPairProvider.SSH_RSA.equals(keyType);
});
client.start();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1591 次 |
| 最近记录: |