繁体   English   中英

如何在Apache MINA sshd中设置服务器身份验证?

[英]How to set up server authentication in Apache MINA sshd?

我已经使用Apache MINA sshd为SFTP设置了一个ssh服务器。 我想启用服务器身份验证,以便客户端不会被欺骗。 在文档页面中,它所说的是使用以下方法( Apache MINA sshd doc ):

sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));

但据我所知,这会产生一个密钥对。 如果我想为此服务器使用现有证书文件,该怎么办?

好的我找到了。 我使用了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格式存储。

FileKeyPairProvider更简单

Path path = Paths.get(getClass().getClassLoader().getResource("server-key.pem").toURI());
sshd.setKeyPairProvider(new FileKeyPairProvider(path));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM