[英]How to Set Root Directory in Apache Mina Sshd Server in Java
[英]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.