How to allow anonymous login to org.apache.ftpserver?

I wrote some code like this to start the ftp server built into my application. It is based on apache ftpserver

I found that an anonymous user was unable to log in. Customer continues to receive 530.

Do I have a configuration file for ftp? I cannot find an API to create a user to add to 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(); } 
+4
source share
3 answers

To log in anonymously through Apache FtpServer, you must enable anonymous authentication, and then add the "anonymous" user to the UserManager.

Here is a snippet that sets up anonymous authentication:

 FtpServerFactory serverFactory = new FtpServerFactory(); ConnectionConfigFactory connectionConfigFactory = new ConnectionConfigFactory(); connectionConfigFactory.setAnonymousLoginEnabled(false); serverFactory.setConnectionConfig(connectionConfigFactory.createConnectionConfig()); serverFactory.setUserManager(new TestUserManagerFactory().createUserManager()); startFtpServer(serverFactory); 

Then you can provide a UserManager that authenticates and authorizes logins. Here is the custom UserManagerFactory and 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; } } 

The bit that really matters is the return of anonUser .

NTN

+1
source

Native UserManager is not needed. Try the following:

 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); 
+4
source

Try setting anon-enabled="true" in your server settings.

0
source

All Articles