1. 完成新建任务功能,但是未完成静态关键信息功能和动态关键信息功能的关联,需要相关人员沟通
2. 完成新建配置模板功能 3. 修改configuration文件夹中全局异常处理器,添加了几种专门处理数据库异常和Valid异常的处理器。 4. 修改application.yml文件,将hikari自动提交设置为false,此项设置可用于避免数据库发生脏读
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
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 org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/deftac")
|
||||
public class TemplateController {
|
||||
|
||||
private final TemplateService templateService;
|
||||
|
||||
public TemplateController(TemplateService templateService) {
|
||||
this.templateService = templateService;
|
||||
}
|
||||
|
||||
@PostMapping("/new")
|
||||
public ResponseResult newTemplate(@RequestBody @Valid Template template) {
|
||||
Integer templateId;
|
||||
try {
|
||||
templateId = templateService.newTemplate(template);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return new ResponseResult(400, "Illegal Argument in template_elements or default_op")
|
||||
.setData("template_id", null)
|
||||
.setData("success", false);
|
||||
}
|
||||
|
||||
|
||||
if (templateId > 0) {
|
||||
return ResponseResult.ok()
|
||||
.setData("template_id", templateId)
|
||||
.setData("success", true);
|
||||
}
|
||||
|
||||
return ResponseResult.error()
|
||||
.setData("template_id", null)
|
||||
.setData("success", false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.realtime.protection.server.defense.template;
|
||||
|
||||
import com.realtime.protection.configuration.entity.defense.template.Template;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
@Mapper
|
||||
public interface TemplateMapper {
|
||||
|
||||
void newTemplate(@Param("template") Template template);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.realtime.protection.server.defense.template;
|
||||
|
||||
import com.realtime.protection.configuration.entity.defense.template.Template;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@Service
|
||||
public class TemplateService {
|
||||
|
||||
private final TemplateMapper templateMapper;
|
||||
|
||||
private final String[] permittedOps = new String[]{"阻断", "清洗", "篡改", "反制"};
|
||||
|
||||
public TemplateService(TemplateMapper templateMapper) {
|
||||
this.templateMapper = templateMapper;
|
||||
}
|
||||
|
||||
public Integer newTemplate(Template template) throws IllegalArgumentException {
|
||||
if (!Arrays.asList(permittedOps).contains(template.getDefaultOp())) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
for (String choice : template.getTemplateElements()) {
|
||||
switch (choice) {
|
||||
case "防护对象IP" -> template.setHasProtectObjectIP(true);
|
||||
case "防护对象端口" -> template.setHasProtectObjectPort(true);
|
||||
case "对端IP" -> template.setHasPeerIP(true);
|
||||
case "对端端口" -> template.setHasPeerPort(true);
|
||||
case "协议" -> template.setHasProtocol(true);
|
||||
case "URL" -> template.setHasURL(true);
|
||||
case "DNS" -> template.setHasDNS(true);
|
||||
|
||||
default -> throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
templateMapper.newTemplate(template);
|
||||
|
||||
if (template.getTemplateId() == null) {
|
||||
return 0;
|
||||
}
|
||||
return template.getTemplateId();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
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.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/task")
|
||||
public class TaskController {
|
||||
|
||||
private final TaskService taskService;
|
||||
|
||||
public TaskController(TaskService taskService) {
|
||||
this.taskService = taskService;
|
||||
}
|
||||
|
||||
@RequestMapping("/new")
|
||||
public ResponseResult newTask(@RequestBody @Valid Task task) {
|
||||
Integer taskId = taskService.newTask(task);
|
||||
|
||||
if (taskId > 0) {
|
||||
return ResponseResult.ok()
|
||||
.setData("task_name", task.getTaskName())
|
||||
.setData("task_id", taskId)
|
||||
.setData("success", true);
|
||||
}
|
||||
|
||||
return ResponseResult.error()
|
||||
.setData("task_name", task.getTaskName())
|
||||
.setData("task_id", 0)
|
||||
.setData("success", false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.realtime.protection.server.task;
|
||||
|
||||
import com.realtime.protection.configuration.entity.task.Task;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
@Mapper
|
||||
public interface TaskMapper {
|
||||
void newTask(@Param("task") Task task);
|
||||
|
||||
void newTaskProobjConcat(@Param("task_id") Integer taskId, @Param("proobj_ids") Integer[] proobjIds);
|
||||
|
||||
Integer newTaskStaticRuleConcat(@Param("task_id") Integer taskId,
|
||||
@Param("rule_ids") Integer[] staticRuleIds);
|
||||
|
||||
Integer newTaskDynamicRuleConcat(@Param("task_id") Integer taskId,
|
||||
@Param("rule_ids") Integer[] dynamicRuleIds);
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.realtime.protection.server.task;
|
||||
|
||||
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 java.time.LocalDateTime;
|
||||
|
||||
@Service
|
||||
public class TaskService {
|
||||
|
||||
private final SqlSessionFactory sqlSessionFactory;
|
||||
|
||||
public TaskService(SqlSessionFactory sqlSessionFactory) {
|
||||
this.sqlSessionFactory = sqlSessionFactory;
|
||||
}
|
||||
|
||||
public Integer newTask(Task task) {
|
||||
task.setTaskCreateTime(LocalDateTime.now());
|
||||
task.setTaskModifyTime(LocalDateTime.now());
|
||||
|
||||
SqlSession session = sqlSessionFactory.openSession(false);
|
||||
TaskMapper taskMapper = session.getMapper(TaskMapper.class);
|
||||
try {
|
||||
taskMapper.newTask(task);
|
||||
|
||||
taskMapper.newTaskProobjConcat(task.getTaskId(), task.getProtectObjectIds());
|
||||
|
||||
|
||||
// if (taskMapper.newTaskStaticRuleConcat(task.getTaskId(), task.getStaticRuleIds())
|
||||
// != task.getStaticRuleIds().length)
|
||||
// throw new Exception("update lines is not equal to static_rule_ids size");
|
||||
|
||||
// if (taskMapper.newTaskDynamicRuleConcat(task.getTaskId(), task.getDynamicRuleIds())
|
||||
// != task.getDynamicRuleIds().length)
|
||||
// throw new Exception("update lines is not equal to dynamic_rule_ids size");
|
||||
|
||||
session.commit();
|
||||
} catch (Exception e) {
|
||||
session.rollback();
|
||||
throw e;
|
||||
} finally {
|
||||
session.close();
|
||||
}
|
||||
|
||||
if (task.getTaskId() == null) {
|
||||
return 0;
|
||||
}
|
||||
return task.getTaskId();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user