update 文档拆分时,解密文件

This commit is contained in:
lijiarao
2024-03-25 11:58:08 +08:00
parent b1383ab03a
commit 9cd53a57b1
4 changed files with 56 additions and 29 deletions
@@ -1,16 +1,15 @@
package com.jero.modules.docking.utils; package com.jero.common.util;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Value;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*; import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils; import org.springframework.util.DigestUtils;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@@ -23,9 +22,29 @@ import java.util.Objects;
* intekey解密文件相关 * intekey解密文件相关
*/ */
@Slf4j @Slf4j
@Component
public class IntekeyUtils { public class IntekeyUtils {
public static boolean ENABLE;
public static String DECRYPT_URL;
public static String DECRYPT_APP_CODE;
public static String DECRYPT_SECRET_KEY;
private static final Logger logger = LoggerFactory.getLogger(IntekeyUtils.class); @Value("${download.enable}")
public void setEnable(boolean enable) {
ENABLE = enable;
}
@Value("${download.decrypt.url}")
public void setDecryptUrl(String decryptUrl) {
DECRYPT_URL = decryptUrl;
}
@Value("${download.decrypt.app_code}")
public void setDecryptAppCode(String decryptAppCode) {
DECRYPT_APP_CODE = decryptAppCode;
}
@Value("${download.decrypt.secret_key}")
public void setDecryptSecretKey(String decryptSecretKey) {
DECRYPT_SECRET_KEY = decryptSecretKey;
}
/** /**
* 解密文件返回InputStream供excel使用 * 解密文件返回InputStream供excel使用
@@ -35,10 +54,9 @@ public class IntekeyUtils {
* @param multipartFile * @param multipartFile
* @return * @return
*/ */
//todo 这个方法需要写在laws-modules-docking 里面 可以新建一个文件夹是encrypt public static InputStream decryptFile(String url, String appCode, String secretKey, Integer scope, MultipartFile multipartFile) {
public static InputStream DecryptFile(String url, String appCode, String secretKey, Integer scope, MultipartFile multipartFile) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
logger.info("解密时间:" + format.format(new Date())); log.info("解密时间:" + format.format(new Date()));
//设置校验 //设置校验
Long time = System.currentTimeMillis(); Long time = System.currentTimeMillis();
@@ -92,6 +110,12 @@ public class IntekeyUtils {
return file; return file;
} }
public static InputStream getInputStreamByDecryptFile(MultipartFile file) {
if(ENABLE){
return decryptFile(DECRYPT_URL, DECRYPT_APP_CODE, DECRYPT_SECRET_KEY, null, file);
}
return null;
}
public static void main(String args[]) { public static void main(String args[]) {
String originalFilename = "C:\\Users\\LQT\\Desktop\\参考代码 - 副本.txt"; String originalFilename = "C:\\Users\\LQT\\Desktop\\参考代码 - 副本.txt";
@@ -1,6 +1,6 @@
package com.jero.modules.docking.download.controller; package com.jero.modules.docking.download.controller;
import com.jero.modules.docking.utils.IntekeyUtils; import com.jero.common.util.IntekeyUtils;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
@@ -28,7 +28,7 @@ public class SearchController {
@RequestParam("secretKey") String secretKey, @RequestParam("secretKey") String secretKey,
@RequestParam(value = "scope",required = false) Integer scope, @RequestParam(value = "scope",required = false) Integer scope,
HttpServletResponse response) { HttpServletResponse response) {
try (InputStream inputStream = IntekeyUtils.DecryptFile(url, appCode, secretKey, scope, file); try (InputStream inputStream = IntekeyUtils.decryptFile(url, appCode, secretKey, scope, file);
OutputStream outputStream = response.getOutputStream() OutputStream outputStream = response.getOutputStream()
) { ) {
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
@@ -1,32 +1,23 @@
package com.jero.modules.docking.download.service.impl; package com.jero.modules.docking.download.service.impl;
import com.jero.common.api.vo.ResultCommon;
import com.jero.common.exception.JeroBootException;
import com.jero.common.system.vo.LoginUser;
import com.jero.modules.docking.download.config.ContentCachingWrapperFilter; import com.jero.modules.docking.download.config.ContentCachingWrapperFilter;
import com.jero.modules.docking.download.service.DownloadDecryptFileService; import com.jero.modules.docking.download.service.DownloadDecryptFileService;
import com.jero.modules.docking.utils.IntekeyUtils; import com.jero.common.util.IntekeyUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.shiro.SecurityUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile; import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.method.HandlerMethod; import org.springframework.web.method.HandlerMethod;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.ContentCachingResponseWrapper;
import org.springframework.web.util.WebUtils;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.UUID; import java.util.UUID;
@@ -151,7 +142,7 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic
decryptFile(response, file, null); decryptFile(response, file, null);
}else if(b1){ }else if(b1){
// 下载文件 先解密,判断组织域,加密 // 下载文件 先解密,判断组织域,加密
try (InputStream is = IntekeyUtils.DecryptFile(decryptUrl, decryptAppCode, decryptSecretKey, null, file)) { try (InputStream is = IntekeyUtils.decryptFile(decryptUrl, decryptAppCode, decryptSecretKey, null, file)) {
MultipartFile fileDecrypt = getMultipartFile(response,is, contentType, fileName); MultipartFile fileDecrypt = getMultipartFile(response,is, contentType, fileName);
// 判断fileDecrypt是不是zip压缩包,如果是压缩包的话则不加密 // 判断fileDecrypt是不是zip压缩包,如果是压缩包的话则不加密
boolean isZipFile = false; boolean isZipFile = false;
@@ -159,7 +150,9 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic
if (originalFileName != null && originalFileName.endsWith(".zip")) { if (originalFileName != null && originalFileName.endsWith(".zip")) {
isZipFile = true; isZipFile = true;
} }
if (isZipFile) return; if (isZipFile) {
return;
}
// 加密 // 加密
encryptFile(response, fileDecrypt, scope); encryptFile(response, fileDecrypt, scope);
} catch (Exception e) { } catch (Exception e) {
@@ -197,7 +190,7 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic
} }
private void decryptFile(HttpServletResponse response, MultipartFile file, Integer scope) { private void decryptFile(HttpServletResponse response, MultipartFile file, Integer scope) {
try (InputStream is = IntekeyUtils.DecryptFile(decryptUrl, decryptAppCode, decryptSecretKey, scope, file)) { try (InputStream is = IntekeyUtils.decryptFile(decryptUrl, decryptAppCode, decryptSecretKey, scope, file)) {
writeResponse(response, is); writeResponse(response, is);
} catch (Exception e) { } catch (Exception e) {
log.error("Exception:",e); log.error("Exception:",e);
@@ -206,7 +199,7 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic
} }
private void encryptFile(HttpServletResponse response, MultipartFile file, Integer scope) { private void encryptFile(HttpServletResponse response, MultipartFile file, Integer scope) {
try (InputStream is = IntekeyUtils.DecryptFile(encryptUrl, encryptAppCode, encryptSecretKey, scope, file)) { try (InputStream is = IntekeyUtils.decryptFile(encryptUrl, encryptAppCode, encryptSecretKey, scope, file)) {
writeResponse(response, is); writeResponse(response, is);
} catch (Exception e) { } catch (Exception e) {
log.error("Exception:",e); log.error("Exception:",e);
@@ -6,6 +6,7 @@ import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jero.common.exception.JeroBootException; import com.jero.common.exception.JeroBootException;
import com.jero.common.system.vo.LoginUser; import com.jero.common.system.vo.LoginUser;
import com.jero.common.util.IntekeyUtils;
import com.jero.common.util.MinioUtil; import com.jero.common.util.MinioUtil;
import com.jero.common.util.UUIDUtils; import com.jero.common.util.UUIDUtils;
import com.jero.modules.laws.documenttool.mapper.DocumentSplitMapper; import com.jero.modules.laws.documenttool.mapper.DocumentSplitMapper;
@@ -26,6 +27,7 @@ import com.jero.modules.split.vo.TitleNumberVO;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.entity.ContentType;
import org.apache.poi.hwpf.usermodel.PictureType; import org.apache.poi.hwpf.usermodel.PictureType;
import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.util.ZipSecureFile; import org.apache.poi.openxml4j.util.ZipSecureFile;
@@ -45,13 +47,16 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.NamedNodeMap; import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import javax.annotation.Resource;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.*; import java.util.*;
@@ -161,7 +166,7 @@ public class FileSpiltService {
// } // }
// 获取文件地址 // 获取文件地址
String fileId = sarFileSplitInfoEO.getFileId(); String fileId = sarFileSplitInfoEO.getFileId();
LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper(); LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(OSSFile::getId, fileId); queryWrapper.eq(OSSFile::getId, fileId);
queryWrapper.orderByDesc(OSSFile::getCreateTime); queryWrapper.orderByDesc(OSSFile::getCreateTime);
List<OSSFile> ossFileList = iossFileService.list(queryWrapper); List<OSSFile> ossFileList = iossFileService.list(queryWrapper);
@@ -174,14 +179,19 @@ public class FileSpiltService {
throw new JeroBootException("当前文件未找到,请重新选择!"); throw new JeroBootException("当前文件未找到,请重新选择!");
} }
try (InputStream is = MinioUtil.download(readFilePath);InputStream is1 = MinioUtil.download(readFilePath)) { try (InputStream is = MinioUtil.download(readFilePath);InputStream is1 = MinioUtil.download(readFilePath)) {
//String readFilePath = "E:\\Data\\Downloads\\wordtest\\GB7258标准修订test - 表格测试 - 副本.docx"; String fileName = readFilePath.substring((readFilePath.lastIndexOf("/") + 1));
//try (InputStream is = new FileInputStream(readFilePath); InputStream is1 = new FileInputStream(readFilePath)) {//需要将文件路更改为word文档所在路径。 MultipartFile mFile = new MockMultipartFile(fileName, fileName, ContentType.APPLICATION_OCTET_STREAM.toString(), is);
MultipartFile mFile1 = new MockMultipartFile(fileName, fileName, ContentType.APPLICATION_OCTET_STREAM.toString(), is1);
InputStream inputStream = IntekeyUtils.getInputStreamByDecryptFile(mFile);
InputStream inputStream1 = IntekeyUtils.getInputStreamByDecryptFile(mFile1);
inputStream = inputStream == null ? is : inputStream;
inputStream1 = inputStream1 == null ? is1 : inputStream1;
ZipSecureFile.setMinInflateRatio(-1.0d); ZipSecureFile.setMinInflateRatio(-1.0d);
XWPFDocument doc = new XWPFDocument(is); XWPFDocument doc = new XWPFDocument(inputStream);
// 获取文档中的图片 // 获取文档中的图片
List<XWPFPictureData> allPictures = doc.getAllPictures(); List<XWPFPictureData> allPictures = doc.getAllPictures();
//获得序号标题和无标题序号 //获得序号标题和无标题序号
TitleNumberVO titleNumberVO = AsposeUtil.getNumber(is1); TitleNumberVO titleNumberVO = AsposeUtil.getNumber(inputStream1);
//TitleNumberVO titleNumberVO = new TitleNumberVO(); //TitleNumberVO titleNumberVO = new TitleNumberVO();
Map<String, String> titleNumberMap = titleNumberVO.getTitleNumberMap(); Map<String, String> titleNumberMap = titleNumberVO.getTitleNumberMap();
List<String> emptyTitleList = titleNumberVO.getEmptyTitleList(); List<String> emptyTitleList = titleNumberVO.getEmptyTitleList();