[1078] FTP使用随机用户和密码
This commit is contained in:
@@ -17,7 +17,5 @@ public class FtpSetting {
|
||||
private Boolean enable = Boolean.FALSE;
|
||||
|
||||
private int port = 21;
|
||||
private String username = "admin";
|
||||
private String password = "admin";
|
||||
private String passivePorts = "10000-10500";
|
||||
}
|
||||
|
||||
@@ -17,22 +17,9 @@ public class Ftplet extends DefaultFtplet {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(Ftplet.class);
|
||||
|
||||
@Value("${ftp.username}")
|
||||
private String username;
|
||||
|
||||
@Autowired
|
||||
private ApplicationEventPublisher applicationEventPublisher;
|
||||
|
||||
@Override
|
||||
public FtpletResult beforeCommand(FtpSession session, FtpRequest request) throws FtpException, IOException {
|
||||
System.out.println("beforeCommand");
|
||||
if (request.getCommand().equalsIgnoreCase("USER") && !username.equals(request.getArgument())) {
|
||||
return FtpletResult.DISCONNECT;
|
||||
}
|
||||
super.beforeCommand(session, request);
|
||||
return FtpletResult.DEFAULT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FtpletResult onUploadEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
|
||||
FtpFile file = session.getFileSystemView().getFile(request.getArgument());
|
||||
@@ -43,8 +30,6 @@ public class Ftplet extends DefaultFtplet {
|
||||
return super.onUploadUniqueEnd(session, request);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public FtpletResult onAppendEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
|
||||
FtpFile file = session.getFileSystemView().getFile(request.getArgument());
|
||||
|
||||
@@ -1,90 +1,86 @@
|
||||
package com.genersoft.iot.vmp.conf.ftpServer;
|
||||
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
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.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.File;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class UserManager implements org.apache.ftpserver.ftplet.UserManager {
|
||||
|
||||
@Value("${ftp.username}")
|
||||
private String username;
|
||||
private static final String PREFIX = "VMP_FTP_USER_";
|
||||
|
||||
@Value("${ftp.password}")
|
||||
private String password;
|
||||
@Autowired
|
||||
private RedisTemplate<Object, Object> redisTemplate;
|
||||
|
||||
|
||||
@Override
|
||||
public User getUserByName(String username) throws FtpException {
|
||||
System.out.println("getUserByName");
|
||||
if (!username.equals(this.username)) {
|
||||
return null;
|
||||
}
|
||||
return getUser();
|
||||
return (BaseUser)redisTemplate.opsForValue().get(PREFIX + username);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getAllUserNames() throws FtpException {
|
||||
String[] strings = new String[1];
|
||||
strings[0] = this.username;
|
||||
return strings;
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String username) throws FtpException {}
|
||||
public void delete(String username) throws FtpException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(User user) throws FtpException {}
|
||||
|
||||
@Override
|
||||
public boolean doesExist(String username) throws FtpException {
|
||||
return this.username.equals(username);
|
||||
return redisTemplate.opsForValue().get(PREFIX + username) != null;
|
||||
}
|
||||
|
||||
@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();
|
||||
BaseUser user = (BaseUser)redisTemplate.opsForValue().get(PREFIX + usernamePasswordAuthentication.getUsername());
|
||||
if (user != null && usernamePasswordAuthentication.getPassword().equals(user.getPassword())) {
|
||||
return user;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private User getUser() {
|
||||
BaseUser use = new BaseUser();
|
||||
use.setName(this.username);
|
||||
use.setPassword(this.password);
|
||||
use.setEnabled(true);
|
||||
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 this.username;
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAdmin(String username) throws FtpException {
|
||||
return username.equals(this.username);
|
||||
return false;
|
||||
}
|
||||
|
||||
public BaseUser getRandomUser(){
|
||||
BaseUser use = new BaseUser();
|
||||
use.setName(RandomStringUtils.randomAlphabetic(6).toLowerCase());
|
||||
use.setPassword(RandomStringUtils.randomAlphabetic(6).toLowerCase());
|
||||
use.setEnabled(true);
|
||||
use.setHomeDirectory("/");
|
||||
List<Authority> authorities = new ArrayList<>();
|
||||
authorities.add(new FtpAuthority());
|
||||
use.setAuthorities(authorities);
|
||||
String key = PREFIX + use.getName();
|
||||
|
||||
// 随机用户信息十分钟自动失效
|
||||
Duration duration = Duration.ofMinutes(10);
|
||||
redisTemplate.opsForValue().set(key, use, duration);
|
||||
return use;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import com.genersoft.iot.vmp.conf.ftpServer.FtpFileSystemFactory;
|
||||
import com.genersoft.iot.vmp.conf.ftpServer.FtpSetting;
|
||||
import com.genersoft.iot.vmp.conf.UserSetting;
|
||||
import com.genersoft.iot.vmp.conf.exception.ControllerException;
|
||||
import com.genersoft.iot.vmp.conf.ftpServer.UserManager;
|
||||
import com.genersoft.iot.vmp.gb28181.service.IGbChannelService;
|
||||
import com.genersoft.iot.vmp.jt1078.bean.*;
|
||||
import com.genersoft.iot.vmp.jt1078.bean.common.ConfigAttribute;
|
||||
@@ -32,6 +33,7 @@ import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.apache.ftpserver.usermanager.impl.BaseUser;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
@@ -77,6 +79,9 @@ public class jt1078ServiceImpl implements Ijt1078Service {
|
||||
@Autowired
|
||||
private FtpSetting ftpSetting;
|
||||
|
||||
@Autowired
|
||||
private UserManager ftpUserManager;
|
||||
|
||||
@Autowired
|
||||
private FtpFileSystemFactory fileSystemFactory;
|
||||
|
||||
@@ -205,8 +210,11 @@ public class jt1078ServiceImpl implements Ijt1078Service {
|
||||
Assert.notNull(session, "连接不存在");
|
||||
InetSocketAddress socketAddress = session.getLoadAddress();
|
||||
String hostName = socketAddress.getHostName();
|
||||
log.info("[JT-录像] 下载,设备:{}, 通道: {}, 开始时间: {}, 结束时间: {} 上传IP: {} 等待上传文件路径: {} ",
|
||||
phoneNumber, channelId, startTime, endTime, hostName, filePath);
|
||||
|
||||
BaseUser randomUser = ftpUserManager.getRandomUser();
|
||||
|
||||
log.info("[JT-录像] 下载,设备:{}, 通道: {}, 开始时间: {}, 结束时间: {} 上传IP: {} 等待上传文件路径: {} 用户名: {}, 密码: {} ",
|
||||
phoneNumber, channelId, startTime, endTime, hostName, filePath, randomUser.getName(), randomUser.getPassword());
|
||||
// 发送停止命令
|
||||
J9206 j92026 = new J9206();
|
||||
j92026.setChannelId(channelId);
|
||||
@@ -214,8 +222,8 @@ public class jt1078ServiceImpl implements Ijt1078Service {
|
||||
j92026.setEndTime(DateUtil.yyyy_MM_dd_HH_mm_ssTo1078(endTime));
|
||||
j92026.setServerIp(hostName);
|
||||
j92026.setPort(ftpSetting.getPort());
|
||||
j92026.setUsername(ftpSetting.getUsername());
|
||||
j92026.setPassword(ftpSetting.getPassword());
|
||||
j92026.setUsername(randomUser.getName());
|
||||
j92026.setPassword(randomUser.getPassword());
|
||||
j92026.setPath(filePath);
|
||||
|
||||
if (mediaType != null) {
|
||||
@@ -733,8 +741,10 @@ public class jt1078ServiceImpl implements Ijt1078Service {
|
||||
InetSocketAddress socketAddress = session.getLoadAddress();
|
||||
String hostName = socketAddress.getHostName();
|
||||
|
||||
log.info("[JT-录像] 下载,设备:{}, 通道: {}, 开始时间: {}, 结束时间: {} 上传IP: {} 等待上传文件路径: {} ",
|
||||
phoneNumber, channelId, startTime, endTime, hostName, filePath);
|
||||
BaseUser randomUser = ftpUserManager.getRandomUser();
|
||||
|
||||
log.info("[JT-录像] 下载,设备:{}, 通道: {}, 开始时间: {}, 结束时间: {} 上传IP: {} 等待上传文件路径: {} 用户名: {}, 密码: {} ",
|
||||
phoneNumber, channelId, startTime, endTime, hostName, filePath, randomUser.getName(), randomUser.getPassword());
|
||||
// 文件上传指令
|
||||
J9206 j9206 = new J9206();
|
||||
j9206.setChannelId(channelId);
|
||||
@@ -742,8 +752,8 @@ public class jt1078ServiceImpl implements Ijt1078Service {
|
||||
j9206.setEndTime(DateUtil.yyyy_MM_dd_HH_mm_ssTo1078(endTime));
|
||||
j9206.setServerIp(hostName);
|
||||
j9206.setPort(ftpSetting.getPort());
|
||||
j9206.setUsername(ftpSetting.getUsername());
|
||||
j9206.setPassword(ftpSetting.getPassword());
|
||||
j9206.setUsername(randomUser.getName());
|
||||
j9206.setPassword(randomUser.getPassword());
|
||||
j9206.setPath(filePath);
|
||||
|
||||
if (mediaType != null) {
|
||||
|
||||
Reference in New Issue
Block a user