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

13

我正在使用这个例子,来自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 服务器设置用户登录和密码。我该怎么做? 谢谢


1
你有什么运气吗?它缺乏文档。请帮助我分享您的经验。 - AZ_
没有运气,仍在等待回复。 - Alvins
我创建了一个答案,也许可以帮到你:http://stackoverflow.com/questions/18694108/apache-mina-sshd-problems-with-authentication-method-when-connecting-to-server/21553897#21553897 - Chris
1个回答

13

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接