SFTP 服务器多用户认证

SFTP server multiple user authentication

我正在尝试扩展用户身份验证示例,该示例也已提供 here, so that multiple users can login to the server. I would also like to assign a different home directory for each user. So far, I haven't been able to find any such utility provided by the Apache SSHD API, so I have tried the following workaround, by using the utilities provided by the Apache FtpServer

我尝试做的是:

  1. 创建一个 UserManager 对象来管理用户列表并将他们的信息存储在 属性 文件中,其方式类似于 this example
  2. 创建一个在其 authenticate 方法中使用 UserManager 的 PasswordAuthenticator,如下所示:

    public class MyPasswordAuthenticator implements PasswordAuthenticator {
    
        private UserManager userManager;
    
        public MyPasswordAuthenticator(){
            this.userManager=null;
        }
    
        public MyPasswordAuthenticator(UserManager manager) {
            this.userManager=manager;
        }
    
        @Override
        public boolean authenticate(String username, String password, ServerSession session) throws PasswordChangeRequiredException {
            if (this.userManager==null) return false;
            User usr=null;
            try {
                usr = userManager.getUserByName(username);
            } catch (FtpException e) {
                e.printStackTrace();
            }
            if (usr==null) return false;
            else{       
                String pass=usr.getPassword();
                return password.equals(pass);
            }
        }
    
    
    }
    

但是,usr.getPassword() return 为空,即使 a) 属性 文件中的密码字段确实有值 b) 我检查了函数 getName()getHomeDirectory() 以及它们 return 各自的字符串值。

我的问题是,为什么会发生这种情况,应该如何解决?

我找到了一种方法让它工作,它是:

usr = this.userManager.authenticate(new UsernamePasswordAuthentication(username, password));