2016-09-30 151 views
0

我已經爲SFTP使用Apache MINA sshd設置了一個ssh服務器。我想啓用服務器身份驗證,因此客戶端不能被欺騙。在文檔頁面的所有它說的是使用下面的方法(Apache MINA sshd doc):如何在Apache MINA sshd中設置服務器認證?

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

但據我瞭解,產生自身的密鑰對。如果我想爲該服務器使用現有的證書文件,該怎麼辦?

回答

0

好的,我發現它。我用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(用戶名,密碼)」,因此您可以登錄例如作爲「根」,「根」。 –

相關問題