1078-添加ftpserver

This commit is contained in:
648540858
2024-05-30 23:41:22 +08:00
parent 89101b2731
commit e8d832d0a5
5 changed files with 117 additions and 60 deletions

View File

@@ -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;
}
}

View File

@@ -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);
}
}

View File

@@ -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<String, Ftplet> ftpLets = new HashMap();
// ftpLets.put("ftpService", ftpPlet);
// serverFactory.setFtplets(ftpLets);
Map<String, Ftplet> 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();

View File

@@ -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<Authority> 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);
}
}