12

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

But I need to set user login and pw for SFTP server. How can i do this? Thanks

Community
  • 1
  • 1
Alvins
  • 867
  • 16
  • 27
  • 1
    did you have any luck? It lacks documentation. Please help me share your experience. – AZ_ Sep 24 '13 at 09:20
  • No luck, still waiting for response. – Alvins Oct 14 '13 at 14:52
  • i created an answer, maybe it can help you: http://stackoverflow.com/questions/18694108/apache-mina-sshd-problems-with-authentication-method-when-connecting-to-server/21553897#21553897 – Chris Feb 04 '14 at 15:25

1 Answers1

12

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);
    }
});
Tomek Rękawek
  • 9,204
  • 2
  • 27
  • 43