From e8d832d0a5209b874349c3a75037836cade8efa6 Mon Sep 17 00:00:00 2001 From: 648540858 <648540858@qq.com> Date: Thu, 30 May 2024 23:41:22 +0800 Subject: [PATCH] =?UTF-8?q?1078-=E6=B7=BB=E5=8A=A0ftpserver?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iot/vmp/conf/ftpServer/FtpAuthority.java | 17 +++++ .../iot/vmp/conf/ftpServer/FtpPlet.java | 48 +++++++------ .../vmp/conf/ftpServer/FtpServerConfig.java | 20 ++---- .../iot/vmp/conf/ftpServer/UserManager.java | 72 ++++++++++++------- .../iot/vmp/jt1078/event/FtpUploadEvent.java | 20 ++++++ 5 files changed, 117 insertions(+), 60 deletions(-) create mode 100644 src/main/java/com/genersoft/iot/vmp/conf/ftpServer/FtpAuthority.java create mode 100644 src/main/java/com/genersoft/iot/vmp/jt1078/event/FtpUploadEvent.java diff --git a/src/main/java/com/genersoft/iot/vmp/conf/ftpServer/FtpAuthority.java b/src/main/java/com/genersoft/iot/vmp/conf/ftpServer/FtpAuthority.java new file mode 100644 index 000000000..0ccb14452 --- /dev/null +++ b/src/main/java/com/genersoft/iot/vmp/conf/ftpServer/FtpAuthority.java @@ -0,0 +1,17 @@ +package com.genersoft.iot.vmp.conf.ftpServer; + +import org.apache.ftpserver.ftplet.Authority; +import org.apache.ftpserver.ftplet.AuthorizationRequest; + +public class FtpAuthority implements Authority { + + @Override + public boolean canAuthorize(AuthorizationRequest authorizationRequest) { + return true; + } + + @Override + public AuthorizationRequest authorize(AuthorizationRequest authorizationRequest) { + return authorizationRequest; + } +} 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 index 35825a1d2..6e63b39a6 100644 --- a/src/main/java/com/genersoft/iot/vmp/conf/ftpServer/FtpPlet.java +++ b/src/main/java/com/genersoft/iot/vmp/conf/ftpServer/FtpPlet.java @@ -1,50 +1,54 @@ package com.genersoft.iot.vmp.conf.ftpServer; +import com.genersoft.iot.vmp.jt1078.event.FtpUploadEvent; import org.apache.ftpserver.ftplet.*; +import org.apache.ftpserver.impl.DefaultFtpSession; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Component; import java.io.IOException; @Component -public class FtpPlet implements Ftplet { +public class FtpPlet extends DefaultFtplet { private FtpletContext ftpletContext; - @Override - public void init(FtpletContext ftpletContext) throws FtpException { - this.ftpletContext = ftpletContext; - System.out.println("ftp-init"); - } + @Value("${ftp.username}") + private String username; - @Override - public void destroy() { - - } + @Autowired + private ApplicationEventPublisher applicationEventPublisher; @Override public FtpletResult beforeCommand(FtpSession session, FtpRequest request) throws FtpException, IOException { - System.out.println("ftp-beforeCommand"); + if (request.getCommand().equalsIgnoreCase("USER") && !username.equals(request.getArgument())) { + return FtpletResult.DISCONNECT; + } + super.beforeCommand(session, request); +// if (request.getCommand().equalsIgnoreCase("STOR") ) { +// FtpUploadEvent ftpUploadEvent = new FtpUploadEvent(this); +// ftpUploadEvent.setFileName(request.getArgument()); +// applicationEventPublisher.publishEvent(ftpUploadEvent); +// } return FtpletResult.DEFAULT; } @Override - public FtpletResult afterCommand(FtpSession session, FtpRequest request, FtpReply reply) throws FtpException, IOException { - System.out.println("ftp-afterCommand"); - return FtpletResult.DEFAULT; + public FtpletResult onUploadStart(FtpSession session, FtpRequest request) throws FtpException, IOException { + DefaultFtpSession ftpSession = (DefaultFtpSession) session; + return super.onUploadStart(session, request); } @Override - public FtpletResult onConnect(FtpSession session) throws FtpException, IOException { - System.out.println("ftp-onConnect"); - System.out.println(session.getSessionId()); - return FtpletResult.DEFAULT; + public FtpletResult onUploadEnd(FtpSession session, FtpRequest request) throws FtpException, IOException { + return super.onUploadEnd(session, request); } @Override - public FtpletResult onDisconnect(FtpSession session) throws FtpException, IOException { - System.out.println("ftp-session"); - System.out.println(session.getSessionId()); - return FtpletResult.DEFAULT; + public FtpletResult onDownloadStart(FtpSession session, FtpRequest request) throws FtpException, IOException { + return super.onDownloadStart(session, request); } } 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 index 3d56f1642..332986f1a 100644 --- a/src/main/java/com/genersoft/iot/vmp/conf/ftpServer/FtpServerConfig.java +++ b/src/main/java/com/genersoft/iot/vmp/conf/ftpServer/FtpServerConfig.java @@ -33,26 +33,18 @@ public class FtpServerConfig { // //1、设置服务端口 listenerFactory.setPort(3131); //2、设置被动模式数据上传的接口范围,云服务器需要开放对应区间的端口给客户端 -// DataConnectionConfigurationFactory dataConnectionConfFactory = new DataConnectionConfigurationFactory(); -// dataConnectionConfFactory.setPassivePorts("10000-10500"); -// listenerFactory.setDataConnectionConfiguration(dataConnectionConfFactory.createDataConnectionConfiguration()); + 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); + 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(); 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 index 8b634f67d..b84d06716 100644 --- a/src/main/java/com/genersoft/iot/vmp/conf/ftpServer/UserManager.java +++ b/src/main/java/com/genersoft/iot/vmp/conf/ftpServer/UserManager.java @@ -1,65 +1,89 @@ 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.ftplet.*; +import org.apache.ftpserver.usermanager.UsernamePasswordAuthentication; import org.apache.ftpserver.usermanager.impl.BaseUser; +import org.jetbrains.annotations.NotNull; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; +import java.io.File; +import java.util.ArrayList; +import java.util.List; + @Component public class UserManager implements org.apache.ftpserver.ftplet.UserManager { + @Value("${ftp.username}") + private String username; + + @Value("${ftp.password}") + private String password; + + @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; + if (!username.equals(this.username)) { + return null; + } + return getUser(); } @Override public String[] getAllUserNames() throws FtpException { String[] strings = new String[1]; - strings[0] = "admin"; + strings[0] = this.username; return strings; } @Override - public void delete(String username) throws FtpException { - } + public void delete(String username) throws FtpException {} @Override - public void save(User user) throws FtpException { - - } + public void save(User user) throws FtpException {} @Override public boolean doesExist(String username) throws FtpException { - return username.equals("admin"); + return this.username.equals(username); } @Override public User authenticate(Authentication authentication) throws AuthenticationFailedException { + UsernamePasswordAuthentication usernamePasswordAuthentication = (UsernamePasswordAuthentication) authentication; + if (usernamePasswordAuthentication.getUsername().equals(this.username) + && usernamePasswordAuthentication.getPassword().equals(this.password)) { + return getUser(); + } + return null; + } + + @NotNull + private User getUser() { BaseUser use = new BaseUser(); - use.setName("admin"); - use.setPassword("admin123"); + use.setName(this.username); + use.setPassword(this.password); use.setEnabled(true); - use.setHomeDirectory("/home/lin"); - use.setMaxIdleTime(100); + File file = new File("ftp"); + if (!file.exists()) { + file.mkdirs(); + }else if (file.isFile()) { + file.delete(); + file.mkdirs(); + } + use.setHomeDirectory(file.getAbsolutePath()); + List authorities = new ArrayList<>(); + authorities.add(new FtpAuthority()); + use.setAuthorities(authorities); return use; } @Override public String getAdminName() throws FtpException { - return "admin"; + return this.username; } @Override public boolean isAdmin(String username) throws FtpException { - return username.equals("admin"); + return username.equals(this.username); } } diff --git a/src/main/java/com/genersoft/iot/vmp/jt1078/event/FtpUploadEvent.java b/src/main/java/com/genersoft/iot/vmp/jt1078/event/FtpUploadEvent.java new file mode 100644 index 000000000..44e17cd15 --- /dev/null +++ b/src/main/java/com/genersoft/iot/vmp/jt1078/event/FtpUploadEvent.java @@ -0,0 +1,20 @@ +package com.genersoft.iot.vmp.jt1078.event; + +import org.springframework.context.ApplicationEvent; + +public class FtpUploadEvent extends ApplicationEvent { + + public FtpUploadEvent(Object source) { + super(source); + } + + private String fileName; + + public String getFileName() { + return fileName; + } + + public void setFileName(String fileName) { + this.fileName = fileName; + } +}