Apache MINA SFTP Example

前端 未结 2 1578
情话喂你
情话喂你 2020-12-18 08:20

I\'m trying to set up an SFTP server with multiple users that each have their own home directory.

I read this answer which explained how to set a virtual directory f

2条回答
  •  南方客
    南方客 (楼主)
    2020-12-18 08:42

    Updated version of above (as of 1.4.0 of sshd-core). Note that I did not specify a file for the host key provider since mine was for a Junit integration test.

    List> userAuthFactories = new ArrayList>();
    userAuthFactories.add(new UserAuthPasswordFactory());
    
    List> sftpCommandFactory = new ArrayList>();
    sftpCommandFactory.add(new SftpSubsystemFactory());
    
    SshServer sshd = SshServer.setUpDefaultServer();
    sshd.setPort(22);
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
    sshd.setUserAuthFactories(userAuthFactories);
    sshd.setCommandFactory(new ScpCommandFactory());
    sshd.setSubsystemFactories(sftpCommandFactory);
    sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
      @Override
      public boolean authenticate(String username, String password, ServerSession session) {
        if ((username.equals("admin")) && (password.equals("admin"))) {
          sshd.setFileSystemFactory(new VirtualFileSystemFactory(new File("C:\\devl").toPath()));
          return true;
        }
        return false;
      }
    });
    
    sshd.start();
    

提交回复
热议问题