1. 新增数据源oracle,已在application.yml中配置

2. 新增任务状态类,未来将在切换任务状态中使用
3. 新增ProtectLevel实体类,用来存储Template对应的三种防护等级数据
4. Task实体类中删除protectObjectIds,因为MySQL表结构发生修改
5. TaskController新增audit和delete路由,用以审核和删除Task
6. TemplateMapper新增newProtectLevel方法
7.
This commit is contained in:
EnderByEndera
2024-01-08 20:01:20 +08:00
parent 1e9fe37d0d
commit 2b04a7d6ce
26 changed files with 392 additions and 204 deletions

View File

@@ -42,7 +42,9 @@ public class ProtectObjectController {
}
@PostMapping("/upload")
public ResponseResult uploadFile(MultipartFile uploadFile) throws IOException {
public ResponseResult uploadFile(
@NotNull(message = "uploadFile cannot be null") MultipartFile uploadFile
) throws IOException {
EasyExcel.read(uploadFile.getInputStream(), ProtectObject.class,
new ProjectObjectDataListener(protectObjectService)).sheet().doRead();
return ResponseResult.ok();
@@ -52,11 +54,12 @@ public class ProtectObjectController {
public void downloadTemplate(HttpServletResponse response) throws IOException {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("防护对象", StandardCharsets.UTF_8).replaceAll("\\+", "%20");
String fileName = URLEncoder.encode("防护对象上传模板", StandardCharsets.UTF_8)
.replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), ProtectObject.class)
.sheet("防护对象")
.sheet("防护对象上传模板")
.doWrite(List.of());
}

View File

@@ -2,9 +2,8 @@ package com.realtime.protection.server.defense.object;
import com.alibaba.excel.util.ListUtils;
import com.realtime.protection.configuration.entity.defense.object.ProtectObject;
import com.realtime.protection.configuration.utils.AuditStatusValidator;
import com.realtime.protection.configuration.utils.status.AuditStatusValidator;
import com.realtime.protection.configuration.utils.SqlSessionWrapper;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -12,12 +11,12 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
@Service
public class ProtectObjectService {
private final ProtectObjectMapper protectObjectMapper;
private final SqlSessionWrapper sqlSessionWrapper;
private static final Integer batchSize = 100;
public ProtectObjectService(ProtectObjectMapper protectObjectMapper, SqlSessionWrapper sqlSessionWrapper) {
this.protectObjectMapper = protectObjectMapper;
@@ -39,10 +38,10 @@ public class ProtectObjectService {
return false;
}
List<ProtectObject> protectObjectBatch = ListUtils.newArrayListWithExpectedSize(100);
List<ProtectObject> protectObjectBatch = ListUtils.newArrayListWithExpectedSize(batchSize);
for (ProtectObject protectObject : protectObjectList) {
protectObjectBatch.add(protectObject);
if (protectObjectBatch.size() < 100) {
if (protectObjectBatch.size() < batchSize) {
continue;
}
mapper.newProtectObjects(protectObjectBatch);
@@ -81,10 +80,10 @@ public class ProtectObjectService {
boolean success = true;
Integer result;
List<Integer> protectObjectBatch = ListUtils.newArrayListWithExpectedSize(100);
List<Integer> protectObjectBatch = ListUtils.newArrayListWithExpectedSize(batchSize);
for (Integer protectObjectId : list) {
protectObjectBatch.add(protectObjectId);
if (protectObjectBatch.size() < 100) {
if (protectObjectBatch.size() < batchSize) {
continue;
}
mapper.deleteProtectObjects(protectObjectBatch);

View File

@@ -1,5 +1,6 @@
package com.realtime.protection.server.defense.template;
import com.realtime.protection.configuration.entity.defense.template.ProtectLevel;
import com.realtime.protection.configuration.entity.defense.template.Template;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@@ -10,10 +11,14 @@ import java.util.List;
public interface TemplateMapper {
void newTemplate(@Param("template") Template template);
void newProtectLevel(@Param("level") ProtectLevel protectLevel);
List<Template> queryTemplates(@Param("template_name") String templateName,
@Param("page") Integer page,
@Param("page_size") Integer pageSize);
ProtectLevel queryProtectLevel(@Param("level_id") Integer protectLevelId);
Boolean updateTemplateInformation(@Param("template") Template template);
void countTemplateRunningTasks(@Param("template_id") Integer templateId);

View File

@@ -2,7 +2,9 @@ package com.realtime.protection.server.defense.template;
import com.realtime.protection.configuration.entity.defense.template.Template;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.beans.Transient;
import java.util.Arrays;
import java.util.List;
@@ -11,18 +13,15 @@ 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();
}
setTemplateElements(template);
@Transactional
public Integer newTemplate(Template template) {
templateMapper.newProtectLevel(template.getProtectLevelLow());
templateMapper.newProtectLevel(template.getProtectLevelMedium());
templateMapper.newProtectLevel(template.getProtectLevelHigh());
templateMapper.newTemplate(template);
@@ -37,28 +36,11 @@ public class TemplateService {
}
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);
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();
}
}
}
public Boolean addTemplateUsedTimes(Integer templateId, Integer addTimes) {
return templateMapper.addTemplateUsedTimes(templateId, addTimes);
}