SFTP服务器在Apache Mina SSHD中设置用户/密码 [英] SFTP server set user/password in Apache Mina SSHD
本文介绍了SFTP服务器在Apache Mina SSHD中设置用户/密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用此示例,取自 Java SFTP服务器库?:
I'm using this example, taken from Java SFTP Server Library?:
public void setupSftpServer(){
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(22);
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));
List<NamedFactory<UserAuth>> userAuthFactories = new ArrayList<NamedFactory<UserAuth>>();
userAuthFactories.add(new UserAuthNone.Factory());
sshd.setUserAuthFactories(userAuthFactories);
sshd.setCommandFactory(new ScpCommandFactory());
List<NamedFactory<Command>> namedFactoryList = new ArrayList<NamedFactory<Command>>();
namedFactoryList.add(new SftpSubsystem.Factory());
sshd.setSubsystemFactories(namedFactoryList);
try {
sshd.start();
} catch (Exception e) {
e.printStackTrace();
}
}
但我需要为SFTP设置用户登录和pw服务器。我怎样才能做到这一点?
谢谢
But I need to set user login and pw for SFTP server. How can i do this? Thanks
推荐答案
更改新UserAuthNone.Factory()
到新的UserAuthPassword.Factory()
然后实现并注册 PasswordAuthenticator
对象。其身份验证
方法应返回 true
以获取有效的用户名和密码参数。
Change new UserAuthNone.Factory()
to new UserAuthPassword.Factory()
and then implement and register PasswordAuthenticator
object. Its authenticate
method should return true
for valid username and password parameters.
List<NamedFactory<UserAuth>> userAuthFactories = new ArrayList<NamedFactory<UserAuth>>();
userAuthFactories.add(new UserAuthPassword.Factory());
sshd.setUserAuthFactories(userAuthFactories);
sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
public boolean authenticate(String username, String password, ServerSession session) {
return "tomek".equals(username) && "123".equals(password);
}
});
这篇关于SFTP服务器在Apache Mina SSHD中设置用户/密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文