diff --git a/build.gradle b/build.gradle index 7cdc054..6b7ffd3 100644 --- a/build.gradle +++ b/build.gradle @@ -27,9 +27,11 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.3' implementation 'org.springframework.boot:spring-boot-starter-actuator' + compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' runtimeOnly 'com.mysql:mysql-connector-j' + runtimeOnly 'com.oracle.database.jdbc:ojdbc8:19.7.0.0' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter-test:3.0.3' @@ -39,6 +41,7 @@ dependencies { implementation 'cn.dev33:sa-token-spring-boot3-starter:1.37.0' implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0' implementation 'com.alibaba:easyexcel:3.3.3' + implementation 'com.baomidou:dynamic-datasource-spring-boot3-starter:4.3.0' } tasks.named('test') { diff --git a/src/main/java/com/realtime/protection/ProtectionApplication.java b/src/main/java/com/realtime/protection/ProtectionApplication.java index c54ab37..4effcda 100644 --- a/src/main/java/com/realtime/protection/ProtectionApplication.java +++ b/src/main/java/com/realtime/protection/ProtectionApplication.java @@ -1,6 +1,5 @@ package com.realtime.protection; -import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; diff --git a/src/main/java/com/realtime/protection/configuration/entity/defense/template/ProtectLevel.java b/src/main/java/com/realtime/protection/configuration/entity/defense/template/ProtectLevel.java new file mode 100644 index 0000000..8d9fd09 --- /dev/null +++ b/src/main/java/com/realtime/protection/configuration/entity/defense/template/ProtectLevel.java @@ -0,0 +1,22 @@ +package com.realtime.protection.configuration.entity.defense.template; + +import lombok.Data; + +@Data +public class ProtectLevel { + private Integer protectLevelId; + + private Boolean hasProtectObjectIP = false; + + private Boolean hasProtectObjectPort = false; + + private Boolean hasPeerIP = false; + + private Boolean hasPeerPort = false; + + private Boolean hasProtocol = false; + + private Boolean hasURL = false; + + private Boolean hasDNS = false; +} diff --git a/src/main/java/com/realtime/protection/configuration/entity/defense/template/Template.java b/src/main/java/com/realtime/protection/configuration/entity/defense/template/Template.java index 4174e01..74f9fe4 100644 --- a/src/main/java/com/realtime/protection/configuration/entity/defense/template/Template.java +++ b/src/main/java/com/realtime/protection/configuration/entity/defense/template/Template.java @@ -15,30 +15,31 @@ public class Template { @NotNull(message = "template name should not be empty.") private String templateName; - @JsonProperty("template_elements") - private List templateElements; - - @JsonProperty("default_op") - @NotNull(message = "default_op should not be empty.") - private String defaultOp; - @JsonProperty("template_running_tasks") private Integer templateRunningTasks; @JsonProperty("template_used") private Integer templateUsedTimes; - private Boolean hasProtectObjectIP; + @JsonProperty("source_system") + @NotNull(message = "source_system should not be empty. ") + private String sourceSystem; - private Boolean hasProtectObjectPort; + @JsonProperty("protect_level_low") + @NotNull(message = "protect_level_low should not be empty. ") + private ProtectLevel protectLevelLow; - private Boolean hasPeerIP; + @JsonProperty("protect_level_medium") + @NotNull(message = "protect_level_medium should not be empty. ") + private ProtectLevel protectLevelMedium; - private Boolean hasPeerPort; + @JsonProperty("protect_level_high") + @NotNull(message = "protect_level_high should not be empty. ") + private ProtectLevel protectLevelHigh; - private Boolean hasProtocol; + private Integer createUserId; - private Boolean hasURL; + private String createUsername; - private Boolean hasDNS; + private String createDepart; } diff --git a/src/main/java/com/realtime/protection/configuration/entity/task/Task.java b/src/main/java/com/realtime/protection/configuration/entity/task/Task.java index 4f921d3..9e6cabf 100644 --- a/src/main/java/com/realtime/protection/configuration/entity/task/Task.java +++ b/src/main/java/com/realtime/protection/configuration/entity/task/Task.java @@ -55,9 +55,6 @@ public class Task { @JsonProperty("dynamic_rule_ids") private List dynamicRuleIds; - @JsonProperty("protect_object_ids") - private List protectObjectIds; - @JsonProperty("task_status") private Integer taskStatus; diff --git a/src/main/java/com/realtime/protection/configuration/utils/AuditStatusValidator.java b/src/main/java/com/realtime/protection/configuration/utils/status/AuditStatusValidator.java similarity index 91% rename from src/main/java/com/realtime/protection/configuration/utils/AuditStatusValidator.java rename to src/main/java/com/realtime/protection/configuration/utils/status/AuditStatusValidator.java index ae67873..1a50828 100644 --- a/src/main/java/com/realtime/protection/configuration/utils/AuditStatusValidator.java +++ b/src/main/java/com/realtime/protection/configuration/utils/status/AuditStatusValidator.java @@ -1,4 +1,4 @@ -package com.realtime.protection.configuration.utils; +package com.realtime.protection.configuration.utils.status; public class AuditStatusValidator { diff --git a/src/main/java/com/realtime/protection/configuration/utils/status/StatusChanger.java b/src/main/java/com/realtime/protection/configuration/utils/status/StatusChanger.java new file mode 100644 index 0000000..34b3d9d --- /dev/null +++ b/src/main/java/com/realtime/protection/configuration/utils/status/StatusChanger.java @@ -0,0 +1,20 @@ +package com.realtime.protection.configuration.utils.status; + +import com.realtime.protection.configuration.utils.status.state.State; + +public class StatusChanger { + + private final State state; + + public StatusChanger(State state) { + this.state = state; + } + + public static StatusChanger setOriginal(State original) { + return new StatusChanger(original); + } + + public Boolean changeState(State newState) { + return this.state.handle(newState); + } +} diff --git a/src/main/java/com/realtime/protection/configuration/utils/status/state/PauseState.java b/src/main/java/com/realtime/protection/configuration/utils/status/state/PauseState.java new file mode 100644 index 0000000..0b37d33 --- /dev/null +++ b/src/main/java/com/realtime/protection/configuration/utils/status/state/PauseState.java @@ -0,0 +1,16 @@ +package com.realtime.protection.configuration.utils.status.state; + +public class PauseState implements State { + @Override + public Boolean handle(State newState) { + if (!(newState instanceof RunningState)) { + return false; + } + + return handleRun(); + } + + private Boolean handleRun() { + return true; + } +} diff --git a/src/main/java/com/realtime/protection/configuration/utils/status/state/RunningState.java b/src/main/java/com/realtime/protection/configuration/utils/status/state/RunningState.java new file mode 100644 index 0000000..ca21331 --- /dev/null +++ b/src/main/java/com/realtime/protection/configuration/utils/status/state/RunningState.java @@ -0,0 +1,28 @@ +package com.realtime.protection.configuration.utils.status.state; + +public class RunningState implements State { + @Override + public Boolean handle(State newState) { + if (newState instanceof RunningState) { + return false; + } + + if (newState instanceof PauseState) { + return handlePause(); + } + + if (newState instanceof StopState) { + return handleStop(); + } + + return false; + } + + private Boolean handlePause() { + return true; + } + + private Boolean handleStop() { + return true; + } +} diff --git a/src/main/java/com/realtime/protection/configuration/utils/status/state/State.java b/src/main/java/com/realtime/protection/configuration/utils/status/state/State.java new file mode 100644 index 0000000..6e931c1 --- /dev/null +++ b/src/main/java/com/realtime/protection/configuration/utils/status/state/State.java @@ -0,0 +1,6 @@ +package com.realtime.protection.configuration.utils.status.state; + +public interface State { + + Boolean handle(State newState); +} diff --git a/src/main/java/com/realtime/protection/configuration/utils/status/state/StopState.java b/src/main/java/com/realtime/protection/configuration/utils/status/state/StopState.java new file mode 100644 index 0000000..3ec30fd --- /dev/null +++ b/src/main/java/com/realtime/protection/configuration/utils/status/state/StopState.java @@ -0,0 +1,17 @@ +package com.realtime.protection.configuration.utils.status.state; + +public class StopState implements State { + + @Override + public Boolean handle(State newState) { + if (!(newState instanceof RunningState)) { + return false; + } + + return handleRun(); + } + + public Boolean handleRun() { + return true; + } +} diff --git a/src/main/java/com/realtime/protection/server/defense/object/ProtectObjectController.java b/src/main/java/com/realtime/protection/server/defense/object/ProtectObjectController.java index 4144d37..f6bf6c3 100644 --- a/src/main/java/com/realtime/protection/server/defense/object/ProtectObjectController.java +++ b/src/main/java/com/realtime/protection/server/defense/object/ProtectObjectController.java @@ -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()); } diff --git a/src/main/java/com/realtime/protection/server/defense/object/ProtectObjectService.java b/src/main/java/com/realtime/protection/server/defense/object/ProtectObjectService.java index f671565..2f713e5 100644 --- a/src/main/java/com/realtime/protection/server/defense/object/ProtectObjectService.java +++ b/src/main/java/com/realtime/protection/server/defense/object/ProtectObjectService.java @@ -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 protectObjectBatch = ListUtils.newArrayListWithExpectedSize(100); + List 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 protectObjectBatch = ListUtils.newArrayListWithExpectedSize(100); + List protectObjectBatch = ListUtils.newArrayListWithExpectedSize(batchSize); for (Integer protectObjectId : list) { protectObjectBatch.add(protectObjectId); - if (protectObjectBatch.size() < 100) { + if (protectObjectBatch.size() < batchSize) { continue; } mapper.deleteProtectObjects(protectObjectBatch); diff --git a/src/main/java/com/realtime/protection/server/defense/template/TemplateMapper.java b/src/main/java/com/realtime/protection/server/defense/template/TemplateMapper.java index 166ee34..2cb1505 100644 --- a/src/main/java/com/realtime/protection/server/defense/template/TemplateMapper.java +++ b/src/main/java/com/realtime/protection/server/defense/template/TemplateMapper.java @@ -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