hugh
4 years ago
8 changed files with 300 additions and 1 deletions
@ -1,3 +1,6 @@
|
||||
# demo-global-request-filter |
||||
|
||||
global filter 的demo示例 |
||||
global filter 的demo示例\ |
||||
Demo示例简单展示了该接口的最基本的用法\ |
||||
SedDemo示例用于指明多个global filter接口的执行顺序\ |
||||
ErrorDemo示例则展示了该接口的错误用法\ |
@ -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']) |
||||
} |
||||
|
||||
|
@ -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> |
Binary file not shown.
@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?><plugin> |
||||
<id>com.tptj.demo.hg.global.filter.v10</id> |
||||
<name><![CDATA[ global filter ]]></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.global.filter</main-package> |
||||
<function-recorder class="com.tptj.demo.hg.global.filter.Demo"/> |
||||
<extra-decision> |
||||
<GlobalRequestFilterProvider class="com.tptj.demo.hg.global.filter.Demo"/> |
||||
<GlobalRequestFilterProvider class="com.tptj.demo.hg.global.filter.ErrorDemo"/> |
||||
<GlobalRequestFilterProvider class="com.tptj.demo.hg.global.filter.SedDemo"/> |
||||
</extra-decision> |
||||
</plugin> |
@ -0,0 +1,54 @@
|
||||
package com.tptj.demo.hg.global.filter; |
||||
|
||||
|
||||
import com.fanruan.api.log.LogKit; |
||||
import com.fr.decision.fun.GlobalRequestFilterProvider; |
||||
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider; |
||||
import com.fr.intelli.record.Focus; |
||||
import com.fr.record.analyzer.EnableMetrics; |
||||
import com.fr.web.utils.WebUtils; |
||||
import org.jetbrains.annotations.NotNull; |
||||
|
||||
import javax.servlet.FilterChain; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
|
||||
/** |
||||
* @author 秃破天际 |
||||
* @version 10.0 |
||||
* Created by 秃破天际 on 2021-03-26 |
||||
* 注:filter除极特殊情况外,只能用于请求的预处理、转换或重定向。不得用于生成一个新的web业务功能接口 |
||||
**/ |
||||
@EnableMetrics |
||||
public class Demo extends AbstractGlobalRequestFilterProvider { |
||||
@Override |
||||
public String filterName() { |
||||
return "Demo"; |
||||
} |
||||
|
||||
@Override |
||||
public String[] urlPatterns() { |
||||
return new String[]{"/*"}; |
||||
} |
||||
|
||||
@Override |
||||
@Focus(id="com.tptj.demo.hg.global.filter.v10",text = "global filter") |
||||
public void doFilter( HttpServletRequest req, HttpServletResponse res, FilterChain chain ){ |
||||
LogKit.info( "sort 1,path={},ip={}", req.getPathInfo(), WebUtils.getIpAddr(req) ); |
||||
try{ |
||||
chain.doFilter(req,res); |
||||
}catch (Exception e){ |
||||
LogKit.error(e.getMessage(),e); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 可选实现的多个filter排序(执行顺序)的方法 |
||||
* @param other |
||||
* @return 0 相等,大于0是自身优先 小于0 是other优先 |
||||
*/ |
||||
@Override |
||||
public int compareTo(@NotNull GlobalRequestFilterProvider other ){ |
||||
return 0; |
||||
} |
||||
} |
@ -0,0 +1,39 @@
|
||||
package com.tptj.demo.hg.global.filter; |
||||
|
||||
import com.fanruan.api.log.LogKit; |
||||
import com.fanruan.api.web.FlushKit; |
||||
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider; |
||||
|
||||
import javax.servlet.FilterChain; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
|
||||
/** |
||||
* @author 秃破天际 |
||||
* @version 10.0 |
||||
* Created by 秃破天际 on 2021-03-26 |
||||
* 这是一个错误的场景使用示例! |
||||
**/ |
||||
public class ErrorDemo extends AbstractGlobalRequestFilterProvider { |
||||
|
||||
@Override |
||||
public String filterName() { |
||||
return "Error Demo"; |
||||
} |
||||
|
||||
@Override |
||||
public String[] urlPatterns() { |
||||
return new String[]{ |
||||
"/decision/error/demo" |
||||
}; |
||||
} |
||||
|
||||
@Override |
||||
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain ) { |
||||
try{ |
||||
FlushKit.printAsString(res,"This Is A Error Demo!"); |
||||
}catch(Exception e){ |
||||
LogKit.error(e.getMessage(),e); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,48 @@
|
||||
package com.tptj.demo.hg.global.filter; |
||||
|
||||
import com.fanruan.api.log.LogKit; |
||||
import com.fr.decision.fun.GlobalRequestFilterProvider; |
||||
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider; |
||||
import com.fr.web.utils.WebUtils; |
||||
import org.jetbrains.annotations.NotNull; |
||||
|
||||
import javax.servlet.FilterChain; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
|
||||
/** |
||||
* @author 秃破天际 |
||||
* @version 10.0 |
||||
* Created by 秃破天际 on 2021-03-26 |
||||
**/ |
||||
public class SedDemo extends AbstractGlobalRequestFilterProvider { |
||||
@Override |
||||
public String filterName() { |
||||
return "A Demo"; |
||||
} |
||||
|
||||
@Override |
||||
public String[] urlPatterns() { |
||||
return new String[]{"/*"}; |
||||
} |
||||
|
||||
@Override |
||||
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain ){ |
||||
LogKit.info( "sort 2,path={},ip={}", req.getPathInfo(), WebUtils.getIpAddr(req) ); |
||||
try{ |
||||
chain.doFilter(req,res); |
||||
}catch (Exception e){ |
||||
LogKit.error(e.getMessage(),e); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 可选实现的多个filter排序(执行顺序)的方法 |
||||
* @param other |
||||
* @return 0 相等,大于0是自身优先 小于0 是other优先 |
||||
*/ |
||||
@Override |
||||
public int compareTo(@NotNull GlobalRequestFilterProvider other ){ |
||||
return 0; |
||||
} |
||||
} |
Loading…
Reference in new issue