You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
7.3 KiB

package com.eco.plugin.wink.xdfileencrypt.filter;
import com.eco.plugin.wink.xdfileencrypt.utils.FRUtils;
import com.eco.plugin.wink.xdfileencrypt.utils.FileUtils;
import com.eco.plugin.wink.xdfileencrypt.utils.HttpUtils;
import com.fr.base.core.IgnoreBytesInputStream;
import com.fr.base.core.ParseResult;
import com.fr.base.core.PostParseUtils;
import com.fr.cache.Attachment;
import com.fr.cache.AttachmentFileBase;
import com.fr.cache.AttachmentSource;
import com.fr.cache.factory.AttachmentFileManager;
import com.fr.cache.type.AttachmentScope;
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider;
import com.fr.decision.record.OperateMessage;
import com.fr.decision.webservice.utils.CharLimitType;
import com.fr.decision.webservice.utils.WebServiceUtils;
import com.fr.decision.webservice.v10.security.SecurityService;
import com.fr.general.CommonIOUtils;
import com.fr.general.ComparatorUtils;
import com.fr.intelli.record.MetricRegistry;
import com.fr.plugin.transform.FunctionRecorder;
import com.fr.record.analyzer.EnableMetrics;
import com.fr.security.WebSecurityConfig;
import com.fr.stable.CodeUtils;
import com.fr.third.org.apache.commons.io.FilenameUtils;
import com.fr.third.org.apache.http.entity.ContentType;
import com.fr.web.AttachmentHelper;
import com.fr.web.utils.WebUtils;
import javax.servlet.FilterChain;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.UUID;
@EnableMetrics
@FunctionRecorder
public class SSOFilter extends AbstractGlobalRequestFilterProvider {
private static final byte[] NEW_LINE_BYTES = new byte[]{13, 10};
private static final byte[] BOUNDARY_END = new byte[]{45, 45};
// private static final String directory = "/opt/tomcat/temp/";
// private static final String directory = "E:\\temp\\";
private static final String directory = "/usr/fine/temp";
@Override
public String filterName() {
return "xdfileencryptFilter";
}
@Override
public String[] urlPatterns() {
return new String[]{"/*"};
}
@Override
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain ){
String url = FRUtils.getAllUrl(req);
if(url.contains("global/export")){
res = new EncryptResponse( res );
}
if(url.contains("attach/upload")){
String filename = req.getParameter("filename");
if(filename.contains(".xls") || filename.contains(".xlsx") || filename.contains(".csv")){
try {
int width = Integer.valueOf(req.getParameter("width"));
int height = Integer.valueOf(req.getParameter("height"));
uploadAttach(req,res,width,height,filename,false);
} catch (Exception e) {
FRUtils.FRLogInfo("上传文件失败:"+filename);
}
return ;
}
}
release(req,res,chain);
}
private static void uploadAttach(HttpServletRequest req, HttpServletResponse res, int width, int height, String filename, boolean isHolder) throws Exception {
SecurityService.checkXss(filename);
if (!filename.startsWith(" ") && !WebServiceUtils.containIllegalChars(CharLimitType.UPLOAD_FILE_NAME_LIMIT, filename)) {
ServletInputStream is = req.getInputStream();
ParseResult fileheader = PostParseUtils.parse(is, req.getCharacterEncoding());
/**
* 文件落盘
*/
String filenameNew = directory+"jm"+ UUID.randomUUID().toString()+ filename;
IgnoreBytesInputStream ignoreBytesInputStream = new IgnoreBytesInputStream(is, concat(concat(NEW_LINE_BYTES, fileheader.getBoundary().getBytes()), BOUNDARY_END));
FileUtils.inputstreamToFile(ignoreBytesInputStream,filenameNew);
/**
* 获取新文件inputStream
*/
byte[] decryptFileByte = HttpUtils.getFileByte("http://localhost:8090/decrypt?fileName="+URLEncoder.encode(filenameNew));
InputStream inputStream = new ByteArrayInputStream(decryptFileByte);
IgnoreBytesInputStream ignoreBytesInputStream2 = new IgnoreBytesInputStream(inputStream, concat(concat(NEW_LINE_BYTES, fileheader.getBoundary().getBytes()), BOUNDARY_END));
String fileType = fileheader.getContentType().indexOf("image") > 0 ? "image" : "other";
Attachment attachment = addAttachment(fileType, CodeUtils.cjkDecode(filename), ignoreBytesInputStream2, width, height, isHolder ? AttachmentScope.HOLDER : AttachmentScope.DEFAULT);
if (WebSecurityConfig.getInstance().isFileVerificationEnabled()) {
checkFile(fileType, attachment, filename);
}
MetricRegistry.getMetric().submit(OperateMessage.build("Dec-Module-Attachment", "Dec-Attachment_Upload", filename, "Dec-Log_Add"));
res.setContentType(ContentType.APPLICATION_JSON.getMimeType());
PrintWriter var12 = WebUtils.createPrintWriter(res);
var12.print(attachment.toConfig());
var12.flush();
var12.close();
} else {
throw new Exception("Dec-Data_Set_File_Name_Error");
}
}
/**
* 添加附件
* @param fileType 附件类型 image or other
* @param filename
* @param is
* @param width
* @param height
* @param scope
* @return
*/
public static Attachment addAttachment(String fileType, String filename, InputStream is, int width, int height, AttachmentScope scope) {
AttachmentFileBase fileBase = AttachmentFileManager.getManager().createFile((String)null, is, scope);
Attachment attachment = new Attachment(fileBase.getFileName(), fileType, filename, fileBase, width, height, scope);
AttachmentSource.putAttachment(fileBase.getFileName(), attachment);
return attachment;
}
private static byte[] concat(byte[] var1, byte[] var2) {
byte[] var3 = new byte[var1.length + var2.length];
System.arraycopy(var1, 0, var3, 0, var1.length);
System.arraycopy(var2, 0, var3, var1.length, var2.length);
return var3;
}
private static void checkFile(String var1, Attachment var2, String var3) throws Exception {
InputStream var4 = var2.getInputStream();
if (ComparatorUtils.equals(var1, "image") && var4.available() > 20971520) {
AttachmentHelper.removeAttachment(var2.getID());
throw new Exception("Dec-File_Too_Large_Error");
} else {
try {
boolean var5 = WebSecurityConfig.getInstance().getFileInspector().checkFileType(var4, FilenameUtils.getExtension(var3));
if (!var5) {
AttachmentHelper.removeAttachment(var2.getID());
throw new Exception("Dec-Invalid_file_Error");
}
} finally {
CommonIOUtils.close(var4);
}
}
}
//放行拦截器
private void release(HttpServletRequest req, HttpServletResponse res, FilterChain chain) {
try{
chain.doFilter(req,res);
}catch (Exception e){
}
}
}