文件下载加解密
This commit is contained in:
-7
@@ -3,8 +3,6 @@ package com.jero.modules.docking.download.config;
|
|||||||
import com.jero.modules.docking.download.service.DownloadDecryptFileService;
|
import com.jero.modules.docking.download.service.DownloadDecryptFileService;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.context.request.RequestContextHolder;
|
|
||||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
||||||
import org.springframework.web.servlet.HandlerInterceptor;
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
|
||||||
@@ -34,11 +32,6 @@ public class DownloadInterceptor implements HandlerInterceptor{
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||||
// HandlerMethod handlerMethod = (HandlerMethod) handler;
|
|
||||||
// Method method = handlerMethod.getMethod();
|
|
||||||
// String methodName = method.getName();
|
|
||||||
// System.out.println("====拦截到了方法:"+methodName+",preHandle====");
|
|
||||||
// other
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+48
-48
@@ -8,14 +8,18 @@ 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;
|
||||||
|
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;
|
||||||
@@ -84,8 +88,8 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic
|
|||||||
@Override
|
@Override
|
||||||
public void downloadDecryptFile(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
public void downloadDecryptFile(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
||||||
// 判断返回值是否为文件流,否则其他接口返回值会变更成字符串,导致前端无法解析
|
// 判断返回值是否为文件流,否则其他接口返回值会变更成字符串,导致前端无法解析
|
||||||
String responseBody = null;
|
|
||||||
HandlerMethod handlerMethod = (HandlerMethod) handler;
|
HandlerMethod handlerMethod = (HandlerMethod) handler;
|
||||||
|
String responseBody = null;
|
||||||
try {
|
try {
|
||||||
String returnType = handlerMethod.getMethod().getReturnType().getName();
|
String returnType = handlerMethod.getMethod().getReturnType().getName();
|
||||||
if(!Objects.equals(returnType,"void") && !Objects.equals(returnType,"org.springframework.web.servlet.ModelAndView")){
|
if(!Objects.equals(returnType,"void") && !Objects.equals(returnType,"org.springframework.web.servlet.ModelAndView")){
|
||||||
@@ -93,71 +97,67 @@ public class DownloadDecryptFileServiceImpl implements DownloadDecryptFileServic
|
|||||||
}
|
}
|
||||||
responseBody = ContentCachingWrapperFilter.getResponseBody(response);
|
responseBody = ContentCachingWrapperFilter.getResponseBody(response);
|
||||||
if(StringUtils.isBlank(responseBody)){
|
if(StringUtils.isBlank(responseBody)){
|
||||||
return;
|
throw new JeroBootException(ResultCommon.ERROR);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
throw new JeroBootException(ResultCommon.ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MultipartFile file = getMultipartFile(responseBody);
|
||||||
|
// 访问路径
|
||||||
|
String str = request.getRequestURI();
|
||||||
|
|
||||||
|
boolean b = isBoolean(str, viewUrl);
|
||||||
|
|
||||||
|
boolean b1 = isBoolean(str, downloadUrl);
|
||||||
|
|
||||||
|
if(b){
|
||||||
|
// 预览,进行解密
|
||||||
|
writeFile(response, file, null);
|
||||||
|
}else if(b1){
|
||||||
|
// 下载文件 先解密,判断组织域,加密
|
||||||
|
writeFile(response, file, null);
|
||||||
|
int scope = getScope();
|
||||||
|
// 加密
|
||||||
|
writeFile(response, file, scope);
|
||||||
|
}else{
|
||||||
|
// excel 判断组织域,加密
|
||||||
|
int scope = getScope();
|
||||||
|
// 加密
|
||||||
|
writeFile(response, file, scope);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeFile(HttpServletResponse response, MultipartFile file, Integer scope) {
|
||||||
|
try (InputStream is = IntekeyUtils.DecryptFile(encryptUrl, encryptAppCode, encryptSecretKey, scope, file)) {
|
||||||
|
writeResponse(response, is);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new JeroBootException(ResultCommon.ERROR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private MultipartFile getMultipartFile(String responseBody) {
|
||||||
MultipartFile file;
|
MultipartFile file;
|
||||||
try {
|
try (InputStream inputStream = new ByteArrayInputStream(responseBody.getBytes(StandardCharsets.UTF_8))){
|
||||||
InputStream inputStream = new ByteArrayInputStream(responseBody.getBytes(StandardCharsets.UTF_8));
|
|
||||||
file = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
|
file = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
throw new JeroBootException(ResultCommon.ERROR);
|
throw new JeroBootException(ResultCommon.ERROR);
|
||||||
}
|
}
|
||||||
// 访问路径
|
return file;
|
||||||
String str = request.getRequestURI();
|
}
|
||||||
|
|
||||||
|
private boolean isBoolean(String str, List<String> viewUrl) {
|
||||||
boolean b = false;
|
boolean b = false;
|
||||||
for (String s : viewUrl) {
|
for (String s : viewUrl) {
|
||||||
if(s.contains(str)){
|
if (s.contains(str)) {
|
||||||
b = true;
|
b = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return b;
|
||||||
boolean b1 = false;
|
|
||||||
for (String s : downloadUrl) {
|
|
||||||
if(s.contains(str)){
|
|
||||||
b1 = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(b){
|
|
||||||
// 预览,进行解密
|
|
||||||
try (InputStream is = IntekeyUtils.DecryptFile(decryptUrl, decryptAppCode, decryptSecretKey, null, file)){
|
|
||||||
writeResponse(response,is);
|
|
||||||
}catch (Exception e){
|
|
||||||
throw new JeroBootException(ResultCommon.ERROR);
|
|
||||||
}
|
|
||||||
}else if(b1){
|
|
||||||
// 下载文件 先解密,判断组织域,加密
|
|
||||||
try (InputStream is = IntekeyUtils.DecryptFile(decryptUrl, decryptAppCode, decryptSecretKey, null, file)){
|
|
||||||
writeResponse(response,is);
|
|
||||||
}catch (Exception e){
|
|
||||||
throw new JeroBootException(ResultCommon.ERROR);
|
|
||||||
}
|
|
||||||
int scope = getScope();
|
|
||||||
// 加密
|
|
||||||
try (InputStream is = IntekeyUtils.DecryptFile(encryptUrl, encryptAppCode, encryptSecretKey, scope, file)){
|
|
||||||
writeResponse(response,is);
|
|
||||||
}catch (Exception e){
|
|
||||||
throw new JeroBootException(ResultCommon.ERROR);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}else{
|
|
||||||
// excel 判断组织域,加密
|
|
||||||
int scope = getScope();
|
|
||||||
// 加密
|
|
||||||
try (InputStream is = IntekeyUtils.DecryptFile(encryptUrl, encryptAppCode, encryptSecretKey, scope, file)){
|
|
||||||
writeResponse(response,is);
|
|
||||||
}catch (Exception e){
|
|
||||||
throw new JeroBootException(ResultCommon.ERROR);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getScope() {
|
private int getScope() {
|
||||||
|
|||||||
Reference in New Issue
Block a user