如何在Apache MINA sshd中设置服务器认证?

问题描述:

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

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格式。

+0

如果运行bin \ sshd.bat,您是否找到如此配置身份验证的方法? –

+0

不,对不起。我以编程方式完成了这一切。 –

+0

谢谢!对于任何偶然发现的人:集成身份验证被固定为“Objects.equals(用户名,密码)”,因此您可以登录例如作为“根”,“根”。 –