Browse Source

第三课

master
onlyxx 4 years ago
commit
af8e7020c6
  1. 122
      build.gradle
  2. BIN
      doc/插件开发成长计划系列教程-第三章.pptx
  3. 13
      encrypt.xml
  4. 23
      plugin.xml
  5. 3
      readme.md
  6. 100
      src/main/java/com/fr/plugin/MyEmbedRequestFilter.java
  7. 69
      src/main/java/com/fr/plugin/MyGlobalRequestFilter.java

122
build.gradle

@ -0,0 +1,122 @@
apply plugin: 'java'
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'])
}

BIN
doc/插件开发成长计划系列教程-第三章.pptx

Binary file not shown.

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>

23
plugin.xml

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?><plugin>
<id>com.fr.plugin.my.filter</id>
<name><![CDATA[拦截器插件]]></name>
<active>yes</active>
<version>1.0</version>
<env-version>10.0</env-version>
<jartime>2018-07-31</jartime>
<vendor>author</vendor>
<description><![CDATA[拦截登录]]></description>
<change-notes><![CDATA[
[2020-06-02]初始化插件。<br/>
]]></change-notes>
<!-- <extra-decision>-->
<!-- <EmbedRequestFilterProvider class="com.fr.plugin.MyEmbedRequestFilter"/>-->
<!-- </extra-decision>-->
<extra-decision>
<GlobalRequestFilterProvider class="com.fr.plugin.MyGlobalRequestFilter"/>
</extra-decision>
<function-recorder class="com.fr.plugin.MyEmbedRequestFilter"/>
</plugin>

3
readme.md

@ -0,0 +1,3 @@
# 函数插件
该函数可以求两个整数的最大公约数。

100
src/main/java/com/fr/plugin/MyEmbedRequestFilter.java

@ -0,0 +1,100 @@
package com.fr.plugin;
import com.fr.data.NetworkHelper;
import com.fr.decision.authority.data.User;
import com.fr.decision.fun.impl.AbstractEmbedRequestFilterProvider;
import com.fr.decision.mobile.terminal.TerminalHandler;
import com.fr.decision.webservice.bean.authentication.LoginClientBean;
import com.fr.decision.webservice.exception.general.ServerTimeoutException;
import com.fr.decision.webservice.exception.login.LoginInfoNotAvailableException;
import com.fr.decision.webservice.exception.login.UserPwdErrorException;
import com.fr.decision.webservice.exception.user.UserNotAvailableException;
import com.fr.decision.webservice.utils.DecisionServiceConstants;
import com.fr.decision.webservice.utils.DecisionStatusService;
import com.fr.decision.webservice.v10.login.LoginService;
import com.fr.decision.webservice.v10.login.TokenResource;
import com.fr.decision.webservice.v10.user.UserService;
import com.fr.general.ComparatorUtils;
import com.fr.log.FineLoggerFactory;
import com.fr.plugin.transform.ExecuteFunctionRecord;
import com.fr.plugin.transform.FunctionRecorder;
import com.fr.security.JwtUtils;
import com.fr.stable.StringUtils;
import com.fr.web.utils.WebUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Base64;
@FunctionRecorder
public class MyEmbedRequestFilter extends AbstractEmbedRequestFilterProvider {
@Override
@ExecuteFunctionRecord
public void filter(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
if(!isLogged(httpServletRequest)){
//第一步获取token
String my_token = httpServletRequest.getParameter("my_token");
//第二步判断是否是否为空
if (StringUtils.isNotBlank(my_token)) {
try {
String userName =new String( Base64.getUrlDecoder().decode(my_token));
UserService userService = UserService.getInstance();
//第三步检查用户是否存在
User user = userService.getUserByUserName(userName);
if (user != null) {
//第四步登录并设置Token到请求中
String token = LoginService.getInstance().login(httpServletRequest, httpServletResponse, userName);
httpServletRequest.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME,token);
}
FineLoggerFactory.getLogger().info("{} login success from my filter",userName);
}catch (Exception e){
e.printStackTrace();
}
}
}
}
// private boolean isLogin(HttpServletRequest request){
// return LoginService.getInstance().isLogged(request);
// }
public LoginClientBean loginStatusValid(String token, TerminalHandler terminal) throws Exception {
if (StringUtils.isEmpty(token)) {
throw new LoginInfoNotAvailableException("Token is empty!");
} else if (!JwtUtils.checkJWTExpired(token)) {
throw new ServerTimeoutException();
} else {
String loginUserName = JwtUtils.parseJWT(token).getSubject();
LoginClientBean clientBean = (LoginClientBean) DecisionStatusService.loginStatusService().get(token);
if (clientBean != null) {
String username = clientBean.getUsername();
User user = UserService.getInstance().getUserByUserName(username);
if (user != null && ComparatorUtils.equals(loginUserName, username)) {
if (user.isEnable()) {
return clientBean;
} else {
throw new UserNotAvailableException();
}
} else {
throw new UserPwdErrorException();
}
} else {
throw new LoginInfoNotAvailableException("Login info is null! username: " + loginUserName);
}
}
}
public boolean isLogged(HttpServletRequest req) {
boolean logged = true;
try {
String token = TokenResource.COOKIE.getToken(req);
this.loginStatusValid(token, TerminalHandler.getTerminal(req, NetworkHelper.getDevice(req)));
} catch (Exception var4) {
logged = false;
}
return logged;
}
}

69
src/main/java/com/fr/plugin/MyGlobalRequestFilter.java

@ -0,0 +1,69 @@
package com.fr.plugin;
import com.fr.decision.authority.data.User;
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider;
import com.fr.decision.webservice.utils.DecisionServiceConstants;
import com.fr.decision.webservice.v10.login.LoginService;
import com.fr.decision.webservice.v10.user.UserService;
import com.fr.log.FineLoggerFactory;
import com.fr.stable.StringUtils;
import com.fr.web.utils.WebUtils;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Base64;
public class MyGlobalRequestFilter extends AbstractGlobalRequestFilterProvider {
@Override
public String filterName() {
return "everyThing";
}
@Override
public String[] urlPatterns() {
return new String[]{
"/decision/*"
};
}
@Override
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain filterChain) {
try {
if(!isLogin(req)){
String my_token = req.getParameter("my_token");
if (StringUtils.isNotBlank(my_token)) {
try {
String userName = new String(Base64.getUrlDecoder().decode(my_token));
User user = UserService.getInstance().getUserByUserName(userName);
if (user != null) {
String token = LoginService.getInstance().login(req, res, userName);
req.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME,token);
}else{
FineLoggerFactory.getLogger().error("登录用户不存在:{}",userName);
WebUtils.printAsString(res,"login user not exist :"+userName);
return;
}
} catch (Exception e) {
//登录过程中出现异常
FineLoggerFactory.getLogger().error("登录过程异常:{}",e);
// e.printStackTrace();
}
}
}
filterChain.doFilter(req, res);
} catch (IOException e) {
e.printStackTrace();
} catch (ServletException e) {
e.printStackTrace();
}
}
private boolean isLogin(HttpServletRequest request){
return LoginService.getInstance().isLogged(request);
}
}
Loading…
Cancel
Save