1. 添加HandlerMethodValidationException全局异常器
2. 新增防护对象类,添加Service、Mapper、Controller(Controller仍然在开发中) 3. page和pageSize添加@Min注解,限定最低整数大小 4. 将所有的批量类型方法修改为forEach,在SpringBoot中循环执行并整合为事务
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
package com.realtime.protection.server.defense.object;
|
||||
|
||||
public class ProtectObjectController {
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.realtime.protection.server.defense.object;
|
||||
|
||||
import com.realtime.protection.configuration.entity.defense.object.ProtectObject;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface ProtectObjectMapper {
|
||||
void newProtectObject(@Param("proobj") ProtectObject protectObject);
|
||||
|
||||
List<ProtectObject> queryProtectObjects(@Param("proobj_name") String protectObjectName,
|
||||
@Param("proobj_id") Integer protectObjectId,
|
||||
@Param("page") Integer page,
|
||||
@Param("page_size") Integer pageSize);
|
||||
|
||||
ProtectObject queryProtectObject(@Param("proobj_id") Integer protectObjectId);
|
||||
|
||||
Boolean updateProtectObject(@Param("proobj") ProtectObject protectObject);
|
||||
|
||||
Boolean deleteProtectObject(@Param("proobj_id") Integer protectObjectId);
|
||||
|
||||
Boolean changeProtectObjectAuditStatus(@Param("proobj_audit_status") Integer protectObjectAuditStatus);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.realtime.protection.server.defense.object;
|
||||
|
||||
import com.realtime.protection.configuration.entity.defense.object.ProtectObject;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class ProtectObjectService {
|
||||
private final ProtectObjectMapper protectObjectMapper;
|
||||
|
||||
public ProtectObjectService(ProtectObjectMapper protectObjectMapper) {
|
||||
this.protectObjectMapper = protectObjectMapper;
|
||||
}
|
||||
|
||||
public Integer newProtectObject(ProtectObject protectObject) {
|
||||
protectObjectMapper.newProtectObject(protectObject);
|
||||
|
||||
if (protectObject.getProtectObjectId() == null) {
|
||||
return 0;
|
||||
}
|
||||
return protectObject.getProtectObjectId();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<Integer> newProtectObjects(List<ProtectObject> protectObjectList) {
|
||||
protectObjectList.forEach(protectObjectMapper::newProtectObject);
|
||||
return protectObjectList.stream().map(ProtectObject::getProtectObjectId).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<ProtectObject> queryProtectObjects(String protectObjectName, Integer protectObjectId, Integer page, Integer pageSize) {
|
||||
return protectObjectMapper.queryProtectObjects(protectObjectName, protectObjectId, page, pageSize);
|
||||
}
|
||||
|
||||
public ProtectObject queryProtectObject(Integer protectObjectId) {
|
||||
return protectObjectMapper.queryProtectObject(protectObjectId);
|
||||
}
|
||||
|
||||
public Boolean updateProtectObject(ProtectObject protectObject) {
|
||||
return protectObjectMapper.updateProtectObject(protectObject);
|
||||
}
|
||||
|
||||
public Boolean deleteProtectObject(Integer protectObjectId) {
|
||||
return protectObjectMapper.deleteProtectObject(protectObjectId);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Boolean deleteProtectObjects(List<Integer> protectObjectIds) {
|
||||
return protectObjectIds.stream().allMatch(protectObjectMapper::deleteProtectObject);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.realtime.protection.server.defense.template;
|
||||
import com.realtime.protection.configuration.entity.defense.template.Template;
|
||||
import com.realtime.protection.configuration.response.ResponseResult;
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
@@ -41,16 +42,32 @@ public class TemplateController {
|
||||
}
|
||||
|
||||
@GetMapping("/query")
|
||||
public ResponseResult queryTemplate(@RequestParam(value = "template_name", required = false) String templateName,
|
||||
@RequestParam("page") Integer page,
|
||||
@RequestParam("page_size") Integer pageSize) {
|
||||
if (page <= 0 || pageSize <= 0) {
|
||||
return ResponseResult.invalid()
|
||||
.setData("template_list", null);
|
||||
}
|
||||
public ResponseResult queryTemplates(@RequestParam(value = "template_name", required = false) String templateName,
|
||||
@RequestParam("page") @Min(1) Integer page,
|
||||
@RequestParam("page_size") @Min(1) Integer pageSize) {
|
||||
List<Template> templates = templateService.queryTemplates(templateName, page, pageSize);
|
||||
|
||||
return ResponseResult.ok()
|
||||
.setData("templates", templates);
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/update")
|
||||
public ResponseResult updateTemplate(@PathVariable("id") @Min(1) Integer templateId,
|
||||
@RequestBody @Valid Template template) {
|
||||
Boolean success = templateService.updateTemplate(templateId, template);
|
||||
return ResponseResult.ok()
|
||||
.setData("template_id", templateId)
|
||||
.setData("success", success);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}/addUsedTimes")
|
||||
public ResponseResult addTemplateUsedTimes(@PathVariable("id") @Min(1) Integer templateId,
|
||||
@RequestParam(value = "add_num") @Min(0) Integer addNum) {
|
||||
Boolean success = templateService.addTemplateUsedTimes(templateId, addNum);
|
||||
return ResponseResult.ok()
|
||||
.setData("template_id", templateId)
|
||||
.setData("success", success);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -13,4 +13,10 @@ public interface TemplateMapper {
|
||||
List<Template> queryTemplates(@Param("template_name") String templateName,
|
||||
@Param("page") Integer page,
|
||||
@Param("page_size") Integer pageSize);
|
||||
|
||||
Boolean updateTemplateInformation(@Param("template") Template template);
|
||||
|
||||
void countTemplateRunningTasks(@Param("template_id") Integer templateId);
|
||||
|
||||
Boolean addTemplateUsedTimes(@Param("template_id") Integer templateId, @Param("add_times") Integer addTimes);
|
||||
}
|
||||
|
||||
@@ -22,6 +22,28 @@ public class TemplateService {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
setTemplateElements(template);
|
||||
|
||||
templateMapper.newTemplate(template);
|
||||
|
||||
if (template.getTemplateId() == null) {
|
||||
return 0;
|
||||
}
|
||||
return template.getTemplateId();
|
||||
}
|
||||
|
||||
public List<Template> queryTemplates(String templateName, Integer page, Integer pageSize) {
|
||||
return templateMapper.queryTemplates(templateName, page, pageSize);
|
||||
}
|
||||
|
||||
public Boolean updateTemplate(Integer templateId, Template template) {
|
||||
setTemplateElements(template);
|
||||
template.setTemplateId(templateId);
|
||||
|
||||
return templateMapper.updateTemplateInformation(template);
|
||||
}
|
||||
|
||||
private void setTemplateElements(Template template) {
|
||||
for (String choice : template.getTemplateElements()) {
|
||||
switch (choice) {
|
||||
case "防护对象IP" -> template.setHasProtectObjectIP(true);
|
||||
@@ -35,16 +57,10 @@ public class TemplateService {
|
||||
default -> throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
templateMapper.newTemplate(template);
|
||||
|
||||
if (template.getTemplateId() == null) {
|
||||
return 0;
|
||||
}
|
||||
return template.getTemplateId();
|
||||
}
|
||||
|
||||
public List<Template> queryTemplates(String templateName, Integer page, Integer pageSize) {
|
||||
return templateMapper.queryTemplates(templateName, page, pageSize);
|
||||
public Boolean addTemplateUsedTimes(Integer templateId, Integer addTimes) {
|
||||
return templateMapper.addTemplateUsedTimes(templateId, addTimes);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.realtime.protection.server.task;
|
||||
import com.realtime.protection.configuration.entity.task.Task;
|
||||
import com.realtime.protection.configuration.response.ResponseResult;
|
||||
import jakarta.validation.Valid;
|
||||
import org.apache.coyote.Response;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
@@ -40,19 +40,15 @@ public class TaskController {
|
||||
@RequestParam(value = "task_type", required = false) String taskType,
|
||||
@RequestParam(value = "task_name", required = false) String taskName,
|
||||
@RequestParam(value = "task_creator", required = false) String taskCreator,
|
||||
@RequestParam("page") Integer page,
|
||||
@RequestParam("page_size") Integer pageSize) {
|
||||
if (page <= 0 || pageSize <= 0) {
|
||||
return new ResponseResult(400, "page or page_size is invalid")
|
||||
.setData("task_list", null);
|
||||
}
|
||||
@RequestParam("page") @Min(1) Integer page,
|
||||
@RequestParam("page_size") @Min(1) Integer pageSize) {
|
||||
List<Task> tasks = taskService.queryTasks(taskStatus, taskType, taskName, taskCreator, page, pageSize);
|
||||
return ResponseResult.ok()
|
||||
.setData("task_list", tasks);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}/query")
|
||||
public ResponseResult queryTask(@PathVariable("id") Integer id) {
|
||||
public ResponseResult queryTask(@PathVariable("id") @Min(1) Integer id) {
|
||||
Task task = taskService.queryTask(id);
|
||||
|
||||
if (task == null) {
|
||||
@@ -74,7 +70,7 @@ public class TaskController {
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/update")
|
||||
public ResponseResult updateTask(@PathVariable("id") Integer taskId, @RequestBody Task task) {
|
||||
public ResponseResult updateTask(@PathVariable("id") @Min(1) Integer taskId, @RequestBody @Valid Task task) {
|
||||
task.setTaskId(taskId);
|
||||
taskService.updateTask(task);
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import java.util.List;
|
||||
public interface TaskMapper {
|
||||
void newTask(@Param("task") Task task);
|
||||
|
||||
void newTaskProtectObjectConcat(@Param("task_id") Integer taskId, @Param("proobj_ids") List<Integer> proobjIds);
|
||||
void newTaskProtectObjectConcat(@Param("task_id") Integer taskId, @Param("proobj_id") Integer proobjId);
|
||||
|
||||
void newTaskStaticRuleConcat(@Param("task_id") Integer taskId,
|
||||
@Param("rule_ids") List<Integer> staticRuleIds);
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.realtime.protection.configuration.entity.task.Task;
|
||||
import org.apache.ibatis.session.SqlSession;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -24,9 +25,11 @@ public class TaskService {
|
||||
try {
|
||||
taskMapper.newTask(task);
|
||||
|
||||
taskMapper.newTaskProtectObjectConcat(task.getTaskId(), task.getProtectObjectIds());
|
||||
task.getProtectObjectIds().forEach(
|
||||
proobjId -> taskMapper.newTaskProtectObjectConcat(task.getTaskId(), proobjId));
|
||||
|
||||
taskMapper.newTaskStaticRuleConcat(task.getTaskId(), task.getStaticRuleIds());
|
||||
// taskMapper.newTaskDynamicRuleConcat(task.getTaskId(), task.getDynamicRuleIds());
|
||||
|
||||
session.commit();
|
||||
} catch (Exception e) {
|
||||
@@ -73,34 +76,17 @@ public class TaskService {
|
||||
return task;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void updateTask(Task task) {
|
||||
SqlSession session = sqlSessionFactory.openSession(false);
|
||||
TaskMapper taskMapper = session.getMapper(TaskMapper.class);
|
||||
try {
|
||||
taskMapper.updateTask(task);
|
||||
taskMapper.updateTask(task);
|
||||
|
||||
taskMapper.clearTaskProtectObjectConcat(task.getTaskId());
|
||||
taskMapper.clearTaskConnectedStaticRule(task.getTaskId());
|
||||
// taskMapper.clearTaskConnectedDynamicRule(task.getTaskId());
|
||||
taskMapper.clearTaskProtectObjectConcat(task.getTaskId());
|
||||
taskMapper.clearTaskConnectedStaticRule(task.getTaskId());
|
||||
taskMapper.clearTaskConnectedDynamicRule(task.getTaskId());
|
||||
|
||||
if (task.getProtectObjectIds() != null && !task.getProtectObjectIds().isEmpty()) {
|
||||
taskMapper.newTaskProtectObjectConcat(task.getTaskId(), task.getProtectObjectIds());
|
||||
}
|
||||
|
||||
if (task.getStaticRuleIds() != null && !task.getStaticRuleIds().isEmpty()) {
|
||||
taskMapper.newTaskStaticRuleConcat(task.getTaskId(), task.getStaticRuleIds());
|
||||
}
|
||||
|
||||
if (task.getDynamicRuleIds() != null && !task.getDynamicRuleIds().isEmpty()) {
|
||||
taskMapper.newTaskDynamicRuleConcat(task.getTaskId(), task.getDynamicRuleIds());
|
||||
}
|
||||
|
||||
session.commit();
|
||||
} catch (Exception e) {
|
||||
session.rollback();
|
||||
throw e;
|
||||
} finally {
|
||||
session.close();
|
||||
}
|
||||
task.getProtectObjectIds().forEach(
|
||||
proobjId -> taskMapper.newTaskProtectObjectConcat(task.getTaskId(), proobjId));
|
||||
taskMapper.newTaskStaticRuleConcat(task.getTaskId(), task.getStaticRuleIds());
|
||||
taskMapper.newTaskDynamicRuleConcat(task.getTaskId(), task.getDynamicRuleIds());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user