【问题标题】:Apache FTP Server - Create user programmaticallyApache FTP 服务器 - 以编程方式创建用户
【发布时间】:2014-12-10 15:26:07
【问题描述】:

我正在使用嵌入在我的代码中的 Apache FTP 服务器。我使用了 Apache 网站上的示例,并且确实在 5 分钟内嵌入了服务器(如图所示)

Apache Example here

这个例子使用了 users.properties 文件,这很好。

但是,我想在代码中创建我的用户。我不希望用户能够更改属性。

我在网上找到了各种示例,但所有示例似乎都不完整,而且似乎并不完全具备我需要的一切。

简而言之,我想根据以下属性配置在代码中创建一个用户

# Password is "admin"
ftpserver.user.admin.userpassword=21232F297A57A5A743894A0E4A801FC3
ftpserver.user.admin.homedirectory=/home/ftproot
ftpserver.user.admin.enableflag=true
ftpserver.user.admin.writepermission=true
ftpserver.user.admin.maxloginnumber=0
ftpserver.user.admin.maxloginperip=0
ftpserver.user.admin.idletime=0
ftpserver.user.admin.uploadrate=0
ftpserver.user.admin.downloadrate=0

我尝试了一些方法,包括以下内容均无济于事:

    UserFactory uf = new UserFactory();

    uf.setName( "admin" );
    uf.setPassword( "admin" );
    uf.setHomeDirectory( "/home/ftproot" );
    uf.setMaxIdleTime( 0 );
    uf.setEnabled(true);
    uf.createUser();

我遗漏了一些东西,无法在网络上找到任何完整/有效的示例。

编辑

这是我收到的错误消息

C:\WINDOWS>ftp localhost
Connected to localhost.
220 Service ready for new user.
User (localhost:(none)): admin
331 User name okay, need password for admin.
Password:
530 Authentication failed.
Login failed.
ftp>

【问题讨论】:

    标签: java apache ftp


    【解决方案1】:
    FtpServerFactory serverFactory = new FtpServerFactory();
        ListenerFactory factory = new ListenerFactory();
        //factory.setServerAddress("127.0.0.1");
    
        // set the port of the listener
        factory.setPort(3232);
        factory.setIdleTimeout(3000);
    
        // replace the default listener
        serverFactory.addListener("default", factory.createListener());
    
        System.out.println("Adding Users Now");
        PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
        userManagerFactory.setFile(new File("users.properties"));
    
        userManagerFactory.setPasswordEncryptor(new PasswordEncryptor()
        {//We store clear-text passwords in this example
    
                @Override
                public String encrypt(String password) {
                    return password;
                }
    
                @Override
                public boolean matches(String passwordToCheck, String storedPassword) {
                    return passwordToCheck.equals(storedPassword);
                }
            });
    
            BaseUser user1 = new BaseUser();
            user1.setName("test");
            user1.setPassword("test");
            user1.setHomeDirectory("D:/Softwares/ApacheFTP/ftpserver-1.0.6/apache-ftpserver-1.0.6/res/home");
            List<Authority> authorities = new ArrayList<Authority>();
            authorities.add(new WritePermission());
            user1.setAuthorities(authorities);
            UserManager um = userManagerFactory.createUserManager();
            try
            {
                um.save(user1);//Save the user to the user list on the filesystem
            }
            catch (FtpException e1)
            {
                e1.printStackTrace();
            }
    
        serverFactory.setUserManager(um);
    
    
         Map<String, Ftplet> m = new HashMap<String, Ftplet>();
            m.put("miaFtplet", new Ftplet()
            {
    
                @Override
                public void init(FtpletContext ftpletContext) throws FtpException {
                    System.out.println("init");
                    //System.out.println("Thread #" + Thread.currentThread().getId());
                }
    
                @Override
                public void destroy() {
                    System.out.println("destroy");
                    //System.out.println("Thread #" + Thread.currentThread().getId());
                }
    
                @Override
                public FtpletResult beforeCommand(FtpSession session, FtpRequest request) throws FtpException, IOException
                {
                    //System.out.println("beforeCommand " + session.getUserArgument() + " : " + session.toString() + " | " + request.getArgument() + " : " + request.getCommand() + " : " + request.getRequestLine());
                    //System.out.println("Thread #" + Thread.currentThread().getId());
    
                    //do something
                    return FtpletResult.DEFAULT;//...or return accordingly
                }
    
                @Override
                public FtpletResult afterCommand(FtpSession session, FtpRequest request, FtpReply reply) throws FtpException, IOException
                {
                    //System.out.println("afterCommand " + session.getUserArgument() + " : " + session.toString() + " | " + request.getArgument() + " : " + request.getCommand() + " : " + request.getRequestLine() + " | " + reply.getMessage() + " : " + reply.toString());
                    //System.out.println("Thread #" + Thread.currentThread().getId());
    
                    //do something
                    return FtpletResult.DEFAULT;//...or return accordingly
                }
    
                @Override
                public FtpletResult onConnect(FtpSession session) throws FtpException, IOException
                {
                    //System.out.println("onConnect " + session.getUserArgument() + " : " + session.toString());
                    //System.out.println("Thread #" + Thread.currentThread().getId());
    
                    //do something
                    return FtpletResult.DEFAULT;//...or return accordingly
                }
    
                @Override
                public FtpletResult onDisconnect(FtpSession session) throws FtpException, IOException
                {
                    //System.out.println("onDisconnect " + session.getUserArgument() + " : " + session.toString());
                    //System.out.println("Thread #" + Thread.currentThread().getId());
    
                    //do something
                    return FtpletResult.DEFAULT;//...or return accordingly
                }
            });
            serverFactory.setFtplets(m);
    
        // start the server
        FtpServer server = serverFactory.createServer();
    
        System.out.println("Server Starting" + factory.getPort());
        try{
        server.start();
        }
        catch(Exception e2){e2.printStackTrace();}
    

    【讨论】:

    • 谢谢。我会试一试。感谢您抽出宝贵时间写出这样一个透彻的答案。
    • 我的任务是创建一个容器化的 ftp-server 用于测试目的。作为 spring boot http-rest-server 中的嵌入式服务器,以便在运行时添加用户。 - 但新用户似乎需要为其设置写权限。现在服务器工作正常!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多