feat: 定时任务添加扩展的白名单配置(降低对若依框架代码的修改)

pull/414/head
潇湘振宇 2022-10-04 22:13:56 +08:00
parent db3e571af0
commit 46e1f99727
2 changed files with 16 additions and 7 deletions

View File

@ -4,6 +4,7 @@ import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions; import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.quartz.SchedulerException; import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap; import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
@ -37,6 +38,9 @@ import com.ruoyi.quartz.util.ScheduleUtils;
public class SysJobController extends BaseController public class SysJobController extends BaseController
{ {
private String prefix = "monitor/job"; private String prefix = "monitor/job";
@Value("${job.whiteList:}")
private List<String> whiteList;
@Autowired @Autowired
private ISysJobService jobService; private ISysJobService jobService;
@ -153,7 +157,7 @@ public class SysJobController extends BaseController
{ {
return error("新增任务'" + job.getJobName() + "'失败,目标字符串存在违规"); return error("新增任务'" + job.getJobName() + "'失败,目标字符串存在违规");
} }
else if (!ScheduleUtils.whiteList(job.getInvokeTarget())) else if (!ScheduleUtils.whiteList(job.getInvokeTarget(), whiteList.toArray(new String[whiteList.size()])))
{ {
return error("新增任务'" + job.getJobName() + "'失败,目标字符串不在白名单内"); return error("新增任务'" + job.getJobName() + "'失败,目标字符串不在白名单内");
} }
@ -201,7 +205,7 @@ public class SysJobController extends BaseController
{ {
return error("修改任务'" + job.getJobName() + "'失败,目标字符串存在违规"); return error("修改任务'" + job.getJobName() + "'失败,目标字符串存在违规");
} }
else if (!ScheduleUtils.whiteList(job.getInvokeTarget())) else if (!ScheduleUtils.whiteList(job.getInvokeTarget(), whiteList.toArray(new String[whiteList.size()])))
{ {
return error("修改任务'" + job.getJobName() + "'失败,目标字符串不在白名单内"); return error("修改任务'" + job.getJobName() + "'失败,目标字符串不在白名单内");
} }

View File

@ -1,5 +1,6 @@
package com.ruoyi.quartz.util; package com.ruoyi.quartz.util;
import org.apache.commons.lang3.ArrayUtils;
import org.quartz.CronScheduleBuilder; import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger; import org.quartz.CronTrigger;
import org.quartz.Job; import org.quartz.Job;
@ -123,17 +124,21 @@ public class ScheduleUtils
* *
* *
* @param invokeTarget * @param invokeTarget
* @param extendedWhiteList
* @return * @return
*/ */
public static boolean whiteList(String invokeTarget) public static boolean whiteList(String invokeTarget, String... extendedWhiteList)
{ {
String[] whiteList = StringUtils.isEmpty(extendedWhiteList) ? Constants.JOB_WHITELIST_STR
: ArrayUtils.addAll(extendedWhiteList, Constants.JOB_WHITELIST_STR);
String packageName = StringUtils.substringBefore(invokeTarget, "("); String packageName = StringUtils.substringBefore(invokeTarget, "(");
int count = StringUtils.countMatches(packageName, "."); int count = StringUtils.countMatches(packageName, ".");
if (count > 1) if (count > 1)
{ {
return StringUtils.containsAnyIgnoreCase(invokeTarget, Constants.JOB_WHITELIST_STR); return StringUtils.containsAnyIgnoreCase(invokeTarget, whiteList);
} }
Object obj = SpringUtils.getBean(StringUtils.split(invokeTarget, ".")[0]); Object obj = SpringUtils.getBean(StringUtils.split(invokeTarget, ".")[0]);
return StringUtils.containsAnyIgnoreCase(obj.getClass().getPackage().getName(), Constants.JOB_WHITELIST_STR); return StringUtils.containsAnyIgnoreCase(obj.getClass().getPackage().getName(), whiteList);
} }
} }