mirror of
https://gitee.com/pan648540858/wvp-GB28181-pro.git
synced 2026-05-24 22:17:49 +08:00
[1078] FTP使用随机用户和密码
This commit is contained in:
parent
9e880786b3
commit
f3bd58acfe
@ -17,7 +17,5 @@ public class FtpSetting {
|
|||||||
private Boolean enable = Boolean.FALSE;
|
private Boolean enable = Boolean.FALSE;
|
||||||
|
|
||||||
private int port = 21;
|
private int port = 21;
|
||||||
private String username = "admin";
|
|
||||||
private String password = "admin";
|
|
||||||
private String passivePorts = "10000-10500";
|
private String passivePorts = "10000-10500";
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,22 +17,9 @@ public class Ftplet extends DefaultFtplet {
|
|||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(Ftplet.class);
|
private final Logger logger = LoggerFactory.getLogger(Ftplet.class);
|
||||||
|
|
||||||
@Value("${ftp.username}")
|
|
||||||
private String username;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ApplicationEventPublisher applicationEventPublisher;
|
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
|
@Override
|
||||||
public FtpletResult onUploadEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
|
public FtpletResult onUploadEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
|
||||||
FtpFile file = session.getFileSystemView().getFile(request.getArgument());
|
FtpFile file = session.getFileSystemView().getFile(request.getArgument());
|
||||||
@ -43,8 +30,6 @@ public class Ftplet extends DefaultFtplet {
|
|||||||
return super.onUploadUniqueEnd(session, request);
|
return super.onUploadUniqueEnd(session, request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FtpletResult onAppendEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
|
public FtpletResult onAppendEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
|
||||||
FtpFile file = session.getFileSystemView().getFile(request.getArgument());
|
FtpFile file = session.getFileSystemView().getFile(request.getArgument());
|
||||||
|
|||||||
@ -1,90 +1,86 @@
|
|||||||
package com.genersoft.iot.vmp.conf.ftpServer;
|
package com.genersoft.iot.vmp.conf.ftpServer;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.RandomStringUtils;
|
||||||
import org.apache.ftpserver.ftplet.*;
|
import org.apache.ftpserver.ftplet.*;
|
||||||
import org.apache.ftpserver.usermanager.UsernamePasswordAuthentication;
|
import org.apache.ftpserver.usermanager.UsernamePasswordAuthentication;
|
||||||
import org.apache.ftpserver.usermanager.impl.BaseUser;
|
import org.apache.ftpserver.usermanager.impl.BaseUser;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.time.Duration;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
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 static final String PREFIX = "VMP_FTP_USER_";
|
||||||
private String username;
|
|
||||||
|
|
||||||
@Value("${ftp.password}")
|
@Autowired
|
||||||
private String password;
|
private RedisTemplate<Object, Object> redisTemplate;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public User getUserByName(String username) throws FtpException {
|
public User getUserByName(String username) throws FtpException {
|
||||||
System.out.println("getUserByName");
|
return (BaseUser)redisTemplate.opsForValue().get(PREFIX + username);
|
||||||
if (!username.equals(this.username)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return getUser();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String[] getAllUserNames() throws FtpException {
|
public String[] getAllUserNames() throws FtpException {
|
||||||
String[] strings = new String[1];
|
return new String[0];
|
||||||
strings[0] = this.username;
|
|
||||||
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 this.username.equals(username);
|
return redisTemplate.opsForValue().get(PREFIX + username) != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public User authenticate(Authentication authentication) throws AuthenticationFailedException {
|
public User authenticate(Authentication authentication) throws AuthenticationFailedException {
|
||||||
UsernamePasswordAuthentication usernamePasswordAuthentication = (UsernamePasswordAuthentication) authentication;
|
UsernamePasswordAuthentication usernamePasswordAuthentication = (UsernamePasswordAuthentication) authentication;
|
||||||
if (usernamePasswordAuthentication.getUsername().equals(this.username)
|
BaseUser user = (BaseUser)redisTemplate.opsForValue().get(PREFIX + usernamePasswordAuthentication.getUsername());
|
||||||
&& usernamePasswordAuthentication.getPassword().equals(this.password)) {
|
if (user != null && usernamePasswordAuthentication.getPassword().equals(user.getPassword())) {
|
||||||
return getUser();
|
return user;
|
||||||
}
|
}
|
||||||
return null;
|
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
|
@Override
|
||||||
public String getAdminName() throws FtpException {
|
public String getAdminName() throws FtpException {
|
||||||
return this.username;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isAdmin(String username) throws FtpException {
|
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.ftpServer.FtpSetting;
|
||||||
import com.genersoft.iot.vmp.conf.UserSetting;
|
import com.genersoft.iot.vmp.conf.UserSetting;
|
||||||
import com.genersoft.iot.vmp.conf.exception.ControllerException;
|
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.gb28181.service.IGbChannelService;
|
||||||
import com.genersoft.iot.vmp.jt1078.bean.*;
|
import com.genersoft.iot.vmp.jt1078.bean.*;
|
||||||
import com.genersoft.iot.vmp.jt1078.bean.common.ConfigAttribute;
|
import com.genersoft.iot.vmp.jt1078.bean.common.ConfigAttribute;
|
||||||
@ -32,6 +33,7 @@ import com.github.pagehelper.PageHelper;
|
|||||||
import com.github.pagehelper.PageInfo;
|
import com.github.pagehelper.PageInfo;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.ObjectUtils;
|
import org.apache.commons.lang3.ObjectUtils;
|
||||||
|
import org.apache.ftpserver.usermanager.impl.BaseUser;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.event.EventListener;
|
import org.springframework.context.event.EventListener;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
@ -77,6 +79,9 @@ public class jt1078ServiceImpl implements Ijt1078Service {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private FtpSetting ftpSetting;
|
private FtpSetting ftpSetting;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserManager ftpUserManager;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private FtpFileSystemFactory fileSystemFactory;
|
private FtpFileSystemFactory fileSystemFactory;
|
||||||
|
|
||||||
@ -205,8 +210,11 @@ public class jt1078ServiceImpl implements Ijt1078Service {
|
|||||||
Assert.notNull(session, "连接不存在");
|
Assert.notNull(session, "连接不存在");
|
||||||
InetSocketAddress socketAddress = session.getLoadAddress();
|
InetSocketAddress socketAddress = session.getLoadAddress();
|
||||||
String hostName = socketAddress.getHostName();
|
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();
|
J9206 j92026 = new J9206();
|
||||||
j92026.setChannelId(channelId);
|
j92026.setChannelId(channelId);
|
||||||
@ -214,8 +222,8 @@ public class jt1078ServiceImpl implements Ijt1078Service {
|
|||||||
j92026.setEndTime(DateUtil.yyyy_MM_dd_HH_mm_ssTo1078(endTime));
|
j92026.setEndTime(DateUtil.yyyy_MM_dd_HH_mm_ssTo1078(endTime));
|
||||||
j92026.setServerIp(hostName);
|
j92026.setServerIp(hostName);
|
||||||
j92026.setPort(ftpSetting.getPort());
|
j92026.setPort(ftpSetting.getPort());
|
||||||
j92026.setUsername(ftpSetting.getUsername());
|
j92026.setUsername(randomUser.getName());
|
||||||
j92026.setPassword(ftpSetting.getPassword());
|
j92026.setPassword(randomUser.getPassword());
|
||||||
j92026.setPath(filePath);
|
j92026.setPath(filePath);
|
||||||
|
|
||||||
if (mediaType != null) {
|
if (mediaType != null) {
|
||||||
@ -733,8 +741,10 @@ public class jt1078ServiceImpl implements Ijt1078Service {
|
|||||||
InetSocketAddress socketAddress = session.getLoadAddress();
|
InetSocketAddress socketAddress = session.getLoadAddress();
|
||||||
String hostName = socketAddress.getHostName();
|
String hostName = socketAddress.getHostName();
|
||||||
|
|
||||||
log.info("[JT-录像] 下载,设备:{}, 通道: {}, 开始时间: {}, 结束时间: {} 上传IP: {} 等待上传文件路径: {} ",
|
BaseUser randomUser = ftpUserManager.getRandomUser();
|
||||||
phoneNumber, channelId, startTime, endTime, hostName, filePath);
|
|
||||||
|
log.info("[JT-录像] 下载,设备:{}, 通道: {}, 开始时间: {}, 结束时间: {} 上传IP: {} 等待上传文件路径: {} 用户名: {}, 密码: {} ",
|
||||||
|
phoneNumber, channelId, startTime, endTime, hostName, filePath, randomUser.getName(), randomUser.getPassword());
|
||||||
// 文件上传指令
|
// 文件上传指令
|
||||||
J9206 j9206 = new J9206();
|
J9206 j9206 = new J9206();
|
||||||
j9206.setChannelId(channelId);
|
j9206.setChannelId(channelId);
|
||||||
@ -742,8 +752,8 @@ public class jt1078ServiceImpl implements Ijt1078Service {
|
|||||||
j9206.setEndTime(DateUtil.yyyy_MM_dd_HH_mm_ssTo1078(endTime));
|
j9206.setEndTime(DateUtil.yyyy_MM_dd_HH_mm_ssTo1078(endTime));
|
||||||
j9206.setServerIp(hostName);
|
j9206.setServerIp(hostName);
|
||||||
j9206.setPort(ftpSetting.getPort());
|
j9206.setPort(ftpSetting.getPort());
|
||||||
j9206.setUsername(ftpSetting.getUsername());
|
j9206.setUsername(randomUser.getName());
|
||||||
j9206.setPassword(ftpSetting.getPassword());
|
j9206.setPassword(randomUser.getPassword());
|
||||||
j9206.setPath(filePath);
|
j9206.setPath(filePath);
|
||||||
|
|
||||||
if (mediaType != null) {
|
if (mediaType != null) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user