阿沐 2022-11-08 10:54:00 +08:00
parent 77a9db8c8f
commit 446eb72943
2 changed files with 31 additions and 13 deletions

View File

@ -1,8 +1,11 @@
package net.maku.quartz.controller; package net.maku.quartz.controller;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.extra.spring.SpringUtil;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import net.maku.framework.common.exception.ServerException;
import net.maku.framework.common.page.PageResult; import net.maku.framework.common.page.PageResult;
import net.maku.framework.common.utils.Result; import net.maku.framework.common.utils.Result;
import net.maku.quartz.convert.ScheduleJobConvert; import net.maku.quartz.convert.ScheduleJobConvert;
@ -12,6 +15,7 @@ import net.maku.quartz.service.ScheduleJobService;
import net.maku.quartz.utils.CronUtils; import net.maku.quartz.utils.CronUtils;
import net.maku.quartz.vo.ScheduleJobVO; import net.maku.quartz.vo.ScheduleJobVO;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.validation.Valid; import javax.validation.Valid;
@ -55,6 +59,9 @@ public class ScheduleJobController {
return Result.error("操作失败Cron表达式不正确"); return Result.error("操作失败Cron表达式不正确");
} }
// 检查Bean的合法性
checkBean(vo.getBeanName());
scheduleJobService.save(vo); scheduleJobService.save(vo);
return Result.ok(); return Result.ok();
@ -68,6 +75,9 @@ public class ScheduleJobController {
return Result.error("操作失败Cron表达式不正确"); return Result.error("操作失败Cron表达式不正确");
} }
// 检查Bean的合法性
checkBean(vo.getBeanName());
scheduleJobService.update(vo); scheduleJobService.update(vo);
return Result.ok(); return Result.ok();
@ -99,4 +109,12 @@ public class ScheduleJobController {
return Result.ok(); return Result.ok();
} }
private void checkBean(String beanName) {
// 为避免执行jdbcTemplate等类只允许添加有@Service注解的Bean
String[] serviceBeans = SpringUtil.getApplicationContext().getBeanNamesForAnnotation(Service.class);
if (!ArrayUtil.contains(serviceBeans, beanName)) {
throw new ServerException("只允许添加有@Service注解的Bean");
}
}
} }

View File

@ -1,7 +1,7 @@
package net.maku.quartz.task; package net.maku.quartz.task;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Service;
/** /**
* 测试定时任务 * 测试定时任务
@ -9,7 +9,7 @@ import org.springframework.stereotype.Component;
* @author 阿沐 babamu@126.com * @author 阿沐 babamu@126.com
*/ */
@Slf4j @Slf4j
@Component @Service
public class TestTask { public class TestTask {
public void run(String params) throws InterruptedException { public void run(String params) throws InterruptedException {