Java 如何在Apache MINA sshd中设置服务器身份验证?
我已经使用ApacheMina sshd为SFTP设置了一个ssh服务器。我想启用服务器身份验证,这样客户端就不会被欺骗。在文档页面中,它只说明使用以下方法():Java 如何在Apache MINA sshd中设置服务器身份验证?,java,sshd,mina,Java,Sshd,Mina,我已经使用ApacheMina sshd为SFTP设置了一个ssh服务器。我想启用服务器身份验证,这样客户端就不会被欺骗。在文档页面中,它只说明使用以下方法(): 但据我所知,这会自动生成密钥对。如果我想使用此服务器的现有证书文件,该怎么办?好的,我找到了。我使用了MappedKeyPairProvider类: sshd.setKeyPairProvider(new MappedKeyPairProvider(loadKeyPair("certificateFile.p12"))); load
但据我所知,这会自动生成密钥对。如果我想使用此服务器的现有证书文件,该怎么办?好的,我找到了。我使用了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格式存储的。好的,我找到了。我使用了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));
FileKeyPairProvider更简单
Path path = Paths.get(getClass().getClassLoader().getResource("server-key.pem").toURI());
sshd.setKeyPairProvider(new FileKeyPairProvider(path));
如果运行bin\sshd.bat,您找到配置身份验证的方法了吗?没有,抱歉。我都是按程序做的。谢谢!对于任何无意中访问的人:集成身份验证固定为“Objects.equals(username,password)”,因此您可以以“root”、“root”等身份登录。如果运行bin\sshd.bat,您是否找到配置身份验证的方法?不,抱歉。我都是按程序做的。谢谢!对于任何一个无意中访问的人来说:集成身份验证被固定为“Objects.equals(username,password)”,因此你可以以“root”、“root”等身份登录。如果你打包了你的应用程序,这仍然有效吗?我觉得
FileKeyPayProvider
需要一个物理文件,而这只在开发过程中起作用,因为对资源调用.toURI
仍然能够生成一个文件://
URI.FoundClassLoadableResourceKeyPairProvider
。如果打包应用程序,这仍然有效吗?我觉得FileKeyPayProvider
需要一个物理文件,这只在开发过程中起作用,因为对资源调用.toURI
仍然能够生成一个文件://
URI.FoundClassLoadableResourceKeyPairProvider
。