java - Apache FTP 服务器 - 以编程方式创建用户

标签 java apache ftp

我正在使用代码中嵌入的 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>

最佳答案

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();}

关于java - Apache FTP 服务器 - 以编程方式创建用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27404709/

相关文章:

Java JPA 和 Java JDBC 对于同一 MySQL 查询返回不同的结果

java - 什么时候安装 Guice 模块?

.net - ASP.NET 的自定义文件扩展名 - 需要帮助!

java - 如何使用apache配置库获取属性值和标签值

linux - 清除大型 Apache 域日志

java - FTP 至 1and1.com

linux - vsftpd 文件夹上不显示任何内容

java - 防止 Android One 设备中的屏幕捕获

java - 如何在 apache commons csv 中读取包含 header 的 csv?

php - 从文件夹加载所有 .php 文件