Browse Source

提交demo代码

10.0
hugh 3 years ago
parent
commit
5c812f6102
  1. 20
      README.md
  2. 126
      build.gradle
  3. 13
      encrypt.xml
  4. BIN
      lib/common-kit-10.0-20210309.jar
  5. BIN
      lib/finekit-10.0-20210309.jar
  6. 16
      plugin.xml
  7. 46
      src/main/java/com/tptj/demo/hg/http/handler/AdminHttpHandler.java
  8. 25
      src/main/java/com/tptj/demo/hg/http/handler/Demo.java
  9. 48
      src/main/java/com/tptj/demo/hg/http/handler/ModulesHttpHandler.java
  10. 37
      src/main/java/com/tptj/demo/hg/http/handler/PublicHttpHandler.java
  11. 27
      src/main/java/com/tptj/demo/hg/http/handler/TestRequestBean.java
  12. 29
      src/main/java/com/tptj/demo/hg/http/handler/TestResponseBean.java
  13. 55
      src/main/java/com/tptj/demo/hg/http/handler/UserHttpHandler.java

20
README.md

@ -1,3 +1,21 @@
# demo-http-handler
10.0请求接口demo
10.0请求接口demo\
demo生效后访问\
GET http://localhost:8075/webroot/decision/plugin/public/com.tptj.demo.hg.http.handler.v10/demo/public \
响应:Hello World \
\
GET http://localhost:8075/webroot/decision/plugin/private/com.tptj.demo.hg.http.handler.v10/demo/admin \
body raw = {"key":"hello","value":"测试"}\
header: Authorization=Bearer $Fine_Auth_Token (注:超管的token)\
响应: {"key":"hello","value":"测试"}\
\
GET http://localhost:8075/webroot/decision/plugin/private/com.tptj.demo.hg.http.handler.v10/demo/module?key=哈哈哈 \
header: Authorization=Bearer $Fine_Auth_Token (注:有用户管理权限的用户token)\
响应: 哈哈哈\
\
http://localhost:8075/webroot/decision/plugin/private/com.tptj.demo.hg.http.handler.v10/demo/user \
header: Authorization=Bearer $Fine_Auth_Token (注:用户名为Anna的token)\
响应: {"time":"当前时间","msg":"Hello Anna!"}}\
\
对于需要token的请求,如果没有正确的token,则会返回登录页面或提示无权限的页面

126
build.gradle

@ -0,0 +1,126 @@
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'])
implementation project(':kit-fine')
implementation project(':kit-common')
}

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>

BIN
lib/common-kit-10.0-20210309.jar

Binary file not shown.

BIN
lib/finekit-10.0-20210309.jar

Binary file not shown.

16
plugin.xml

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?><plugin>
<id>com.tptj.demo.hg.http.handler.v10</id>
<name><![CDATA[ http请求 ]]></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.http.handler</main-package>
<function-recorder class="com.tptj.demo.hg.http.handler.Demo"/>
<extra-decision>
<HttpHandlerProvider class="com.tptj.demo.hg.http.handler.Demo"/>
</extra-decision>
</plugin>

46
src/main/java/com/tptj/demo/hg/http/handler/AdminHttpHandler.java

@ -0,0 +1,46 @@
package com.tptj.demo.hg.http.handler;
import com.fanruan.api.web.ResponseKit;
import com.fr.decision.fun.impl.BaseHttpHandler;
import com.fr.third.springframework.web.bind.annotation.RequestMethod;
import com.tptj.api.start.RequestKit;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author 秃破天际
* @version 10.0
* Created by 秃破天际 on 2021-03-22
* 只有超管可以访问的接口实现
* http://localhost:8075/webroot/decision/plugin/private/com.tptj.demo.hg.http.handler.v10/demo/admin
* body = {"key":"hello","value":"测试"}
* header: Authorization=Bearer $Fine_Auth_Token
**/
public class AdminHttpHandler extends BaseHttpHandler {
@Override
public RequestMethod getMethod() {
return RequestMethod.GET;
}
@Override
public String getPath() {
return "/demo/admin";
}
@Override
public boolean isPublic() {
return false;
}
@Override
public boolean needAdmin() {
return true;
}
@Override
public void handle(HttpServletRequest request, HttpServletResponse response) throws Exception {
TestRequestBean bean = RequestKit.readObject(request,TestRequestBean.class);
RequestKit.print(request,response, ResponseKit.ok(bean));
}
}

25
src/main/java/com/tptj/demo/hg/http/handler/Demo.java

@ -0,0 +1,25 @@
package com.tptj.demo.hg.http.handler;
import com.fr.decision.fun.HttpHandler;
import com.fr.decision.fun.impl.AbstractHttpHandlerProvider;
import com.fr.intelli.record.Focus;
import com.fr.record.analyzer.EnableMetrics;
/**
* @author 秃破天际
* @version 10.0
* Created by 秃破天际 on 2021-03-22
**/
@EnableMetrics
public class Demo extends AbstractHttpHandlerProvider {
@Override
@Focus(id = "com.tptj.demo.hg.http.handler.v10",text = "http请求")
public HttpHandler[] registerHandlers() {
return new HttpHandler[]{
new PublicHttpHandler(),
new AdminHttpHandler(),
new ModulesHttpHandler(),
new UserHttpHandler()
};
}
}

48
src/main/java/com/tptj/demo/hg/http/handler/ModulesHttpHandler.java

@ -0,0 +1,48 @@
package com.tptj.demo.hg.http.handler;
import com.fanruan.api.decision.AuthorityKit;
import com.fanruan.api.net.NetworkKit;
import com.fanruan.api.web.FlushKit;
import com.fr.decision.fun.impl.BaseHttpHandler;
import com.fr.third.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author 秃破天际
* @version 10.0
* Created by 秃破天际 on 2021-03-22
* 基于平台模块权限的接口实现
* http://localhost:8075/webroot/decision/plugin/private/com.tptj.demo.hg.http.handler.v10/demo/module?key=哈哈哈
**/
public class ModulesHttpHandler extends BaseHttpHandler {
@Override
public RequestMethod getMethod() {
return RequestMethod.GET;
}
@Override
public String getPath() {
return "/demo/module";
}
@Override
public boolean isPublic() {
return false;
}
@Override
public String[] modules(){
//hugh:仅仅当前用户具有用户管理模块的权限时,才能调用这个请求
return new String[]{
AuthorityKit.Management.USER
};
}
@Override
public void handle(HttpServletRequest request, HttpServletResponse response) throws Exception {
String key = NetworkKit.getHTTPRequestParameter(request,"key");
FlushKit.printAsString(response,key);
}
}

37
src/main/java/com/tptj/demo/hg/http/handler/PublicHttpHandler.java

@ -0,0 +1,37 @@
package com.tptj.demo.hg.http.handler;
import com.fanruan.api.web.FlushKit;
import com.fr.decision.fun.impl.BaseHttpHandler;
import com.fr.third.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author 秃破天际
* @version 10.0
* Created by 秃破天际 on 2021-03-22
* 不需要鉴权的接口实现
* http://localhost:8075/webroot/decision/plugin/public/com.tptj.demo.hg.http.handler.v10/demo/public
**/
public class PublicHttpHandler extends BaseHttpHandler {
@Override
public RequestMethod getMethod() {
return RequestMethod.GET;
}
@Override
public String getPath() {
return "/demo/public";
}
@Override
public boolean isPublic() {
return true;
}
@Override
public void handle( HttpServletRequest request, HttpServletResponse response ) throws Exception {
FlushKit.printAsString( response, "Hello World!" );
}
}

27
src/main/java/com/tptj/demo/hg/http/handler/TestRequestBean.java

@ -0,0 +1,27 @@
package com.tptj.demo.hg.http.handler;
/**
* @author 秃破天际
* @version 10.0
* Created by 秃破天际 on 2021-03-22
**/
public class TestRequestBean {
private String key;
private String value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

29
src/main/java/com/tptj/demo/hg/http/handler/TestResponseBean.java

@ -0,0 +1,29 @@
package com.tptj.demo.hg.http.handler;
import java.util.Date;
/**
* @author 秃破天际
* @version 10.0
* Created by 秃破天际 on 2021-03-22
**/
public class TestResponseBean {
private Date time;
private String msg;
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}

55
src/main/java/com/tptj/demo/hg/http/handler/UserHttpHandler.java

@ -0,0 +1,55 @@
package com.tptj.demo.hg.http.handler;
import com.fanruan.api.decision.user.UserKit;
import com.fr.decision.fun.impl.BaseHttpHandler;
import com.fr.third.springframework.web.bind.annotation.RequestMethod;
import com.tptj.api.start.RequestKit;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
/**
* @author 秃破天际
* @version 10.0
* Created by 秃破天际 on 2021-03-22
* 根据用户名判断权限
* http://localhost:8075/webroot/decision/plugin/private/com.tptj.demo.hg.http.handler.v10/demo/user
* header: Authorization=Bearer $Fine_Auth_Token
**/
public class UserHttpHandler extends BaseHttpHandler {
@Override
public RequestMethod getMethod() {
return RequestMethod.GET;
}
@Override
public String getPath() {
return "/demo/user";
}
@Override
public boolean isPublic() {
return false;
}
public boolean accessControl(String userId) {
//hugh:这个方法的本意是如果超管权限和模块权限控制不够使用时,可以使用这个方法进行权限控制的扩展
//入参只有一个userId,我们也可以通过下面的方法,获取到当前请求对象,通过请求的其他信息控制权限
try{
HttpServletRequest request = RequestKit.loadRequest();
//UserService.getInstance().getUser(userId).getUsername()
return "Anna".equals(UserKit.getUserByRequest(request).getUserName());
}catch(Exception e){
return false;
}
}
@Override
public void handle( HttpServletRequest request, HttpServletResponse response ) throws Exception {
TestResponseBean data = new TestResponseBean();
data.setTime(new Date());
data.setMsg("Hello Anna!");
RequestKit.print(request,response,data);
}
}
Loading…
Cancel
Save