1. 增加jackson配置,缩减json数据长度

2. ExceptionHandler添加SaTokenException检查,用于校验登陆
3. ResponseResult添加invalid和unauthorized静态方法
4. Task模块添加单查询,多查询,更新路由
5. Template添加两个JsonProperty
6. Template模块添加query路由
7.
This commit is contained in:
松岳 陈
2024-01-03 22:53:02 +08:00
parent b0c1700bd3
commit 06886de328
16 changed files with 342 additions and 73 deletions

View File

@@ -1,5 +1,6 @@
package com.realtime.protection;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

View File

@@ -20,17 +20,23 @@ public class Template {
@NotNull(message = "default_op should not be empty")
private String defaultOp;
private boolean hasProtectObjectIP;
@JsonProperty("template_running_tasks")
private Integer templateRunningTasks;
private boolean hasProtectObjectPort;
@JsonProperty("template_used")
private Integer templateUsedTimes;
private boolean hasPeerIP;
private Boolean hasProtectObjectIP;
private boolean hasPeerPort;
private Boolean hasProtectObjectPort;
private boolean hasProtocol;
private Boolean hasPeerIP;
private boolean hasURL;
private Boolean hasPeerPort;
private boolean hasDNS;
private Boolean hasProtocol;
private Boolean hasURL;
private Boolean hasDNS;
}

View File

@@ -5,6 +5,7 @@ import jakarta.validation.constraints.NotNull;
import lombok.Data;
import java.time.LocalDateTime;
import java.util.List;
@Data
public class Task {
@@ -53,13 +54,13 @@ public class Task {
// -----------------------------------------------------------
@JsonProperty("static_rule_ids")
private Integer[] staticRuleIds;
private List<Integer> staticRuleIds;
@JsonProperty("dynamic_rule_ids")
private Integer[] dynamicRuleIds;
private List<Integer> dynamicRuleIds;
@JsonProperty("protect_object_ids")
private Integer[] protectObjectIds;
private List<Integer> protectObjectIds;
@JsonProperty("task_status")
private Integer taskStatus;

View File

@@ -1,7 +1,9 @@
package com.realtime.protection.configuration.exception;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.SaTokenException;
import com.realtime.protection.configuration.response.ResponseResult;
import io.swagger.v3.oas.annotations.ExternalDocumentation;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.core.annotation.Order;
@@ -24,26 +26,30 @@ public class GlobalExceptionHandler {
@ExceptionHandler(value = NotLoginException.class)
public ResponseResult handleNotLoginException(NotLoginException e) {
return new ResponseResult(
400,
401,
e.getMessage()
);
}
@Order(2)
@ExceptionHandler(value = PersistenceException.class)
public ResponseResult handleSQLException() {
return new ResponseResult(
400,
public ResponseResult handleSQLException(PersistenceException e) {
return ResponseResult.invalid().setMessage(
"please check the integrity of the data. check if the json data exists in the database");
}
@Order(2)
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public ResponseResult handleBindException(MethodArgumentNotValidException e) {
return new ResponseResult(
400,
return ResponseResult.invalid().setMessage(
e.getBindingResult().getAllErrors().stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining())
);
}
@Order(2)
@ExceptionHandler(value = SaTokenException.class)
public ResponseResult handleSaTokenException(SaTokenException e) {
return ResponseResult.unAuthorized().setMessage(e.getMessage());
}
}

View File

@@ -30,7 +30,7 @@ public class ResponseResult implements Serializable {
}
public static ResponseResult ok() {
return new ResponseResult(200, "request succeeded");
return new ResponseResult(200, "request succeed");
}
public static ResponseResult ok(String message) {
@@ -41,6 +41,14 @@ public class ResponseResult implements Serializable {
return new ResponseResult(500, "request failed");
}
public static ResponseResult invalid() {
return new ResponseResult(400, "invalid request");
}
public static ResponseResult unAuthorized() {
return new ResponseResult(401, "UnAuthorized User");
}
public static ResponseResult error(String message) {
return new ResponseResult(500, message);
}

View File

@@ -3,10 +3,9 @@ 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;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/deftac")
@@ -40,4 +39,18 @@ public class TemplateController {
.setData("template_id", null)
.setData("success", false);
}
@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);
}
List<Template> templates = templateService.queryTemplates(templateName, page, pageSize);
return ResponseResult.ok()
.setData("templates", templates);
}
}

View File

@@ -4,8 +4,13 @@ import com.realtime.protection.configuration.entity.defense.template.Template;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface TemplateMapper {
void newTemplate(@Param("template") Template template);
List<Template> queryTemplates(@Param("template_name") String templateName,
@Param("page") Integer page,
@Param("page_size") Integer pageSize);
}

View File

@@ -4,6 +4,7 @@ import com.realtime.protection.configuration.entity.defense.template.Template;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
@Service
public class TemplateService {
@@ -42,4 +43,8 @@ public class TemplateService {
}
return template.getTemplateId();
}
public List<Template> queryTemplates(String templateName, Integer page, Integer pageSize) {
return templateMapper.queryTemplates(templateName, page, pageSize);
}
}

View File

@@ -3,9 +3,10 @@ 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;
import org.apache.coyote.Response;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/task")
@@ -17,7 +18,7 @@ public class TaskController {
this.taskService = taskService;
}
@RequestMapping("/new")
@PostMapping("/new")
public ResponseResult newTask(@RequestBody @Valid Task task) {
Integer taskId = taskService.newTask(task);
@@ -33,4 +34,52 @@ public class TaskController {
.setData("task_id", 0)
.setData("success", false);
}
@GetMapping("/query")
public ResponseResult queryTasks(@RequestParam(value = "task_status", required = false) Integer taskStatus,
@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);
}
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) {
Task task = taskService.queryTask(id);
if (task == null) {
return ResponseResult.invalid().setMessage("Task ID is invalid");
}
return ResponseResult.ok()
.setData("task_id", task.getTaskId())
.setData("task_name", task.getTaskName())
.setData("task_type", task.getTaskType())
.setData("task_status", task.getTaskStatus())
.setData("task_creator", task.getTaskCreateUsername())
.setData("task_creator_depart", task.getTaskCreateDepart())
.setData("task_start_time", task.getTaskStartTime())
.setData("task_end_time", task.getTaskEndTime())
.setData("task_static_rule_ids", task.getStaticRuleIds())
.setData("task_dynamic_rule_ids", task.getDynamicRuleIds())
.setData("task_protect_object_ids", task.getProtectObjectIds());
}
@PostMapping("/{id}/update")
public ResponseResult updateTask(@PathVariable("id") Integer taskId, @RequestBody Task task) {
task.setTaskId(taskId);
taskService.updateTask(task);
return ResponseResult.ok()
.setData("task_id", taskId)
.setData("success", true);
}
}

View File

@@ -4,15 +4,37 @@ import com.realtime.protection.configuration.entity.task.Task;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface TaskMapper {
void newTask(@Param("task") Task task);
void newTaskProobjConcat(@Param("task_id") Integer taskId, @Param("proobj_ids") Integer[] proobjIds);
void newTaskProtectObjectConcat(@Param("task_id") Integer taskId, @Param("proobj_ids") List<Integer> proobjIds);
Integer newTaskStaticRuleConcat(@Param("task_id") Integer taskId,
@Param("rule_ids") Integer[] staticRuleIds);
void newTaskStaticRuleConcat(@Param("task_id") Integer taskId,
@Param("rule_ids") List<Integer> staticRuleIds);
Integer newTaskDynamicRuleConcat(@Param("task_id") Integer taskId,
@Param("rule_ids") Integer[] dynamicRuleIds);
void newTaskDynamicRuleConcat(@Param("task_id") Integer taskId,
@Param("rule_ids") List<Integer> dynamicRuleIds);
List<Task> queryTasks(@Param("task_status") Integer taskStatus, @Param("task_type") String task_type,
@Param("task_name") String taskName, @Param("task_creator") String taskCreator,
@Param("page") Integer page, @Param("page_size") Integer pageSize);
Task queryTask(@Param("task_id") Integer taskId);
List<Integer> queryTaskConcatProtectObjectIds(@Param("task_id") Integer taskId);
List<Integer> queryTaskConcatStaticRuleIds(@Param("task_id") Integer taskId);
List<Integer> queryTaskConcatDynamicRuleIds(@Param("task_id") Integer taskId);
void updateTask(@Param("task") Task task);
void clearTaskProtectObjectConcat(@Param("task_id") Integer taskId);
void clearTaskConnectedStaticRule(@Param("task_id") Integer taskId);
void clearTaskConnectedDynamicRule(@Param("task_id") Integer taskId);
}

View File

@@ -5,36 +5,28 @@ import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class TaskService {
private final SqlSessionFactory sqlSessionFactory;
private final TaskMapper taskMapper;
public TaskService(SqlSessionFactory sqlSessionFactory) {
public TaskService(SqlSessionFactory sqlSessionFactory, TaskMapper taskMapper) {
this.sqlSessionFactory = sqlSessionFactory;
this.taskMapper = taskMapper;
}
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());
taskMapper.newTaskProtectObjectConcat(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");
taskMapper.newTaskStaticRuleConcat(task.getTaskId(), task.getStaticRuleIds());
session.commit();
} catch (Exception e) {
@@ -49,4 +41,66 @@ public class TaskService {
}
return task.getTaskId();
}
public List<Task> queryTasks(Integer taskStatus,
String taskType, String taskName, String taskCreator,
Integer page, Integer pageSize) {
return taskMapper.queryTasks(taskStatus, taskType, taskName, taskCreator, page, pageSize);
}
public Task queryTask(Integer id) {
SqlSession session = sqlSessionFactory.openSession(false);
TaskMapper taskMapper = session.getMapper(TaskMapper.class);
Task task;
try {
task = taskMapper.queryTask(id);
if (task == null) {
return null;
}
task.setProtectObjectIds(taskMapper.queryTaskConcatProtectObjectIds(task.getTaskId()));
// task.setDynamicRuleIds(taskMapper.queryTaskConcatDynamicRuleIds(task.getTaskId()));
task.setStaticRuleIds(taskMapper.queryTaskConcatStaticRuleIds(task.getTaskId()));
session.commit();
} catch (Exception e) {
session.rollback();
throw e;
} finally {
session.close();
}
return task;
}
public void updateTask(Task task) {
SqlSession session = sqlSessionFactory.openSession(false);
TaskMapper taskMapper = session.getMapper(TaskMapper.class);
try {
taskMapper.updateTask(task);
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();
}
}
}