Browse Source

提交demo源码

10.0
LAPTOP-SB56SG4Q\86185 3 years ago
parent
commit
6d860f7264
  1. 4
      README.md
  2. 124
      build.gradle
  3. 13
      encrypt.xml
  4. 21
      plugin.xml
  5. 87
      src/main/java/com/tptj/demo/hg/export/encrypt/EncryptOutputStream.java
  6. 23
      src/main/java/com/tptj/demo/hg/export/encrypt/demo1/Demo.java
  7. 26
      src/main/java/com/tptj/demo/hg/export/encrypt/demo1/EncryptExcelOperate.java
  8. 48
      src/main/java/com/tptj/demo/hg/export/encrypt/demo1/EncryptExporter.java
  9. 13
      src/main/java/com/tptj/demo/hg/export/encrypt/demo1/Format.java
  10. 26
      src/main/java/com/tptj/demo/hg/export/encrypt/demo2/Demo.java
  11. 49
      src/main/java/com/tptj/demo/hg/export/encrypt/demo3/DecryptRequest.java
  12. 46
      src/main/java/com/tptj/demo/hg/export/encrypt/demo3/DelegatingServletInputStream.java
  13. 50
      src/main/java/com/tptj/demo/hg/export/encrypt/demo3/Demo.java
  14. 26
      src/main/java/com/tptj/demo/hg/export/encrypt/demo3/EncryptResponse.java

4
README.md

@ -1,3 +1,5 @@
# demo-export-encrypt # demo-export-encrypt
导入导出加解密demo 导入导出加解密demo\
三种基本的导入导出加解密实现方案。\
注:该代码仅用于教学演示!本身不保证在实际商业使用中代码的可靠性!需开发者自行根据需求进行调整!

124
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'])
}

13
encrypt.xml

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project>
<target name="compile_encrypt_javas" depends="copyFiles">
<echo message="加密文件"/>
<echo message="${projectDir}"/>
<taskdef name="pretreatment" classname="com.fr.plugin.pack.PluginPretreatmentTask">
<classpath refid="compile.classpath"/>
</taskdef>
<pretreatment baseDir="${projectDir}"/>
</target>
<target name="compile_plain_javas" depends="copyFiles">
</target>
</project>

21
plugin.xml

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?><plugin>
<id>com.tptj.demo.hg.export.encrypt.v10</id>
<name><![CDATA[ Export Encrypt ]]></name>
<active>yes</active>
<version>1.0</version>
<env-version>10.0</env-version>
<vendor>tptj</vendor>
<jartime>2019-07-18</jartime>
<description><![CDATA[ ]]></description>
<change-notes><![CDATA[]]></change-notes>
<main-package>com.tptj.demo.hg.export.encrypt</main-package>
<function-recorder class="com.tptj.demo.hg.export.encrypt.EncryptOutputStream"/>
<!-- 以下三个接口实际使用时只需要选择其中一个即可,同时使用会产生冲突! -->
<extra-decision>
<GlobalRequestFilterProvider class="com.tptj.demo.hg.export.encrypt.demo3.Demo"/>
</extra-decision>
<extra-report>
<ExportOperateProvider class="com.tptj.demo.hg.export.encrypt.demo1.Demo"/>
<ExportExtensionProcessor class="com.tptj.demo.hg.export.encrypt.demo2.Demo"/>
</extra-report>
</plugin>

87
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) {
}
}

23
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;
}
}

26
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;
}
}

48
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);
}
}

13
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";
}

26
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;
}
}

49
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 )));
}
}

46
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) { }
}

50
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());
}
}
}

26
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);
}
}
Loading…
Cancel
Save