1078-增加ftp-server

This commit is contained in:
648540858
2024-05-30 18:36:24 +08:00
parent 56fdbd34f4
commit 89101b2731
4 changed files with 194 additions and 0 deletions

13
pom.xml
View File

@@ -338,6 +338,19 @@
<version>32.1.3-jre</version>
</dependency>
<!--ftp server-->
<dependency>
<groupId>org.apache.ftpserver</groupId>
<artifactId>ftpserver-core</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.ftpserver</groupId>
<artifactId>ftplet-api</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

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

View File

@@ -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<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();
try {
server.start();
} catch (FtpException e) {
System.out.println("ftp-启动失败" + e.getMessage());
}
return server;
}
}

View File

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