QLExpress在脚本中执行Java代码 原创 2022-12-09 11:06:48.0 阅读(2529)次 QLExpress可以执行java代码,但要注意控制可执行的代码范围开启代码黑名单或白名单,否则容易出现被利用。 开启白名单示例代码如下: 举个例子,我要替换掉有mt开头的字符 ``` public static void main(String args[]) throws Exception { QLExpressRunStrategy.setForbidInvokeSecurityRiskMethods(true); QLExpressRunStrategy.addSecureMethod(CommonUtil.class, "startWith"); QLExpressRunStrategy.addSecureMethod(CommonUtil.class, "subStrNoStart"); ExpressRunner expressRunner = new ExpressRunner(); String expressValue = "import com.jary.mytools.server.utils.CommonUtil;if(CommonUtil.startWith(a,'mt_'))then{return CommonUtil.subStrNoStart(a,'mt_');} else if(CommonUtil.startWith(a,'t_'))then{return CommonUtil.subStrNoStart(a,'t_');} else {return a;}"; DefaultContext context = new DefaultContext(); context.put("a","mt_user_info"); Object result = expressRunner.execute(expressValue, context, null, true, false); String value = result.toString(); System.out.println(value); } ``` 输出:user_info maven引入: ```xml com.alibaba QLExpress 3.3.0 ``` CommonUtil的代码如下: ``` package com.jary.mytools.server.utils; import java.util.*; public class CommonUtil { public static boolean startWith(String string,String startWith) { return string.startsWith(startWith); } public static boolean endWith(String string,String endWith) { return string.startsWith(endWith); } public static String subStrNoStart(String string,String startWith) { return string.replaceFirst(startWith,""); } public static String subStrNoEnd(String string,String endWith) { int index = string.lastIndexOf(endWith); return string.substring(0,index); } } ``` java 上一篇:QLExpress if判断以及多个if else如何写 下一篇:java Map通过遍历器Iterator来实现遍历过程中删除元素来避免ConcurrentModificationException异常
相关文章 Jep实现Java表达式计算(2057) 阿里巴巴p3c代码规范eclipse插件(1554) java使用snakeyaml库读取和操作yml文件(2496) java通过域名获取域名的IP地址(1520) jmeter随机日期参数(1128) 线程安全测试 ArrayList Collections.synchronizedList CopyOnWriteArrayList(1514) ThreadLocal正确使用姿势,避免再掉坑(1214) java解析request或url中的参数工具类代码(2919) Base64编码出现换行符(2606) Java实现Https发送get和post请求(2738) 推荐文章 使用spring4实现websocket连接(1) Parameter index out of range (1 > number of parameters, which is 0(7) spring cloud+feign+mybatis中使用seata0.9实现分布式事务(7) spring cloud gateway报错Only one connection receive subscriber allowed(82) spring cloud中Feign调用诡异报错MethodNotAllowed: status 405 reading(116) elasticsearch7.1保存时报错: Validation Failed: 1: type is missing;(7) 聊聊数据保存到MySQL后数据乱码的问题(1) jquery对象与dom对象互转(1) linux使用epel源yum安装iftop、nload、nginx等(2) linux下nginx安装其他模块(1) 热门文章 java stream去重的几种方式(40819) the dependencies of some of the beans in the application context form a cycle(16324) 解决mybatis打印查询结果集造成太多日志的问题(9839) java enum枚举转list和Map(9722) java stream List转Map与List转List与Map转List以及List转Map(8468) ServletRequest转HttpServletRequest设置header之后取不到header的问题(8434) java中BufferedImage转成 base64字符串(8098) 切分List集合为多个List集合(7385) maven修改jar包版本不生效解决办法(6990) bootstrap.yml配置报错:Could not resolve placeholder 'xx' in value (6949) 标签列表 java java java java java java java基础 微服务 异常处理 mysql clickhouse clickhouse clickhouse clickhouse clickhouse spring cloud spring boot linux elasticsearch feign jdbc spring js docker postgresql solr seata nginx maven gateway hsqldb 数据库 架构 大数据分析 分布式事务 redis canal dubbo hadoop 消息队列 win10 websocket springmvc git html select2 mybatis jenkins rocketmq quartz activemq 数据库集群 ajax bat 电脑 笔记 eclipse 设计模式 阿里云 github freemarker jvm jquery javamail redission redission对象 hystrix http hibernate springmail svn ubuntu ueditor xheditor zookeeper 分布式 小程序 开发工具 gitlab