diff --git a/pom.xml b/pom.xml index 7b60b6931..b04a0e9a2 100644 --- a/pom.xml +++ b/pom.xml @@ -338,6 +338,19 @@ 32.1.3-jre + + + org.apache.ftpserver + ftpserver-core + 1.2.0 + + + org.apache.ftpserver + ftplet-api + 1.2.0 + + + org.springframework.boot spring-boot-starter-test diff --git a/src/main/java/com/genersoft/iot/vmp/conf/ftpServer/FtpPlet.java b/src/main/java/com/genersoft/iot/vmp/conf/ftpServer/FtpPlet.java new file mode 100644 index 000000000..35825a1d2 --- /dev/null +++ b/src/main/java/com/genersoft/iot/vmp/conf/ftpServer/FtpPlet.java @@ -0,0 +1,50 @@ +package com.genersoft.iot.vmp.conf.ftpServer; + +import org.apache.ftpserver.ftplet.*; +import org.springframework.stereotype.Component; + +import java.io.IOException; + + +@Component +public class FtpPlet implements Ftplet { + + private FtpletContext ftpletContext; + + @Override + public void init(FtpletContext ftpletContext) throws FtpException { + this.ftpletContext = ftpletContext; + System.out.println("ftp-init"); + } + + @Override + public void destroy() { + + } + + @Override + public FtpletResult beforeCommand(FtpSession session, FtpRequest request) throws FtpException, IOException { + System.out.println("ftp-beforeCommand"); + return FtpletResult.DEFAULT; + } + + @Override + public FtpletResult afterCommand(FtpSession session, FtpRequest request, FtpReply reply) throws FtpException, IOException { + System.out.println("ftp-afterCommand"); + return FtpletResult.DEFAULT; + } + + @Override + public FtpletResult onConnect(FtpSession session) throws FtpException, IOException { + System.out.println("ftp-onConnect"); + System.out.println(session.getSessionId()); + return FtpletResult.DEFAULT; + } + + @Override + public FtpletResult onDisconnect(FtpSession session) throws FtpException, IOException { + System.out.println("ftp-session"); + System.out.println(session.getSessionId()); + return FtpletResult.DEFAULT; + } +} diff --git a/src/main/java/com/genersoft/iot/vmp/conf/ftpServer/FtpServerConfig.java b/src/main/java/com/genersoft/iot/vmp/conf/ftpServer/FtpServerConfig.java new file mode 100644 index 000000000..3d56f1642 --- /dev/null +++ b/src/main/java/com/genersoft/iot/vmp/conf/ftpServer/FtpServerConfig.java @@ -0,0 +1,66 @@ +package com.genersoft.iot.vmp.conf.ftpServer; + +import org.apache.ftpserver.*; +import org.apache.ftpserver.ftplet.FtpException; +import org.apache.ftpserver.ftplet.Ftplet; +import org.apache.ftpserver.listener.Listener; +import org.apache.ftpserver.listener.ListenerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.HashMap; +import java.util.Map; + +@Configuration +@ConditionalOnProperty(value = "ftp.enable", havingValue = "true") +public class FtpServerConfig { + + @Autowired + private UserManager userManager; + + @Autowired + private FtpPlet ftpPlet; + + /** + * ftp server init + */ + @Bean + public FtpServer ftpServer() { + FtpServerFactory serverFactory = new FtpServerFactory(); + ListenerFactory listenerFactory = new ListenerFactory(); +// //1、设置服务端口 + listenerFactory.setPort(3131); + //2、设置被动模式数据上传的接口范围,云服务器需要开放对应区间的端口给客户端 +// DataConnectionConfigurationFactory dataConnectionConfFactory = new DataConnectionConfigurationFactory(); +// dataConnectionConfFactory.setPassivePorts("10000-10500"); +// listenerFactory.setDataConnectionConfiguration(dataConnectionConfFactory.createDataConnectionConfiguration()); + //4、替换默认的监听器 + Listener listener = listenerFactory.createListener(); + serverFactory.addListener("default", listener); + //5、配置自定义用户事件 +// Map ftpLets = new HashMap(); +// ftpLets.put("ftpService", ftpPlet); +// serverFactory.setFtplets(ftpLets); + //6、读取用户的配置信息 + //6.2、设置用信息 + ConnectionConfigFactory connectionConfigFactory = new ConnectionConfigFactory(); + connectionConfigFactory.setAnonymousLoginEnabled(true); +// connectionConfigFactory.setLoginFailureDelay(1000); +// connectionConfigFactory.setMaxAnonymousLogins(100); +// connectionConfigFactory.setMaxLoginFailures(30); +// connectionConfigFactory.setMaxThreads(10); + ConnectionConfig connectionConfig = connectionConfigFactory.createConnectionConfig(); + serverFactory.setConnectionConfig(connectionConfig); + serverFactory.setUserManager(userManager); + //7、实例化FTP Server + FtpServer server = serverFactory.createServer(); + try { + server.start(); + } catch (FtpException e) { + System.out.println("ftp-启动失败" + e.getMessage()); + } + return server; + } +} diff --git a/src/main/java/com/genersoft/iot/vmp/conf/ftpServer/UserManager.java b/src/main/java/com/genersoft/iot/vmp/conf/ftpServer/UserManager.java new file mode 100644 index 000000000..8b634f67d --- /dev/null +++ b/src/main/java/com/genersoft/iot/vmp/conf/ftpServer/UserManager.java @@ -0,0 +1,65 @@ +package com.genersoft.iot.vmp.conf.ftpServer; + +import org.apache.ftpserver.ftplet.Authentication; +import org.apache.ftpserver.ftplet.AuthenticationFailedException; +import org.apache.ftpserver.ftplet.FtpException; +import org.apache.ftpserver.ftplet.User; +import org.apache.ftpserver.usermanager.impl.BaseUser; +import org.springframework.stereotype.Component; + +@Component +public class UserManager implements org.apache.ftpserver.ftplet.UserManager { + + @Override + public User getUserByName(String username) throws FtpException { + BaseUser use = new BaseUser(); + use.setName("admin"); + use.setPassword("admin123"); + use.setEnabled(true); + use.setHomeDirectory("/home/lin"); + use.setMaxIdleTime(100); + return use; + } + + @Override + public String[] getAllUserNames() throws FtpException { + String[] strings = new String[1]; + strings[0] = "admin"; + return strings; + } + + @Override + public void delete(String username) throws FtpException { + } + + @Override + public void save(User user) throws FtpException { + + } + + @Override + public boolean doesExist(String username) throws FtpException { + return username.equals("admin"); + } + + @Override + public User authenticate(Authentication authentication) throws AuthenticationFailedException { + BaseUser use = new BaseUser(); + use.setName("admin"); + use.setPassword("admin123"); + use.setEnabled(true); + use.setHomeDirectory("/home/lin"); + use.setMaxIdleTime(100); + return use; + } + + @Override + public String getAdminName() throws FtpException { + return "admin"; + } + + @Override + public boolean isAdmin(String username) throws FtpException { + return username.equals("admin"); + } +}