From 6d860f72646e93b9e939b518713860205e39eaa1 Mon Sep 17 00:00:00 2001 From: "LAPTOP-SB56SG4Q\\86185" Date: Thu, 15 Jul 2021 11:41:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4demo=E6=BA=90=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +- build.gradle | 124 ++++++++++++++++++ encrypt.xml | 13 ++ plugin.xml | 21 +++ .../export/encrypt/EncryptOutputStream.java | 87 ++++++++++++ .../demo/hg/export/encrypt/demo1/Demo.java | 23 ++++ .../encrypt/demo1/EncryptExcelOperate.java | 26 ++++ .../export/encrypt/demo1/EncryptExporter.java | 48 +++++++ .../demo/hg/export/encrypt/demo1/Format.java | 13 ++ .../demo/hg/export/encrypt/demo2/Demo.java | 26 ++++ .../export/encrypt/demo3/DecryptRequest.java | 49 +++++++ .../demo3/DelegatingServletInputStream.java | 46 +++++++ .../demo/hg/export/encrypt/demo3/Demo.java | 50 +++++++ .../export/encrypt/demo3/EncryptResponse.java | 26 ++++ 14 files changed, 555 insertions(+), 1 deletion(-) create mode 100644 build.gradle create mode 100644 encrypt.xml create mode 100644 plugin.xml create mode 100644 src/main/java/com/tptj/demo/hg/export/encrypt/EncryptOutputStream.java create mode 100644 src/main/java/com/tptj/demo/hg/export/encrypt/demo1/Demo.java create mode 100644 src/main/java/com/tptj/demo/hg/export/encrypt/demo1/EncryptExcelOperate.java create mode 100644 src/main/java/com/tptj/demo/hg/export/encrypt/demo1/EncryptExporter.java create mode 100644 src/main/java/com/tptj/demo/hg/export/encrypt/demo1/Format.java create mode 100644 src/main/java/com/tptj/demo/hg/export/encrypt/demo2/Demo.java create mode 100644 src/main/java/com/tptj/demo/hg/export/encrypt/demo3/DecryptRequest.java create mode 100644 src/main/java/com/tptj/demo/hg/export/encrypt/demo3/DelegatingServletInputStream.java create mode 100644 src/main/java/com/tptj/demo/hg/export/encrypt/demo3/Demo.java create mode 100644 src/main/java/com/tptj/demo/hg/export/encrypt/demo3/EncryptResponse.java diff --git a/README.md b/README.md index ba18d5c..be6b6f9 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ # demo-export-encrypt -导入导出加解密demo \ No newline at end of file +导入导出加解密demo\ +三种基本的导入导出加解密实现方案。\ +注:该代码仅用于教学演示!本身不保证在实际商业使用中代码的可靠性!需开发者自行根据需求进行调整! \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..5231b5d --- /dev/null +++ b/build.gradle @@ -0,0 +1,124 @@ + +apply plugin: 'java' + +[compileJava,compileTestJava]*.options*.encoding = 'UTF-8' + +ext { + /** + * 项目中依赖的jar的路径 + * 1.如果依赖的jar需要打包到zip中,放置在lib根目录下 + * 2.如果依赖的jar仅仅是编译时需要,防止在lib下子目录下即可 + */ + libPath = "$projectDir/../../webroot/WEB-INF/lib" + + /** + * 是否对插件的class进行加密保护,防止反编译 + */ + guard = false + + def pluginInfo = getPluginInfo() + pluginPre = "fine-plugin" + pluginName = pluginInfo.id + pluginVersion = pluginInfo.version + + outputPath = "$projectDir/../../webroot/WEB-INF/plugins/plugin-" + pluginName + "-1.0/classes" +} + +group = 'com.fr.plugin' +version = '10.0' +sourceCompatibility = '8' + +sourceSets { + main { + java.outputDir = file(outputPath) + output.resourcesDir = file(outputPath) + } +} + +ant.importBuild("encrypt.xml") +//定义ant变量 +ant.projectDir = projectDir +ant.references["compile.classpath"] = ant.path { + fileset(dir: libPath, includes: '**/*.jar') + fileset(dir: ".",includes:"**/*.jar" ) +} + +classes.dependsOn('clean') + +task copyFiles(type: Copy,dependsOn: 'classes'){ + from outputPath + into "$projectDir/classes" +} + +task preJar(type:Copy,dependsOn: guard ? 'compile_encrypt_javas' : 'compile_plain_javas'){ + from "$projectDir/classes" + into "$projectDir/transform-classes" + include "**/*.*" +} +jar.dependsOn("preJar") + +task makeJar(type: Jar,dependsOn: preJar){ + from fileTree(dir: "$projectDir/transform-classes") + baseName pluginPre + appendix pluginName + version pluginVersion + destinationDir = file("$buildDir/libs") + + doLast(){ + delete file("$projectDir/classes") + delete file("$projectDir/transform-classes") + } +} + +task copyFile(type: Copy,dependsOn: ["makeJar"]){ + from "$buildDir/libs" + from("$projectDir/lib") { + include "*.jar" + } + from "$projectDir/plugin.xml" + into file("$buildDir/temp/plugin") +} + +task zip(type:Zip,dependsOn:["copyFile"]){ + from "$buildDir/temp/plugin" + destinationDir file("$buildDir/install") + baseName pluginPre + appendix pluginName + version pluginVersion +} + +//控制build时包含哪些文件,排除哪些文件 +processResources { +// exclude everything +// 用*.css没效果 +// exclude '**/*.css' +// except this file +// include 'xx.xml' +} + +/*读取plugin.xml中的version*/ +def getPluginInfo(){ + def xmlFile = file("plugin.xml") + if (!xmlFile.exists()) { + return ["id":"none", "version":"1.0.0"] + } + def plugin = new XmlParser().parse(xmlFile) + def version = plugin.version[0].text() + def id = plugin.id[0].text() + return ["id":id,"version":version] +} + +repositories { + mavenLocal() + maven { + url = uri('http://mvn.finedevelop.com/repository/maven-public/') + } +} + +dependencies { + //使用本地jar + implementation fileTree(dir: 'lib', include: ['**/*.jar']) + implementation fileTree(dir: libPath, include: ['**/*.jar']) +} + + diff --git a/encrypt.xml b/encrypt.xml new file mode 100644 index 0000000..1401cd1 --- /dev/null +++ b/encrypt.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/plugin.xml b/plugin.xml new file mode 100644 index 0000000..4530587 --- /dev/null +++ b/plugin.xml @@ -0,0 +1,21 @@ + + com.tptj.demo.hg.export.encrypt.v10 + + yes + 1.0 + 10.0 + tptj + 2019-07-18 + + + com.tptj.demo.hg.export.encrypt + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/tptj/demo/hg/export/encrypt/EncryptOutputStream.java b/src/main/java/com/tptj/demo/hg/export/encrypt/EncryptOutputStream.java new file mode 100644 index 0000000..64e35c4 --- /dev/null +++ b/src/main/java/com/tptj/demo/hg/export/encrypt/EncryptOutputStream.java @@ -0,0 +1,87 @@ +package com.tptj.demo.hg.export.encrypt; + +import com.fr.intelli.record.Focus; +import com.fr.record.analyzer.EnableMetrics; +import com.fr.third.org.bouncycastle.util.Arrays; +import javax.servlet.ServletOutputStream; +import javax.servlet.WriteListener; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +/** + * @author 秃破天际 + * @version 10.0 + * Created by 秃破天际 on 2021/7/15 + **/ +@EnableMetrics +public class EncryptOutputStream extends ServletOutputStream { + + private OutputStream original = null; + private boolean closed = false; + private ByteArrayOutputStream crypt = new ByteArrayOutputStream(); + + public EncryptOutputStream( OutputStream original ){ + this.original = original; + } + + /** + * 实际加密的代码就在这里实现了 + * @param bytes + * @return + */ + @Focus(id="com.tptj.demo.hg.export.encrypt.v10",text="Export Encrypt") + private byte[] encrypt(byte[] bytes){ + //假设逆序就是加密了 + return Arrays.reverse(bytes); + } + + @Override + public void write(int b) throws IOException { + if (closed) { + throw new IOException("Cannot write to a closed output stream"); + } + crypt.write(b); + } + + @Override + public void close() throws IOException { + if( !closed ){ + byte[] bytes = crypt.toByteArray(); + bytes = encrypt(bytes); + original.write(bytes); + original.flush(); + original.close(); + closed = true; + } + } + + @Override + public void flush() throws IOException { + crypt.flush(); + } + + @Override + public void write(byte[] data) throws IOException { + write(data, 0, data.length); + } + + @Override + public void write(byte[] data, int off, int len) throws IOException { + if (closed) { + throw new IOException("Cannot write to a closed output stream"); + } + crypt.write(data, off, len); + } + + @Override + public boolean isReady() { + return true; + } + + @Override + public void setWriteListener(WriteListener listener) { + + } + +} diff --git a/src/main/java/com/tptj/demo/hg/export/encrypt/demo1/Demo.java b/src/main/java/com/tptj/demo/hg/export/encrypt/demo1/Demo.java new file mode 100644 index 0000000..b27eb08 --- /dev/null +++ b/src/main/java/com/tptj/demo/hg/export/encrypt/demo1/Demo.java @@ -0,0 +1,23 @@ +package com.tptj.demo.hg.export.encrypt.demo1; + +import com.fr.report.fun.impl.AbstractExportOperateProvider; +import com.fr.web.core.reserve.Operate; + +/** + * @author 秃破天际 + * @version 10.0 + * Created by 秃破天际 on 2021/7/15 + * 仅对某一种文件导出加密的方案1 + **/ +public class Demo extends AbstractExportOperateProvider { + + @Override + public Operate operate() { + return new EncryptExcelOperate(); + } + + @Override + public String markType() { + return Format.EXCEL; + } +} diff --git a/src/main/java/com/tptj/demo/hg/export/encrypt/demo1/EncryptExcelOperate.java b/src/main/java/com/tptj/demo/hg/export/encrypt/demo1/EncryptExcelOperate.java new file mode 100644 index 0000000..33d549b --- /dev/null +++ b/src/main/java/com/tptj/demo/hg/export/encrypt/demo1/EncryptExcelOperate.java @@ -0,0 +1,26 @@ +package com.tptj.demo.hg.export.encrypt.demo1; + +import com.fr.io.collection.ExportCollection; +import com.fr.io.exporter.AppExporter; +import com.fr.stable.web.SessionProvider; +import com.fr.web.core.reserve.ExcelOperate; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * @author 秃破天际 + * @version 10.0 + * Created by 秃破天际 on 2021/7/15 + **/ +public class EncryptExcelOperate extends ExcelOperate { + + @Override + public ExportCollection newExportCollection(HttpServletRequest req, HttpServletResponse res, + SessionProvider sessionIDInfor, String filename ){ + ExportCollection collection = super.newExportCollection(req, res, sessionIDInfor, filename); + AppExporter exporter = collection.getExporter(); + collection.setExporter( new EncryptExporter(exporter) ); + return collection; + } +} diff --git a/src/main/java/com/tptj/demo/hg/export/encrypt/demo1/EncryptExporter.java b/src/main/java/com/tptj/demo/hg/export/encrypt/demo1/EncryptExporter.java new file mode 100644 index 0000000..6cbb321 --- /dev/null +++ b/src/main/java/com/tptj/demo/hg/export/encrypt/demo1/EncryptExporter.java @@ -0,0 +1,48 @@ +package com.tptj.demo.hg.export.encrypt.demo1; + +import com.fr.io.exporter.AppExporter; +import com.fr.main.workbook.ResultWorkBook; +import com.fr.page.PageSetCreator; +import com.fr.page.PageSetProvider; +import com.fr.web.core.ReportRepositoryDeal; +import com.tptj.demo.hg.export.encrypt.EncryptOutputStream; + +import java.io.OutputStream; + +/** + * @author 秃破天际 + * @version 10.0 + * Created by 秃破天际 on 2021/7/15 + **/ +public class EncryptExporter implements AppExporter { + + private AppExporter original; + + public EncryptExporter(AppExporter original){ + this.original = original; + } + @Override + public void export( OutputStream out, ResultWorkBook book ) throws Exception { + original.export( new EncryptOutputStream(out), book ); + } + + @Override + public void export( OutputStream out, PageSetProvider pageSet ) throws Exception { + original.export( new EncryptOutputStream(out), pageSet ); + } + + @Override + public void export(OutputStream out, ResultWorkBook book, PageSetCreator creator, ReportRepositoryDeal repo, int[] sheets) throws Exception { + original.export( new EncryptOutputStream(out), book,creator,repo,sheets); + } + + @Override + public void export(OutputStream out, ResultWorkBook book, PageSetProvider pageSet, ReportRepositoryDeal repo, int[] sheets) throws Exception { + original.export( new EncryptOutputStream(out), book,pageSet,repo,sheets); + } + + @Override + public void setVersion(Object o) { + original.setVersion(o); + } +} diff --git a/src/main/java/com/tptj/demo/hg/export/encrypt/demo1/Format.java b/src/main/java/com/tptj/demo/hg/export/encrypt/demo1/Format.java new file mode 100644 index 0000000..6eaf29d --- /dev/null +++ b/src/main/java/com/tptj/demo/hg/export/encrypt/demo1/Format.java @@ -0,0 +1,13 @@ +package com.tptj.demo.hg.export.encrypt.demo1; + +/** + * @author 秃破天际 + * @version 10.0 + * Created by 秃破天际 on 2021/7/15 + **/ +public class Format { + public static final String EXCEL = "excel"; + public static final String WORD = "word"; + public static final String PDF = "pdf"; + public static final String IMAGE = "image"; +} diff --git a/src/main/java/com/tptj/demo/hg/export/encrypt/demo2/Demo.java b/src/main/java/com/tptj/demo/hg/export/encrypt/demo2/Demo.java new file mode 100644 index 0000000..a8c42a0 --- /dev/null +++ b/src/main/java/com/tptj/demo/hg/export/encrypt/demo2/Demo.java @@ -0,0 +1,26 @@ +package com.tptj.demo.hg.export.encrypt.demo2; + +import com.fr.io.collection.ExportCollection; +import com.fr.io.exporter.AppExporter; +import com.fr.web.core.ReportSessionIDInfor; +import com.fr.web.core.reserve.DefaultExportExtension; +import com.tptj.demo.hg.export.encrypt.demo1.EncryptExporter; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * @author 秃破天际 + * @version 10.0 + * Created by 秃破天际 on 2021/7/15 + **/ +public class Demo extends DefaultExportExtension { + + @Override + public ExportCollection createCollection(HttpServletRequest req, HttpServletResponse res, + ReportSessionIDInfor info, String format, String filename, boolean isEmbed) throws Exception { + ExportCollection collection = super.createCollection(req, res, info, format,filename,isEmbed); + AppExporter exporter = collection.getExporter(); + collection.setExporter( new EncryptExporter(exporter) ); + return collection; + } +} diff --git a/src/main/java/com/tptj/demo/hg/export/encrypt/demo3/DecryptRequest.java b/src/main/java/com/tptj/demo/hg/export/encrypt/demo3/DecryptRequest.java new file mode 100644 index 0000000..cc682a6 --- /dev/null +++ b/src/main/java/com/tptj/demo/hg/export/encrypt/demo3/DecryptRequest.java @@ -0,0 +1,49 @@ +package com.tptj.demo.hg.export.encrypt.demo3; + +import com.fr.general.IOUtils; +import com.fr.third.org.bouncycastle.util.Arrays; + +import javax.servlet.ServletInputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStreamReader; + +/** + * @author 秃破天际 + * @version 10.0 + * Created by 秃破天际 on 2021/7/15 + **/ +public class DecryptRequest extends HttpServletRequestWrapper { + private byte[] data; + public DecryptRequest(HttpServletRequest request) { + super(request); + init(); + } + + private void init(){ + try{ + data = IOUtils.inputStream2Bytes(getRequest().getInputStream()); + //实现解密(假设加密就是逆序,那么解密就是再次逆序即可) + data = Arrays.reverse(data); + }catch(Exception e){ + data = new byte[0]; + } + } + + @Override + public ServletInputStream getInputStream() throws IOException { + try{ + return new DelegatingServletInputStream(new ByteArrayInputStream( data )); + }catch (Exception e){ + return null; + } + } + + @Override + public BufferedReader getReader()throws IOException{ + return new BufferedReader(new InputStreamReader(new ByteArrayInputStream( data ))); + } +} diff --git a/src/main/java/com/tptj/demo/hg/export/encrypt/demo3/DelegatingServletInputStream.java b/src/main/java/com/tptj/demo/hg/export/encrypt/demo3/DelegatingServletInputStream.java new file mode 100644 index 0000000..882d28f --- /dev/null +++ b/src/main/java/com/tptj/demo/hg/export/encrypt/demo3/DelegatingServletInputStream.java @@ -0,0 +1,46 @@ +package com.tptj.demo.hg.export.encrypt.demo3; + +import javax.servlet.ReadListener; +import javax.servlet.ServletInputStream; +import java.io.IOException; +import java.io.InputStream; + +/** + * @author 秃破天际 + * @version 10.0 + * Created by 秃破天际 on 2021/7/15 + **/ +public class DelegatingServletInputStream extends ServletInputStream { + private final InputStream sourceStream; + + public DelegatingServletInputStream(InputStream stream) { + this.sourceStream = stream; + } + + public final InputStream getSourceStream() { + return this.sourceStream; + } + @Override + public int read() throws IOException { + return this.sourceStream.read(); + } + + @Override + public void close() throws IOException { + super.close(); + this.sourceStream.close(); + } + + @Override + public boolean isFinished() { + return false; + } + + @Override + public boolean isReady() { + return false; + } + + @Override + public void setReadListener(ReadListener listener) { } +} \ No newline at end of file diff --git a/src/main/java/com/tptj/demo/hg/export/encrypt/demo3/Demo.java b/src/main/java/com/tptj/demo/hg/export/encrypt/demo3/Demo.java new file mode 100644 index 0000000..bf96696 --- /dev/null +++ b/src/main/java/com/tptj/demo/hg/export/encrypt/demo3/Demo.java @@ -0,0 +1,50 @@ +package com.tptj.demo.hg.export.encrypt.demo3; + +import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider; +import com.fr.log.FineLoggerFactory; +import com.fr.stable.StringUtils; +import com.fr.web.utils.WebUtils; + +import javax.servlet.FilterChain; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * @author 秃破天际 + * @version 10.0 + * Created by 秃破天际 on 2021/7/15 + **/ + +public class Demo extends AbstractGlobalRequestFilterProvider { + @Override + public String filterName() { + return "Export Encrypt"; + } + + @Override + public String[] urlPatterns() { + return new String[]{ + "/decision/view/report" + }; + } + + @Override + public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) { + String op = WebUtils.getHTTPRequestParameter(req,"op"); + String format = WebUtils.getHTTPRequestParameter(req,"format"); + String cmd = WebUtils.getHTTPRequestParameter(req,"cmd"); + if( "export".equals(op) && StringUtils.isNotEmpty(format) ){ + //如果是导出,就包裹导出的响应,通过响应中包裹的outputstream进行输出的加密 + res = new EncryptResponse( res ); + } + if( "fr_write".equals(op) && "imp_w_excel_data".equals(cmd) ){ + //导入excel,包裹请求,读取输入的文件流,先解密,再交给后续的产品处理 + req = new DecryptRequest(req); + } + try{ + chain.doFilter(req,res); + }catch(Exception e){ + FineLoggerFactory.getLogger().error(e,e.getMessage()); + } + } +} diff --git a/src/main/java/com/tptj/demo/hg/export/encrypt/demo3/EncryptResponse.java b/src/main/java/com/tptj/demo/hg/export/encrypt/demo3/EncryptResponse.java new file mode 100644 index 0000000..f1ae9d4 --- /dev/null +++ b/src/main/java/com/tptj/demo/hg/export/encrypt/demo3/EncryptResponse.java @@ -0,0 +1,26 @@ +package com.tptj.demo.hg.export.encrypt.demo3; + +import com.tptj.demo.hg.export.encrypt.EncryptOutputStream; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpServletResponseWrapper; +import java.io.IOException; + +/** + * @author 秃破天际 + * @version 10.0 + * Created by 秃破天际 on 2021/7/15 + **/ +public class EncryptResponse extends HttpServletResponseWrapper { + + public EncryptResponse(HttpServletResponse response) { + super(response); + } + + public ServletOutputStream getOutputStream()throws IOException { + ServletOutputStream original = super.getOutputStream(); + return new EncryptOutputStream(original); + } + +}