SFTP服务器在Apache Mina SSHD中设置用户/密码
我正在使用这个例子,取自Java SFTP Server Library? :
public void setupSftpServer(){ SshServer sshd = SshServer.setUpDefaultServer(); sshd.setPort(22); sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser")); List<NamedFactory> userAuthFactories = new ArrayList<NamedFactory>(); userAuthFactories.add(new UserAuthNone.Factory()); sshd.setUserAuthFactories(userAuthFactories); sshd.setCommandFactory(new ScpCommandFactory()); List<NamedFactory> namedFactoryList = new ArrayList<NamedFactory>(); namedFactoryList.add(new SftpSubsystem.Factory()); sshd.setSubsystemFactories(namedFactoryList); try { sshd.start(); } catch (Exception e) { e.printStackTrace(); } }
但是我需要为SFTP服务器设置用户登录和pw。 我怎样才能做到这一点? 谢谢
将new UserAuthNone.Factory()
更改为new UserAuthPassword.Factory()
,然后实现并注册PasswordAuthenticator
对象。 对于有效的用户名和密码参数,其authenticate
方法应返回true
。
List> userAuthFactories = new ArrayList>(); 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); } });