From 9abc636637f176ca86343af64600618423d9d7cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=81=E7=90=A6=E6=B6=9B?= Date: Mon, 27 Nov 2023 17:13:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8B=E8=BD=BD=E5=8A=A0?= =?UTF-8?q?=E8=A7=A3=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../download/dto/MultipartFileDto.java | 117 ------------------ .../impl/DownloadDecryptFileServiceImpl.java | 67 ++++++---- .../src/main/resources/application-dev.yml | 1 + 3 files changed, 41 insertions(+), 144 deletions(-) delete mode 100644 laws-modules-docking/src/main/java/com/jero/modules/docking/download/dto/MultipartFileDto.java diff --git a/laws-modules-docking/src/main/java/com/jero/modules/docking/download/dto/MultipartFileDto.java b/laws-modules-docking/src/main/java/com/jero/modules/docking/download/dto/MultipartFileDto.java deleted file mode 100644 index fc14023f..00000000 --- a/laws-modules-docking/src/main/java/com/jero/modules/docking/download/dto/MultipartFileDto.java +++ /dev/null @@ -1,117 +0,0 @@ -package com.jero.modules.docking.download.dto; - -import org.springframework.util.FileCopyUtils; -import org.springframework.web.multipart.MultipartFile; - -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; - -/** - * @author lqt - * @version 1.0 - * @date 2023/11/27 11:12 - */ - -public class MultipartFileDto implements MultipartFile { - private final String name; - - private String originalFilename; - - private String contentType; - - private final byte[] content ; - - /** - * Create a new MultipartFileDto with the given content. - * - * @param name the name of the file - * @param content the content of the file - */ - public MultipartFileDto(String name, byte[] content) { - this(name, "", null, content); - } - - /** - * Create a new MultipartFileDto with the given content. - * - * @param name the name of the file - * @param contentStream the content of the file as stream - * @throws IOException if reading from the stream failed - */ - public MultipartFileDto(String name, InputStream contentStream) throws IOException { - this(name, "", null, FileCopyUtils.copyToByteArray(contentStream)); - } - - /** - * Create a new MultipartFileDto with the given content. - * - * @param name the name of the file - * @param originalFilename the original filename (as on the client's machine) - * @param contentType the content type (if known) - * @param content the content of the file - */ - public MultipartFileDto(String name, String originalFilename, String contentType, byte[] content) { - this.name = name; - this.originalFilename = (originalFilename != null ? originalFilename : ""); - this.contentType = contentType; - this.content = (content != null ? content : new byte[0]); - } - - /** - * Create a new MultipartFileDto with the given content. - * - * @param name the name of the file - * @param originalFilename the original filename (as on the client's machine) - * @param contentType the content type (if known) - * @param contentStream the content of the file as stream - * @throws IOException if reading from the stream failed - */ - public MultipartFileDto(String name, String originalFilename, String contentType, InputStream contentStream) - throws IOException { - - this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream)); - } - - @Override - public String getName() { - return this.name; - } - - @Override - public String getOriginalFilename() { - return this.originalFilename; - } - - @Override - public String getContentType() { - return this.contentType; - } - - @Override - public boolean isEmpty() { - return (this.content.length == 0); - } - - @Override - public long getSize() { - return this.content.length; - } - - @Override - public byte[] getBytes() throws IOException { - return this.content; - } - - @Override - public InputStream getInputStream() throws IOException { - return new ByteArrayInputStream(this.content); - } - - @Override - public void transferTo(File dest) throws IOException, IllegalStateException { - FileCopyUtils.copy(this.content, dest); - - } -} diff --git a/laws-modules-docking/src/main/java/com/jero/modules/docking/download/service/impl/DownloadDecryptFileServiceImpl.java b/laws-modules-docking/src/main/java/com/jero/modules/docking/download/service/impl/DownloadDecryptFileServiceImpl.java index 5e7cb2c4..bd37a5c9 100644 --- a/laws-modules-docking/src/main/java/com/jero/modules/docking/download/service/impl/DownloadDecryptFileServiceImpl.java +++ b/laws-modules-docking/src/main/java/com/jero/modules/docking/download/service/impl/DownloadDecryptFileServiceImpl.java @@ -3,13 +3,10 @@ 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.dto.MultipartFileDto; import com.jero.modules.docking.download.service.DownloadDecryptFileService; import com.jero.modules.docking.utils.IntekeyUtils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.http.entity.ContentType; import org.apache.shiro.SecurityUtils; @@ -24,12 +21,9 @@ import org.springframework.web.util.WebUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.net.URLEncoder; -import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.List; import java.util.Objects; @@ -50,6 +44,9 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic @Value("#{'${download.view_url}'.split(',')}") private List viewUrl; + @Value("#{'${download.enable}'}") + private boolean enable; + /** * 管理员角色 */ @@ -90,6 +87,9 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic @Override public void downloadDecryptFile(HttpServletRequest request, HttpServletResponse response, Object handler,String type) { + if(!enable){ + return; + } // 判断返回值是否为文件流,否则其他接口返回值会变更成字符串,导致前端无法解析 HandlerMethod handlerMethod = (HandlerMethod) handler; InputStream responseBody = null; @@ -109,7 +109,11 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic e.printStackTrace(); throw new JeroBootException(ResultCommon.ERROR); } - MultipartFile file = getMultipartFile(responseBody, response); + + String contentType = response.getContentType(); + String fileName = getFileName(response); + + MultipartFile file = getMultipartFile(responseBody, contentType, fileName); // 访问路径 String str = request.getRequestURI(); @@ -122,10 +126,15 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic decryptFile(response, file, null); }else if(b1){ // 下载文件 先解密,判断组织域,加密 - decryptFile(response, file, null); - int scope = getScope(); - // 加密 - encryptFile(response, file, scope); + try (InputStream is = IntekeyUtils.DecryptFile(decryptUrl, decryptAppCode, decryptSecretKey, null, file)) { + MultipartFile fileDecrypt = getMultipartFile(is, contentType, fileName); + int scope = getScope(); + // 加密 + encryptFile(response, fileDecrypt, scope); + } catch (Exception e) { + throw new JeroBootException(ResultCommon.ERROR); + } + }else{ // excel 判断组织域,加密 int scope = getScope(); @@ -134,6 +143,25 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic } } + @NotNull + private String getFileName(HttpServletResponse response) { + String fileName = ""; + String headerField = response.getHeader("Content-Disposition"); + + if (!StringUtils.isBlank(headerField) || headerField.contains("fileName=") || headerField.contains("filename=")){ + String name = ""; + if(headerField.contains("fileName=")){ + name = "fileName"; + }else{ + name = "filename"; + } + fileName = headerField.substring(headerField.lastIndexOf(name + "=") + 9); + }else { + fileName = UUID.randomUUID().toString(); + } + return fileName; + } + private void decryptFile(HttpServletResponse response, MultipartFile file, Integer scope) { try (InputStream is = IntekeyUtils.DecryptFile(decryptUrl, decryptAppCode, decryptSecretKey, scope, file)) { writeResponse(response, is); @@ -151,24 +179,9 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic } @NotNull - private MultipartFile getMultipartFile(InputStream responseBody,HttpServletResponse response) { + private MultipartFile getMultipartFile(InputStream responseBody,String contentType,String fileName) { MultipartFile file; try { - String contentType = response.getContentType(); - String fileName = ""; - String headerField = response.getHeader("Content-Disposition"); - - if (!StringUtils.isBlank(headerField) || headerField.contains("fileName=") || headerField.contains("filename=")){ - String name = ""; - if(headerField.contains("fileName=")){ - name = "fileName"; - }else{ - name = "filename"; - } - fileName = headerField.substring(headerField.lastIndexOf(name + "=") + 9); - }else { - fileName = UUID.randomUUID().toString(); - } file = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(),fileName,contentType, responseBody); } catch (IOException e) { e.printStackTrace(); diff --git a/laws-single-startup/src/main/resources/application-dev.yml b/laws-single-startup/src/main/resources/application-dev.yml index 591a83eb..db9ea9b7 100644 --- a/laws-single-startup/src/main/resources/application-dev.yml +++ b/laws-single-startup/src/main/resources/application-dev.yml @@ -432,6 +432,7 @@ adminRoleCode: admin # 文件下载加解密 download: + enable: true # 文件下载、预览拦截路径,多个以逗号分隔 download_url: /sys/common/download/ view_url: /sys/common/view/