我写了一些像这样的代码来启动我的应用程序中嵌入的ftp服务器。它基于apache ftpserver
我发现匿名用户无法登录。客户端保持530。
我是否为ftp添加了配置文件?我找不到任何API来创建要添加到UserManger的用户。
private void start_ftp() throws FtpException {
FtpServerFactory serverFactory = new FtpServerFactory();
ListenerFactory factory = new ListenerFactory();
// set the port of the listener
factory.setPort(DEF_FTP_PORT);
// replace the default listener
serverFactory.addListener("default", factory.createListener());
Ftplet fl = new MyFtplet();
Map<String, Ftplet> map_ftplest = new LinkedHashMap<String, Ftplet>();
map_ftplest.put("default", fl);
serverFactory.setFtplets(map_ftplest);
UserManagerFactory u_factory = new PropertiesUserManagerFactory();
UserManager u_manager = u_factory.createUserManager();
//u_manager.
Boolean b = u_manager.doesExist("anonymous");
serverFactory.setUserManager(u_manager);
// start the server
server = serverFactory.createServer();
server.start();
}
答案 0 :(得分:7)
不需要自己的UserManager。试试这个:
FtpServerFactory serverFactory = new FtpServerFactory();
ConnectionConfigFactory connectionConfigFactory = new ConnectionConfigFactory();
connectionConfigFactory.setAnonymousLoginEnabled(true);
serverFactory.setConnectionConfig(connectionConfigFactory.createConnectionConfig());
BaseUser user = new BaseUser();
user.setName("anonymous");
serverFactory.getUserManager().save(user);
startFtpServer(serverFactory);
答案 1 :(得分:1)
要通过Apache FtpServer实现匿名登录,您必须启用匿名身份验证,然后向UserManager添加“匿名”用户。
以下是设置匿名身份验证的代码段:
FtpServerFactory serverFactory = new FtpServerFactory();
ConnectionConfigFactory connectionConfigFactory = new ConnectionConfigFactory();
connectionConfigFactory.setAnonymousLoginEnabled(false);
serverFactory.setConnectionConfig(connectionConfigFactory.createConnectionConfig());
serverFactory.setUserManager(new TestUserManagerFactory().createUserManager());
startFtpServer(serverFactory);
然后,您可以提供验证和授权登录的UserManager。这是一个自定义的UserManagerFactory和AbstractUserManager:
private class TestUserManagerFactory implements UserManagerFactory {
@Override
public UserManager createUserManager() {
return new TestUserManager("admin", new ClearTextPasswordEncryptor());
}
}
private class TestUserManager extends AbstractUserManager {
private BaseUser testUser;
private BaseUser anonUser;
public TestUserManager(String adminName, PasswordEncryptor passwordEncryptor) {
super(adminName, passwordEncryptor);
testUser = new BaseUser();
testUser.setAuthorities(Arrays.asList(new Authority[] {new ConcurrentLoginPermission(1, 1)}));
testUser.setEnabled(true);
testUser.setHomeDirectory(TEST_USER_FTP_ROOT);
testUser.setMaxIdleTime(10000);
testUser.setName(TEST_USERNAME);
testUser.setPassword(TEST_PASSWORD);
anonUser = new BaseUser(testUser);
anonUser.setName("anonymous");
}
@Override
public User getUserByName(String username) throws FtpException {
if(TEST_USERNAME.equals(username)) {
return testUser;
} else if(anonUser.getName().equals(username)) {
return anonUser;
}
return null;
}
@Override
public String[] getAllUserNames() throws FtpException {
return new String[] {TEST_USERNAME, anonUser.getName()};
}
@Override
public void delete(String username) throws FtpException {
//no opt
}
@Override
public void save(User user) throws FtpException {
//no opt
System.out.println("save");
}
@Override
public boolean doesExist(String username) throws FtpException {
return (TEST_USERNAME.equals(username) || anonUser.getName().equals(username)) ? true : false;
}
@Override
public User authenticate(Authentication authentication) throws AuthenticationFailedException {
if(UsernamePasswordAuthentication.class.isAssignableFrom(authentication.getClass())) {
UsernamePasswordAuthentication upAuth = (UsernamePasswordAuthentication) authentication;
if(TEST_USERNAME.equals(upAuth.getUsername()) && TEST_PASSWORD.equals(upAuth.getPassword())) {
return testUser;
}
if(anonUser.getName().equals(upAuth.getUsername())) {
return anonUser;
}
} else if(AnonymousAuthentication.class.isAssignableFrom(authentication.getClass())) {
return anonUser;
}
return null;
}
}
真正重要的是 anonUser 的回归。
HTH
答案 2 :(得分:0)
尝试在服务器配置中设置anon-enabled="true"
。