Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SFTP服务器在Apache Mina SSHD中设置用户/密码_Java_Sftp_Apache Mina - Fatal编程技术网

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

Java SFTP服务器在Apache Mina SSHD中设置用户/密码,java,sftp,apache-mina,Java,Sftp,Apache Mina,我使用的示例取自: public void setupSftpServer(){ SshServer sshd=SshServer.setUpDefaultServer(); 设置端口(22); setKeyPairProvider(新的SimpleGeneratorHostKeyProvider(“hostkey.ser”)); List UserAuthFactorys=new ArrayList(); 添加(新的UserAuthNone.Factory()); sshd.setUserAu

我使用的示例取自:

public void setupSftpServer(){
SshServer sshd=SshServer.setUpDefaultServer();
设置端口(22);
setKeyPairProvider(新的SimpleGeneratorHostKeyProvider(“hostkey.ser”));
List UserAuthFactorys=new ArrayList();
添加(新的UserAuthNone.Factory());
sshd.setUserAuthFactorys(UserAuthFactorys);
setCommandFactory(新的ScpCommandFactory());
List namedFactoryList=new ArrayList();
namedFactoryList.add(新的sftpusbsystem.Factory());
sshd.SetSubsystemFactorys(命名工厂列表);
试一试{
sshd.start();
}捕获(例外e){
e、 printStackTrace();
}
}
但我需要为SFTP服务器设置用户登录和pw。我该怎么做?
感谢将
新UserAuthNone.Factory()
更改为
新UserAuthPassword.Factory()
,然后实现并注册
密码验证器
对象。对于有效的用户名和密码参数,其
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);
    }
});
List userauthfactures=new ArrayList();
添加(新的UserAuthPassword.Factory());
sshd.setUserAuthFactorys(UserAuthFactorys);
setPasswordAuthenticator(新的PasswordAuthenticator(){
公共布尔身份验证(字符串用户名、字符串密码、服务器会话){
返回“tomek.equals”(用户名)和“123.equals”(密码);
}
});

你运气好吗?它缺少文档。请帮助我分享您的经验。运气不好,仍在等待回复。我创建了一个答案,也许它可以帮助您:
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);
    }
});