Apache FtpServer 用户管理提供两种方式,一种是基于配置文件,一种是数据库。官方网站对Mysql数据库做了测试。
基于配置文件的用户管理,myuser.properties
ftpserver.user.chen.userpassword=123 ftpserver.user.chen.homedirectory=./src/userftp ftpserver.user.chen.maxloginperip=4 ftpserver.user.chen.maxloginnumber=3 ftpserver.user.chen.downloadrate=0 ftpserver.user.chen.uploadrate=0 ftpserver.user.chen.idletime=0 ftpserver.user.chen.writepermission=true ftpserver.user.chen.enableflag=true
动态添加用户,通过PropertiesUserManagerFactory创建UserManager然后向配置文件添加用户,代码如下:
public UserManager getUserManager(){ String userfile = System.getProperty("user.dir")+File.separator+"src"+File.separator+"ftpserver/myuser.properties"; PropertiesUserManagerFactory propusermanagerfactory = new PropertiesUserManagerFactory(); File file = new File(userfile); UserManager usermanager = null; if(file.exists()){ propusermanagerfactory.setFile(file); SaltedPasswordEncryptor salterpassword = new SaltedPasswordEncryptor(); salterpassword.encrypt("clear"); propusermanagerfactory.setPasswordEncryptor(salterpassword); usermanager = propusermanagerfactory.createUserManager(); }else{ System.out.println("用户属性文件不存在"); } return usermanager; } /** * 添加用户 * @param useraccount 用户名 * @param password 密码 * @param homdir 目录地址 * @throws FtpException */ public void addUser(String useraccount,String password,String homdir) throws FtpException{ UserManager usermanager = getUserManager(); BaseUser user = new BaseUser(); user.setHomeDirectory(homdir); user.setName(useraccount); user.setPassword(password); usermanager.save(user);//user 必须是实现User接口的实体类 } /** * 检测用户是否存在 * @param username 用户名 * @return * @throws FtpException */ public boolean exitUser(String username) throws FtpException{ UserManager usermanager = getUserManager(); return usermanager.doesExist(username); } /** * 删除用户 * @param username * @throws FtpException */ public void deletUser(String username) throws FtpException{ UserManager usermanager = getUserManager(); usermanager.delete(username); } //测试调用程序 public static void main(String args[]){ FtpServerUserUtil ftpusertil = new FtpServerUserUtil(); try { ftpusertil.addUser("chen2", "123", "./src/userftp"); } catch (FtpException e) { e.printStackTrace(); } }
BaseUser 是实现了User接口
package com.dt.ftp.util; import java.util.List; import org.apache.ftpserver.ftplet.Authority; import org.apache.ftpserver.ftplet.AuthorizationRequest; import org.apache.ftpserver.ftplet.User; /** * 实现User接口 * @author Administrator * */ public class BaseUser implements User{ private String name; private String password; private String homeDirectory; public void setName(String name) { this.name = name; } public void setPassword(String password) { this.password = password; } public void setHomeDirectory(String homeDirectory) { this.homeDirectory = homeDirectory; } public AuthorizationRequest authorize(AuthorizationRequest arg0) { return null; } public List<Authority> getAuthorities() { return null; } public List<Authority> getAuthorities(Class<? extends Authority> arg0) { return null; } public boolean getEnabled() { return false; } public String getHomeDirectory() { return homeDirectory; } public int getMaxIdleTime() { return 0; } public String getName() { return name; } public String getPassword() { return password; } }
FtpServer 配置如下:
<server xmlns="http://mina.apache.org/ftpserver/spring/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://mina.apache.org/ftpserver/spring/v1 http://mina.apache.org/ftpserver/ftpserver-1.0.xsd" id="server" max-logins="500" anon-enabled="false" max-anon-logins="123" max-login-failures="124" login-failure-delay="125" > <listeners> <nio-listener name="default" port="21"> <ssl> <keystore file="/ftpserver/ftpserver.jks" password="password" /> </ssl> </nio-listener> </listeners> <!-- 文件用户管理 clear 明文--> <file-user-manager file="users.properties" encrypt-passwords="clear" /> </server>