文件下载加解密
This commit is contained in:
-117
@@ -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);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+40
-27
@@ -3,13 +3,10 @@ package com.jero.modules.docking.download.service.impl;
|
|||||||
import com.jero.common.api.vo.ResultCommon;
|
import com.jero.common.api.vo.ResultCommon;
|
||||||
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.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.download.service.DownloadDecryptFileService;
|
||||||
import com.jero.modules.docking.utils.IntekeyUtils;
|
import com.jero.modules.docking.utils.IntekeyUtils;
|
||||||
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.io.IOUtils;
|
|
||||||
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.apache.shiro.SecurityUtils;
|
||||||
@@ -24,12 +21,9 @@ 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.ByteArrayInputStream;
|
|
||||||
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.net.URLEncoder;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@@ -50,6 +44,9 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic
|
|||||||
@Value("#{'${download.view_url}'.split(',')}")
|
@Value("#{'${download.view_url}'.split(',')}")
|
||||||
private List<String> viewUrl;
|
private List<String> viewUrl;
|
||||||
|
|
||||||
|
@Value("#{'${download.enable}'}")
|
||||||
|
private boolean enable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 管理员角色
|
* 管理员角色
|
||||||
*/
|
*/
|
||||||
@@ -90,6 +87,9 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void downloadDecryptFile(HttpServletRequest request, HttpServletResponse response, Object handler,String type) {
|
public void downloadDecryptFile(HttpServletRequest request, HttpServletResponse response, Object handler,String type) {
|
||||||
|
if(!enable){
|
||||||
|
return;
|
||||||
|
}
|
||||||
// 判断返回值是否为文件流,否则其他接口返回值会变更成字符串,导致前端无法解析
|
// 判断返回值是否为文件流,否则其他接口返回值会变更成字符串,导致前端无法解析
|
||||||
HandlerMethod handlerMethod = (HandlerMethod) handler;
|
HandlerMethod handlerMethod = (HandlerMethod) handler;
|
||||||
InputStream responseBody = null;
|
InputStream responseBody = null;
|
||||||
@@ -109,7 +109,11 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
throw new JeroBootException(ResultCommon.ERROR);
|
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();
|
String str = request.getRequestURI();
|
||||||
|
|
||||||
@@ -122,10 +126,15 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic
|
|||||||
decryptFile(response, file, null);
|
decryptFile(response, file, null);
|
||||||
}else if(b1){
|
}else if(b1){
|
||||||
// 下载文件 先解密,判断组织域,加密
|
// 下载文件 先解密,判断组织域,加密
|
||||||
decryptFile(response, file, null);
|
try (InputStream is = IntekeyUtils.DecryptFile(decryptUrl, decryptAppCode, decryptSecretKey, null, file)) {
|
||||||
int scope = getScope();
|
MultipartFile fileDecrypt = getMultipartFile(is, contentType, fileName);
|
||||||
// 加密
|
int scope = getScope();
|
||||||
encryptFile(response, file, scope);
|
// 加密
|
||||||
|
encryptFile(response, fileDecrypt, scope);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new JeroBootException(ResultCommon.ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
}else{
|
}else{
|
||||||
// excel 判断组织域,加密
|
// excel 判断组织域,加密
|
||||||
int scope = getScope();
|
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) {
|
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);
|
||||||
@@ -151,24 +179,9 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private MultipartFile getMultipartFile(InputStream responseBody,HttpServletResponse response) {
|
private MultipartFile getMultipartFile(InputStream responseBody,String contentType,String fileName) {
|
||||||
MultipartFile file;
|
MultipartFile file;
|
||||||
try {
|
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);
|
file = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(),fileName,contentType, responseBody);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|||||||
@@ -432,6 +432,7 @@ adminRoleCode: admin
|
|||||||
|
|
||||||
# 文件下载加解密
|
# 文件下载加解密
|
||||||
download:
|
download:
|
||||||
|
enable: true
|
||||||
# 文件下载、预览拦截路径,多个以逗号分隔
|
# 文件下载、预览拦截路径,多个以逗号分隔
|
||||||
download_url: /sys/common/download/
|
download_url: /sys/common/download/
|
||||||
view_url: /sys/common/view/
|
view_url: /sys/common/view/
|
||||||
|
|||||||
Reference in New Issue
Block a user