Apache FTP Server - 以编程方式创建用户

时间:2014-12-10 15:26:07

标签: java apache ftp

我正在使用嵌入在我的代码中的Apache FTP Server。我使用了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>

1 个答案:

答案 0 :(得分: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();}