mirror of
https://gitee.com/pan648540858/wvp-GB28181-pro.git
synced 2026-05-25 22:47:49 +08:00
1078-添加ftpserver
This commit is contained in:
parent
89101b2731
commit
e8d832d0a5
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,50 +1,54 @@
|
|||||||
package com.genersoft.iot.vmp.conf.ftpServer;
|
package com.genersoft.iot.vmp.conf.ftpServer;
|
||||||
|
|
||||||
|
import com.genersoft.iot.vmp.jt1078.event.FtpUploadEvent;
|
||||||
import org.apache.ftpserver.ftplet.*;
|
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 org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class FtpPlet implements Ftplet {
|
public class FtpPlet extends DefaultFtplet {
|
||||||
|
|
||||||
private FtpletContext ftpletContext;
|
private FtpletContext ftpletContext;
|
||||||
|
|
||||||
@Override
|
@Value("${ftp.username}")
|
||||||
public void init(FtpletContext ftpletContext) throws FtpException {
|
private String username;
|
||||||
this.ftpletContext = ftpletContext;
|
|
||||||
System.out.println("ftp-init");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Autowired
|
||||||
public void destroy() {
|
private ApplicationEventPublisher applicationEventPublisher;
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FtpletResult beforeCommand(FtpSession session, FtpRequest request) throws FtpException, IOException {
|
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;
|
return FtpletResult.DEFAULT;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FtpletResult afterCommand(FtpSession session, FtpRequest request, FtpReply reply) throws FtpException, IOException {
|
public FtpletResult onUploadStart(FtpSession session, FtpRequest request) throws FtpException, IOException {
|
||||||
System.out.println("ftp-afterCommand");
|
DefaultFtpSession ftpSession = (DefaultFtpSession) session;
|
||||||
return FtpletResult.DEFAULT;
|
return super.onUploadStart(session, request);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FtpletResult onConnect(FtpSession session) throws FtpException, IOException {
|
public FtpletResult onUploadEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
|
||||||
System.out.println("ftp-onConnect");
|
return super.onUploadEnd(session, request);
|
||||||
System.out.println(session.getSessionId());
|
|
||||||
return FtpletResult.DEFAULT;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FtpletResult onDisconnect(FtpSession session) throws FtpException, IOException {
|
public FtpletResult onDownloadStart(FtpSession session, FtpRequest request) throws FtpException, IOException {
|
||||||
System.out.println("ftp-session");
|
return super.onDownloadStart(session, request);
|
||||||
System.out.println(session.getSessionId());
|
|
||||||
return FtpletResult.DEFAULT;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -33,26 +33,18 @@ public class FtpServerConfig {
|
|||||||
// //1、设置服务端口
|
// //1、设置服务端口
|
||||||
listenerFactory.setPort(3131);
|
listenerFactory.setPort(3131);
|
||||||
//2、设置被动模式数据上传的接口范围,云服务器需要开放对应区间的端口给客户端
|
//2、设置被动模式数据上传的接口范围,云服务器需要开放对应区间的端口给客户端
|
||||||
// DataConnectionConfigurationFactory dataConnectionConfFactory = new DataConnectionConfigurationFactory();
|
DataConnectionConfigurationFactory dataConnectionConfFactory = new DataConnectionConfigurationFactory();
|
||||||
// dataConnectionConfFactory.setPassivePorts("10000-10500");
|
dataConnectionConfFactory.setPassivePorts("10000-10500");
|
||||||
// listenerFactory.setDataConnectionConfiguration(dataConnectionConfFactory.createDataConnectionConfiguration());
|
listenerFactory.setDataConnectionConfiguration(dataConnectionConfFactory.createDataConnectionConfiguration());
|
||||||
//4、替换默认的监听器
|
//4、替换默认的监听器
|
||||||
Listener listener = listenerFactory.createListener();
|
Listener listener = listenerFactory.createListener();
|
||||||
serverFactory.addListener("default", listener);
|
serverFactory.addListener("default", listener);
|
||||||
//5、配置自定义用户事件
|
//5、配置自定义用户事件
|
||||||
// Map<String, Ftplet> ftpLets = new HashMap();
|
Map<String, Ftplet> ftpLets = new HashMap();
|
||||||
// ftpLets.put("ftpService", ftpPlet);
|
ftpLets.put("ftpService", ftpPlet);
|
||||||
// serverFactory.setFtplets(ftpLets);
|
serverFactory.setFtplets(ftpLets);
|
||||||
//6、读取用户的配置信息
|
//6、读取用户的配置信息
|
||||||
//6.2、设置用信息
|
//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);
|
serverFactory.setUserManager(userManager);
|
||||||
//7、实例化FTP Server
|
//7、实例化FTP Server
|
||||||
FtpServer server = serverFactory.createServer();
|
FtpServer server = serverFactory.createServer();
|
||||||
|
|||||||
@ -1,65 +1,89 @@
|
|||||||
package com.genersoft.iot.vmp.conf.ftpServer;
|
package com.genersoft.iot.vmp.conf.ftpServer;
|
||||||
|
|
||||||
import org.apache.ftpserver.ftplet.Authentication;
|
import org.apache.ftpserver.ftplet.*;
|
||||||
import org.apache.ftpserver.ftplet.AuthenticationFailedException;
|
import org.apache.ftpserver.usermanager.UsernamePasswordAuthentication;
|
||||||
import org.apache.ftpserver.ftplet.FtpException;
|
|
||||||
import org.apache.ftpserver.ftplet.User;
|
|
||||||
import org.apache.ftpserver.usermanager.impl.BaseUser;
|
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 org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class UserManager implements org.apache.ftpserver.ftplet.UserManager {
|
public class UserManager implements org.apache.ftpserver.ftplet.UserManager {
|
||||||
|
|
||||||
|
@Value("${ftp.username}")
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
@Value("${ftp.password}")
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public User getUserByName(String username) throws FtpException {
|
public User getUserByName(String username) throws FtpException {
|
||||||
BaseUser use = new BaseUser();
|
if (!username.equals(this.username)) {
|
||||||
use.setName("admin");
|
return null;
|
||||||
use.setPassword("admin123");
|
}
|
||||||
use.setEnabled(true);
|
return getUser();
|
||||||
use.setHomeDirectory("/home/lin");
|
|
||||||
use.setMaxIdleTime(100);
|
|
||||||
return use;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String[] getAllUserNames() throws FtpException {
|
public String[] getAllUserNames() throws FtpException {
|
||||||
String[] strings = new String[1];
|
String[] strings = new String[1];
|
||||||
strings[0] = "admin";
|
strings[0] = this.username;
|
||||||
return strings;
|
return strings;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void delete(String username) throws FtpException {
|
public void delete(String username) throws FtpException {}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void save(User user) throws FtpException {
|
public void save(User user) throws FtpException {}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean doesExist(String username) throws FtpException {
|
public boolean doesExist(String username) throws FtpException {
|
||||||
return username.equals("admin");
|
return this.username.equals(username);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public User authenticate(Authentication authentication) throws AuthenticationFailedException {
|
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();
|
BaseUser use = new BaseUser();
|
||||||
use.setName("admin");
|
use.setName(this.username);
|
||||||
use.setPassword("admin123");
|
use.setPassword(this.password);
|
||||||
use.setEnabled(true);
|
use.setEnabled(true);
|
||||||
use.setHomeDirectory("/home/lin");
|
File file = new File("ftp");
|
||||||
use.setMaxIdleTime(100);
|
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;
|
return use;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getAdminName() throws FtpException {
|
public String getAdminName() throws FtpException {
|
||||||
return "admin";
|
return this.username;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isAdmin(String username) throws FtpException {
|
public boolean isAdmin(String username) throws FtpException {
|
||||||
return username.equals("admin");
|
return username.equals(this.username);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user