Apache Ftp Server数据库管理用户学习总结

以下为手动加载数据库的操作,可以参考https://blog.csdn.net/clbhii/article/details/9928329

FtpServerFactory serverFactory = new FtpServerFactory();

DbUserManagerFactory dbUserManagerFactory = new DbUserManagerFactory();
dbUserManagerFactory.setSqlUserAdmin("select userid from ftp_user where userid='admin'");
dbUserManagerFactory.setSqlUserAuthenticate("select userpassword from ftp_user where userid='{userid}'");
dbUserManagerFactory.setSqlUserDelete("delete from ftp_user where userid='{userid}'");
dbUserManagerFactory.setSqlUserInsert("INSERT INTO ftp_user('userid', 'userpassword', 'enableflag', 'homedirectory', 'writepermission', 'idletime', 'uploadrate', 'downloadrate') VALUES ('{userid}', '{userpassword}',{enableflag},'{homedirectory}', {writepermission},{idletime},{uploadrate},{downloadrate})");
dbUserManagerFactory.setSqlUserUpdate("UPDATE ftp_user SET userpassword='{userpassword}',homedirectory='{homedirectory}',enableflag={enableflag},writepermission={writepermission},idletime={idletime},uploadrate={uploadrate},downloadrate={downloadrate} WHERE userid='{userid}'");
dbUserManagerFactory.setSqlUserSelect("SELECT userid, userpassword, homedirectory, enableflag, writepermission, idletime, uploadrate,downloadrate,maxloginnumber, maxloginperip FROM ftp_user WHERE userid = '{userid}'");
dbUserManagerFactory.setSqlUserSelectAll("SELECT userid FROM ftp_user ORDER BY userid");
dbUserManagerFactory.setDataSource(dataSource);

serverFactory.setUserManager(dbUserManagerFactory.createUserManager());
FtpServer server = serverFactory.createServer();
server.start();

在写完以上代码后,测试中可以拿到数据库中的数据,但是连接Ftp server时一直提示认证失败,在调试了很长时间后,找到了错误:在ftpserver-core-1.1.1-sources.jar > org > apache > ftpserver > usermanager中有个MD5PasswordEncryptor.java文件,其中定义了比较函数

public boolean matches(String passwordToCheck, String storedPassword) {
        if(storedPassword == null) {
            throw new NullPointerException("storedPassword can not be null");
        }
        if(passwordToCheck == null) {
            throw new NullPointerException("passwordToCheck can not be null");
        }

        return encrypt(passwordToCheck).equalsIgnoreCase(storedPassword);
    }

最后一句中用了一个MD5加密函数对用户输入的密码进行了处理后在进行比较的,所以后面的storedPassword要用密文表示才能通过验证。

你可能感兴趣的:(笔记)