SFTP服务器在Apache Mina SSHD中设置用户/密码

时间:2013-07-29 13:26:39

标签: java sftp apache-mina

我正在使用此示例,取自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。我怎样才能做到这一点? 感谢

1 个答案:

答案 0 :(得分:10)

new UserAuthNone.Factory()更改为new UserAuthPassword.Factory(),然后实施并注册PasswordAuthenticator个对象。其authenticate方法应返回true以获取有效的用户名和密码参数。

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);
    }
});