文件安全相关移动到配置文件。

This commit is contained in:
chenxudong 2025-08-06 16:54:25 +08:00
parent 4a4260d261
commit 34c39b6648
8 changed files with 27 additions and 14 deletions

View File

@ -7,6 +7,8 @@ import com.electromagnetic.industry.software.backup.pojo.BackupPro;
import com.electromagnetic.industry.software.backup.service.FileService;
import com.electromagnetic.industry.software.common.util.EleCommonUtil;
import jakarta.annotation.Resource;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.MediaType;
@ -17,14 +19,16 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import static com.electromagnetic.industry.software.common.cons.ElectromagneticConstants.FILE_SEC_PASSWD;
@Service
public class FileServiceImpl implements FileService {
@Resource
private BackupPro backupPro;
@Getter
@Value("${file.enc.passwd}")
private String fileEncPasswd;
@Override
public void upload(MultipartFile file) throws IOException {
String name = FileUtil.mainName(file.getOriginalFilename());
@ -60,7 +64,7 @@ public class FileServiceImpl implements FileService {
int index = destPath.lastIndexOf(".");
String zipPath = destPath.substring(0, index) + ".zip";
ZipUtil.zip(destPath, zipPath);
EleCommonUtil.encryptFile(zipPath, SecureUtil.aes(FILE_SEC_PASSWD.getBytes()));
EleCommonUtil.encryptFile(zipPath, SecureUtil.aes(fileEncPasswd.getBytes()));
FileUtil.del(destPath);
}

View File

@ -12,3 +12,7 @@ spring:
server:
port: 12491
file:
enc:
passwd: adknfhkj87654knd

View File

@ -92,6 +92,10 @@ public class ElePropertyConfig {
@Value("${ai.file.upload.store.dir}")
private String aiFileUploadStoreDir;
@Getter
@Value("${file.enc.passwd}")
private String fileEncPasswd;
public String getAiFileUploadStoreDir() {
if (EleCommonUtil.isWinOs()) {
return FileUtil.normalize(winPrefix + File.separator + aiFileUploadStoreDir);
@ -213,4 +217,5 @@ public class ElePropertyConfig {
}
}

View File

@ -663,7 +663,7 @@ public class CommonService {
edFileInfoMapper.insertOrUpdate(newEdFileInfo);
String fileDestPath = getFileSysPath(newEdFileInfo.getId());
FileUtil.writeFromStream(file.getInputStream(), fileDestPath);
EleCommonUtil.encryptFile(fileDestPath, SecureUtil.aes(FILE_SEC_PASSWD.getBytes()));
EleCommonUtil.encryptFile(fileDestPath, SecureUtil.aes(elePropertyConfig.getFileEncPasswd().getBytes()));
finalEdFileInfo = newEdFileInfo;
newEdFileInfo.setSaveStatus(EleDataSaveStatusEnum.SUCCESS.code);
edFileInfoMapper.insertOrUpdate(newEdFileInfo);
@ -765,7 +765,7 @@ public class CommonService {
edFileInfoMapper.insert(newEdFileInfo);
String fileDestPath = getFileSysPath(newEdFileInfo.getId());
fileSystemService.save(file.getInputStream(), fileDestPath);
EleCommonUtil.encryptFile(fileDestPath, SecureUtil.aes(FILE_SEC_PASSWD.getBytes()));
EleCommonUtil.encryptFile(fileDestPath, SecureUtil.aes(elePropertyConfig.getFileEncPasswd().getBytes()));
newEdFileInfo.setSaveStatus(EleDataSaveStatusEnum.SUCCESS.code);
edFileInfoMapper.insertOrUpdate(newEdFileInfo);
return newEdFileInfo;

View File

@ -482,7 +482,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
String mainName = FileUtil.mainName(destColibPath);
String parentDir = FileUtil.getParent(destColibPath, 1);
String zipDirPath = parentDir + File.separator + mainName + ".zip";
AES aes = SecureUtil.aes(FILE_SEC_PASSWD.getBytes());
AES aes = SecureUtil.aes(elePropertyConfig.getFileEncPasswd().getBytes());
try (
InputStream inputStream = Files.newInputStream(Paths.get(destColibPath));
OutputStream outputStream = Files.newOutputStream(Paths.get(zipDirPath))
@ -820,7 +820,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
String exportColibFile = userDownloadDataDir + File.separator + File.separator + EXPORT_PRJ_NAME + "_" + nowTimeStr + EXPORT_FILE_SUFFIX;
FileUtil.del(exportColibFile);
ZipUtil.zip(prjDirPath, exportZipFile);
AES aes = SecureUtil.aes(FILE_SEC_PASSWD.getBytes());
AES aes = SecureUtil.aes(elePropertyConfig.getFileEncPasswd().getBytes());
try (
InputStream inputStream = Files.newInputStream(Paths.get(exportZipFile));
OutputStream outputStream = Files.newOutputStream(Paths.get(exportColibFile))
@ -1320,7 +1320,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
String fileSysPath = commonService.getFileSysPath(fileInfo.getId());
fileSaveTmpPath = elePropertyConfig.getEleTmpPath() + File.separator + IdUtil.fastSimpleUUID() + "." + fileInfo.getFileType();
FileUtil.copy(fileSysPath, fileSaveTmpPath, true);
EleCommonUtil.decryptFile(fileSaveTmpPath, SecureUtil.aes(FILE_SEC_PASSWD.getBytes()));
EleCommonUtil.decryptFile(fileSaveTmpPath, SecureUtil.aes(elePropertyConfig.getFileEncPasswd().getBytes()));
if (Arrays.asList("doc", "docx").contains(fileInfo.getFileType())) {
String pdfTmpPath = elePropertyConfig.getEleTmpPath() + File.separator + fileInfo.getId() + ".pdf";

View File

@ -127,12 +127,12 @@ public class ImportPrjServiceImpl extends ServiceImpl<ImportPrjInfoMapper, Impor
InputStream inputStream = zipFile.getInputStream(tmp);
String destPath = elePropertyConfig.getImportPrjPath() + File.separator + newId;
FileUtil.writeFromStream(inputStream, destPath);
EleCommonUtil.encryptFile(destPath, SecureUtil.aes(FILE_SEC_PASSWD.getBytes()));
EleCommonUtil.encryptFile(destPath, SecureUtil.aes(elePropertyConfig.getFileEncPasswd().getBytes()));
}
String prjId = items.stream().filter(e -> e.getParentId().equals(PRJ_PARENT_ID)).findFirst().get().getId();
String prjPath = elePropertyConfig.getImportPrjPath() + File.separator + prjId;
FileUtil.writeFromStream(file.getInputStream(), prjPath);
EleCommonUtil.encryptFile(prjPath, SecureUtil.aes(FILE_SEC_PASSWD.getBytes()));
EleCommonUtil.encryptFile(prjPath, SecureUtil.aes(elePropertyConfig.getFileEncPasswd().getBytes()));
UserThreadLocal.setSuccessInfo("", prjId, "导入本地工程成功");
} catch (Exception e) {
throw new BizException("导入工程失败,原因 " + e.getMessage(), e);

View File

@ -62,3 +62,5 @@ backup.remote.host=127.0.0.1
backup.remote.port=1111
backup.mysql.path=/workspace/mysqlbak/test
backup.mysql.script.path=/workspace/mysqlbak/back_dev.sh
file.enc.passwd=123456

View File

@ -14,8 +14,6 @@ public interface ElectromagneticConstants {
String NAME_VALID_MSG = "不符合规范只能包含中文字符、下划线、连字符、加号、数字和英文字符且长度小于32。";
String FILE_SEC_PASSWD = "adknfhkj87654knd";
String ED_FILE_FAVORITE = "ed_file_favorite";
String ED_FILE_RELATION = "ed_file_relation";