feat: 企标带文件导出PDF文件先解密后加水印再加密

This commit is contained in:
2024-05-11 14:21:26 +08:00
parent a985191bdc
commit aad080b900
4 changed files with 68 additions and 7 deletions
@@ -29,6 +29,10 @@ public class IntekeyUtils {
public static String DECRYPT_URL; public static String DECRYPT_URL;
public static String DECRYPT_APP_CODE; public static String DECRYPT_APP_CODE;
public static String DECRYPT_SECRET_KEY; public static String DECRYPT_SECRET_KEY;
public static Integer SCOPE_DEV;
public static String ENCRYPT_URL;
public static String ENCRYPT_APP_CODE;
public static String ENCRYPT_SECRET_KEY;
@Value("${download.enable}") @Value("${download.enable}")
public void setEnable(boolean enable) { public void setEnable(boolean enable) {
@@ -50,6 +54,23 @@ public class IntekeyUtils {
DECRYPT_SECRET_KEY = decryptSecretKey; DECRYPT_SECRET_KEY = decryptSecretKey;
} }
@Value("${download.scope_dev}")
public void setScopeDev(Integer scopeDev) {
SCOPE_DEV = scopeDev;
}
@Value("#{'${download.encrypt.url}'}")
public void setEncryptUrl(String encryptUrl) {
ENCRYPT_URL = encryptUrl;
}
@Value("#{'${download.encrypt.app_code}'}")
public void setEncryptAppCode(String encryptAppCode) {
ENCRYPT_APP_CODE = encryptAppCode;
}
@Value("#{'${download.encrypt.secret_key}'}")
public void setEncryptSecretKey(String encryptSecretKey) {
ENCRYPT_SECRET_KEY = encryptSecretKey;
}
public static MultipartFile autoDecryptFile(MultipartFile file) { public static MultipartFile autoDecryptFile(MultipartFile file) {
if (!ENABLE || file == null) { if (!ENABLE || file == null) {
return file; return file;
@@ -84,6 +105,11 @@ public class IntekeyUtils {
} }
} }
public static InputStream autoEncryptInputStreamFile(InputStream is, String fileName) throws IOException {
MultipartFile file = IntekeyUtils.convertMultipart(is, fileName);
return IntekeyUtils.decryptFile(ENCRYPT_URL, ENCRYPT_APP_CODE, ENCRYPT_SECRET_KEY, SCOPE_DEV, file);
}
public static MultipartFile convertMultipart(InputStream is, String fileName) throws IOException { public static MultipartFile convertMultipart(InputStream is, String fileName) throws IOException {
// 假定文件名和内容类型已知 // 假定文件名和内容类型已知
String contentType = "application/octet-stream"; // 或其他类型 String contentType = "application/octet-stream"; // 或其他类型
@@ -2,6 +2,8 @@ package com.jero.modules.system.util;
import com.jero.common.system.vo.LoginUser; import com.jero.common.system.vo.LoginUser;
import com.jero.common.util.DateUtils; import com.jero.common.util.DateUtils;
import com.jero.common.util.IntekeyUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.PDPageContentStream;
@@ -13,14 +15,10 @@ import org.apache.pdfbox.util.Matrix;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.awt.*; import java.awt.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
@@ -30,6 +28,7 @@ import java.util.Date;
* @author CaiHaohan * @author CaiHaohan
*/ */
@Component @Component
@Slf4j
public class SysWaterMarkUtil { public class SysWaterMarkUtil {
@Value("${watermark.font-size}") @Value("${watermark.font-size}")
@@ -63,6 +62,8 @@ public class SysWaterMarkUtil {
@Value("${watermark.margin.top}") @Value("${watermark.margin.top}")
private float marginTop; private float marginTop;
@Value("#{'${download.enable}'}")
private boolean enable;
public List<String> getWaterMarkConfig() { public List<String> getWaterMarkConfig() {
LoginUser loginUser = UserUtils.getLoginUser(); LoginUser loginUser = UserUtils.getLoginUser();
@@ -79,6 +80,9 @@ public class SysWaterMarkUtil {
public synchronized InputStream addWatermarkToPdf(InputStream inputStream) throws IOException { public synchronized InputStream addWatermarkToPdf(InputStream inputStream) throws IOException {
List<String> watermarkText = getWaterMarkConfig(); List<String> watermarkText = getWaterMarkConfig();
if (enable) {
inputStream = IntekeyUtils.autoDecryptInputStreamFile(inputStream, "test.pdf");
}
PDDocument document = PDDocument.load(inputStream); PDDocument document = PDDocument.load(inputStream);
document.setAllSecurityToBeRemoved(true); document.setAllSecurityToBeRemoved(true);
// 加载水印字体 // 加载水印字体
@@ -182,6 +182,14 @@ public interface ILawsCommonService {
*/ */
byte[] getMinioFileContent(OSSFile file) throws Exception; byte[] getMinioFileContent(OSSFile file) throws Exception;
/**
* 获取文件内容(企标专用 特殊处理了加解密)
* @param file
* @return
* @throws Exception
*/
byte[] getMinioFileContentES(OSSFile file) throws Exception;
/** /**
* 企标的模板下载是压缩包,里面有两个Excel模板 * 企标的模板下载是压缩包,里面有两个Excel模板
* @param response * @param response
@@ -223,6 +223,9 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
@Value("#{'${enterpriseDetailRoleCode}'.split(',')}") @Value("#{'${enterpriseDetailRoleCode}'.split(',')}")
private List<String> enterpriseDetailRoleCodeList; private List<String> enterpriseDetailRoleCodeList;
@Value("#{'${download.enable}'}")
private boolean enable;
/** /**
* @Author: liao * @Author: liao
* @Date: 2023/8/23 16:57 * @Date: 2023/8/23 16:57
@@ -1613,7 +1616,11 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
ZipEntry fileEntry = new ZipEntry(file.getFileRelativePath()); ZipEntry fileEntry = new ZipEntry(file.getFileRelativePath());
zipOut.putNextEntry(fileEntry); zipOut.putNextEntry(fileEntry);
// 将文件取出存入流中 // 将文件取出存入流中
bytes = this.getMinioFileContent(file); if(TableNameEnum.ENTERPRISE_STANDARDS.getValue().equals(parameterMap.get(FieldCommon.MODULE))) {
bytes = this.getMinioFileContentES(file);
} else {
bytes = this.getMinioFileContent(file);
}
zipOut.write(bytes, 0, bytes.length); zipOut.write(bytes, 0, bytes.length);
zipOut.closeEntry(); zipOut.closeEntry();
} }
@@ -2558,6 +2565,10 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
@Override @Override
public byte[] getMinioFileContent(OSSFile file) throws Exception { public byte[] getMinioFileContent(OSSFile file) throws Exception {
return this.getMinioFileContentNormal(file, false);
}
private byte[] getMinioFileContentNormal(OSSFile file, boolean esSpecial) throws Exception {
String fileUrl = file.getUrl(); String fileUrl = file.getUrl();
String fileName = file.getFileName(); String fileName = file.getFileName();
String minioUrl = MinioUtil.getMinioUrl(); String minioUrl = MinioUtil.getMinioUrl();
@@ -2568,14 +2579,21 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
int len; int len;
if ("pdf".equalsIgnoreCase(FileUtil.extName(fileName))) { if ("pdf".equalsIgnoreCase(FileUtil.extName(fileName))) {
// 如果是PDF文件, 添加水印 // 如果是PDF文件, 添加水印
try (InputStream originalInputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), url); try (InputStream originalInputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), url);
InputStream watermarkedInputStream = sysWaterMarkUtil.addWatermarkToPdf(originalInputStream)) { InputStream watermarkedInputStream = sysWaterMarkUtil.addWatermarkToPdf(originalInputStream)) {
// 如果是系统开启了加密并且处理的是企标 则需要将加水印的pdf文件再次加密
InputStream is = watermarkedInputStream;
if (esSpecial && enable) {
// 加密企标的PDF
is = IntekeyUtils.autoEncryptInputStreamFile(is, fileName);
}
if (originalInputStream == null) { if (originalInputStream == null) {
return null; return null;
} }
while ((len = watermarkedInputStream.read(buf)) > 0) { while ((len = is.read(buf)) > 0) {
outputStream.write(buf, 0, len); outputStream.write(buf, 0, len);
} }
} catch (Exception e) { } catch (Exception e) {
@@ -2600,6 +2618,11 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
} }
} }
@Override
public byte[] getMinioFileContentES(OSSFile file) throws Exception {
return this.getMinioFileContentNormal(file, true);
}
@Override @Override
public void esTemplateDownload(HttpServletResponse response) throws Exception { public void esTemplateDownload(HttpServletResponse response) throws Exception {
// 1. 生成企标的模板 // 1. 生成企标的模板