Forráskód Böngészése

扫描仪FTP增加IP地址校验。

wukai 2 éve
szülő
commit
33a3a4968d

+ 21 - 1
doc-biz/src/main/java/com/doc/ftp/DocFtpLet.java

@@ -1,23 +1,43 @@
 package com.doc.ftp;
 
+import cn.hutool.extra.spring.SpringUtil;
 import com.doc.common.constant.Constants;
+import com.doc.scanner.service.IScannerInfoService;
 import org.apache.ftpserver.ftplet.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
 
 import java.io.IOException;
+import java.net.InetSocketAddress;
 import java.nio.file.Files;
 import java.nio.file.Paths;
+import java.util.List;
 
 /**
  * 自定义事件
  *
  * @author wukai
  */
+@Component
 public class DocFtpLet extends DefaultFtplet {
 
     public static final Logger log = LoggerFactory.getLogger(DocFtpLet.class);
 
+    private IScannerInfoService infoService = SpringUtil.getBean(IScannerInfoService.class);
+
+    @Override
+    public FtpletResult onConnect(FtpSession session) throws FtpException, IOException {
+        List<String> ipList = infoService.selectIpList();
+        InetSocketAddress client = session.getClientAddress();
+        String ip = client.getAddress().getHostAddress();
+        if (!ipList.contains(ip)) {
+            //只允许扫描仪列表中配置的IP访问
+            throw new FtpException();
+        }
+        return super.onConnect(session);
+    }
+
     /**
      * 开始上传
      * Override this method to intercept uploads
@@ -31,7 +51,7 @@ public class DocFtpLet extends DefaultFtplet {
     @Override
     public FtpletResult onUploadStart(FtpSession session, FtpRequest request)
             throws FtpException, IOException {
-        //获取上传文件的上传路径 
+        //获取上传文件的上传路径
         String path = session.getUser().getHomeDirectory();
         try {
             //自动创建上传路径

+ 1 - 1
doc-biz/src/main/java/com/doc/ftp/DocFtpServer.java

@@ -78,7 +78,7 @@ public class DocFtpServer {
             Listener listener = listenerFactory.createListener();
             serverFactory.addListener("default", listener);
             //5、配置自定义用户事件
-            Map<String, Ftplet> ftpLets = new HashMap();
+            Map<String, Ftplet> ftpLets = new HashMap(16);
             ftpLets.put("ftpService", new DocFtpLet());
             serverFactory.setFtplets(ftpLets);
             //6、读取用户的配置信息

+ 17 - 10
doc-biz/src/main/java/com/doc/scanner/service/IScannerInfoService.java

@@ -1,19 +1,19 @@
 package com.doc.scanner.service;
 
-import java.util.List;
 import com.doc.scanner.domain.ScannerInfo;
 
+import java.util.List;
+
 /**
  * 扫描仪管理Service接口
- * 
+ *
  * @author wukai
  * @date 2023-11-28
  */
-public interface IScannerInfoService 
-{
+public interface IScannerInfoService {
     /**
      * 查询扫描仪管理
-     * 
+     *
      * @param scannerId 扫描仪管理主键
      * @return 扫描仪管理
      */
@@ -21,7 +21,7 @@ public interface IScannerInfoService
 
     /**
      * 查询扫描仪管理列表
-     * 
+     *
      * @param scannerInfo 扫描仪管理
      * @return 扫描仪管理集合
      */
@@ -29,7 +29,7 @@ public interface IScannerInfoService
 
     /**
      * 新增扫描仪管理
-     * 
+     *
      * @param scannerInfo 扫描仪管理
      * @return 结果
      */
@@ -37,7 +37,7 @@ public interface IScannerInfoService
 
     /**
      * 修改扫描仪管理
-     * 
+     *
      * @param scannerInfo 扫描仪管理
      * @return 结果
      */
@@ -45,7 +45,7 @@ public interface IScannerInfoService
 
     /**
      * 批量删除扫描仪管理
-     * 
+     *
      * @param scannerIds 需要删除的扫描仪管理主键集合
      * @return 结果
      */
@@ -53,9 +53,16 @@ public interface IScannerInfoService
 
     /**
      * 删除扫描仪管理信息
-     * 
+     *
      * @param scannerId 扫描仪管理主键
      * @return 结果
      */
     public int deleteScannerInfoByScannerId(Long scannerId);
+
+    /**
+     * 查询所有扫描仪IP列表
+     *
+     * @return IP列表
+     */
+    List<String> selectIpList();
 }

+ 21 - 7
doc-biz/src/main/java/com/doc/scanner/service/impl/ScannerInfoServiceImpl.java

@@ -1,12 +1,14 @@
 package com.doc.scanner.service.impl;
 
-import java.util.List;
-        import com.doc.common.utils.DateUtils;
-import org.springframework.stereotype.Service;
-import com.doc.scanner.mapper.ScannerInfoMapper;
+import com.doc.common.utils.DateUtils;
 import com.doc.scanner.domain.ScannerInfo;
+import com.doc.scanner.mapper.ScannerInfoMapper;
 import com.doc.scanner.service.IScannerInfoService;
+import org.springframework.stereotype.Service;
+
 import javax.annotation.Resource;
+import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * 扫描仪管理Service业务层处理
@@ -49,8 +51,8 @@ public class ScannerInfoServiceImpl implements IScannerInfoService {
      */
     @Override
     public int insertScannerInfo(ScannerInfo scannerInfo) {
-                scannerInfo.setCreateTime(DateUtils.getNowDate());
-            return scannerInfoMapper.insertScannerInfo(scannerInfo);
+        scannerInfo.setCreateTime(DateUtils.getNowDate());
+        return scannerInfoMapper.insertScannerInfo(scannerInfo);
     }
 
     /**
@@ -61,7 +63,7 @@ public class ScannerInfoServiceImpl implements IScannerInfoService {
      */
     @Override
     public int updateScannerInfo(ScannerInfo scannerInfo) {
-                scannerInfo.setUpdateTime(DateUtils.getNowDate());
+        scannerInfo.setUpdateTime(DateUtils.getNowDate());
         return scannerInfoMapper.updateScannerInfo(scannerInfo);
     }
 
@@ -86,4 +88,16 @@ public class ScannerInfoServiceImpl implements IScannerInfoService {
     public int deleteScannerInfoByScannerId(Long scannerId) {
         return scannerInfoMapper.deleteScannerInfoByScannerId(scannerId);
     }
+
+    /**
+     * 查询所有扫描仪IP列表
+     *
+     * @return IP列表
+     */
+    @Override
+    public List<String> selectIpList() {
+        List<ScannerInfo> list = selectScannerInfoList(new ScannerInfo());
+        List<String> ipList = list.stream().map(ScannerInfo::getScannerIp).collect(Collectors.toList());
+        return ipList;
+    }
 }