我已经使用 Apache MINA sshd 为 SFTP 设置了 ssh 服务器。我想启用服务器身份验证,这样客户端就不会被欺骗。在文档页面中,它只说使用以下方法( Apache MINA sshd doc ):
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));
但据我了解,它会自行生成 key 对。如果我想为此服务器使用现有的证书文件怎么办?
最佳答案
好的,我找到了。我使用了 MappedKeyPairProvider 类:
sshd.setKeyPairProvider(new MappedKeyPairProvider(loadKeyPair("certificateFile.p12")));
loadKeyPair 定义如下:
public static loadKeyPair(String path) throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException, UnrecoverableKeyException, NoSuchProviderException {
KeyStore p12 = KeyStore.getInstance("pkcs12");
p12.load(new FileInputStream(path), "certPassword".toCharArray());
java.security.cert.Certificate cert = p12.getCertificate("myAlias");
PublicKey publicKey = cert.getPublicKey();
PrivateKey key = (PrivateKey)p12.getKey("myAlias", "certPassword".toCharArray());
return new KeyPair(publicKey, key);
}
请注意,我的证书以 PKCS12 格式存储。
关于java - 如何在 Apache MINA sshd 中设置服务器身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39791859/