1、DynamicRule实现新增、删除、修改、id查询、分页查询功能。并在crud时与ProtectObject关联。
2、StaticRule添加批量导入、模板文件下载功能,使用sqlSessionWrapper重写批量删除 3、WhiteList添加模板文件下载功能
This commit is contained in:
@@ -1,4 +1,61 @@
|
||||
package com.realtime.protection.configuration.entity.rule.dynamicrule;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.realtime.protection.configuration.entity.defense.object.ProtectObject;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class DynamicRuleObject {
|
||||
|
||||
@JsonProperty("dynamic_rule_id")
|
||||
private Integer dynamicRuleId;
|
||||
|
||||
@NotNull
|
||||
@JsonProperty("dynamic_rule_name")
|
||||
private String dynamicRuleName;
|
||||
|
||||
@JsonProperty("dynamic_rule_create_time")
|
||||
private LocalDateTime dynamicRuleCreateTime;
|
||||
@JsonProperty("dynamic_rule_modify_time")
|
||||
private LocalDateTime dynamicRuleModifyTime;
|
||||
|
||||
@JsonProperty("dynamic_rule_create_username")
|
||||
private String dynamicRuleCreateUsername;
|
||||
|
||||
// @JsonProperty("dynamic_rule_audit_status")
|
||||
// private Integer dynamicRuleAuditStatus;
|
||||
@JsonProperty("dynamic_rule_create_depart")
|
||||
private String dynamicRuleCreateDepart;
|
||||
|
||||
//动态规则选择的防护对象ids?
|
||||
@JsonProperty("protect_object_ids")
|
||||
private List<Integer> protectObjectIds;
|
||||
@JsonProperty("protect_objects")
|
||||
private List<ProtectObject> protectObjects;
|
||||
|
||||
|
||||
//还没有建立外键
|
||||
@JsonProperty("dynamic_rule_create_user_id")
|
||||
private Integer dynamicRuleCreateUserId;
|
||||
@JsonProperty("dynamic_rule_used_task_id")
|
||||
private Integer dynamicRuleUsedTaskId;
|
||||
|
||||
@JsonProperty("dynamic_rule_source_system")
|
||||
private Integer dynamicRuleSourceSystem;
|
||||
@JsonProperty("dynamic_rule_event_type")
|
||||
private Integer dynamicRuleEventType;
|
||||
@JsonProperty("dynamic_rule_protect_level")
|
||||
private Integer dynamicRuleProtectLevel;
|
||||
|
||||
@JsonProperty("dynamic_rule_priority")
|
||||
private Integer dynamicRulePriority;
|
||||
@JsonProperty("dynamic_rule_range")
|
||||
private String dynamicRuleRange;
|
||||
@JsonProperty("dynamic_rule_frequency")
|
||||
private Integer dynamicRuleFrequency;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.realtime.protection.configuration.entity.rule.staticrule;
|
||||
|
||||
import com.alibaba.excel.annotation.ExcelIgnore;
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -15,59 +17,82 @@ import java.time.LocalDateTime;
|
||||
@AllArgsConstructor
|
||||
public class StaticRuleObject {
|
||||
@JsonProperty("static_rule_id")
|
||||
@ExcelIgnore
|
||||
private Integer staticRuleId;
|
||||
|
||||
@NotNull
|
||||
@JsonProperty("static_rule_name")
|
||||
@ExcelProperty("名称")
|
||||
private String staticRuleName;
|
||||
|
||||
@JsonProperty("static_rule_create_time")
|
||||
@ExcelIgnore
|
||||
private LocalDateTime staticRuleCreateTime;
|
||||
@JsonProperty("static_rule_modify_time")
|
||||
@ExcelIgnore
|
||||
private LocalDateTime staticRuleModifyTime;
|
||||
|
||||
@JsonProperty("static_rule_create_username")
|
||||
@ExcelIgnore
|
||||
private String staticRuleCreateUsername;
|
||||
@JsonProperty("static_rule_audit_status")
|
||||
@ExcelIgnore
|
||||
private Integer staticRuleAuditStatus;
|
||||
@JsonProperty("static_rule_create_depart")
|
||||
@ExcelIgnore
|
||||
private String staticRuleCreateDepart;
|
||||
@JsonProperty("static_rule_create_user_id")
|
||||
@ExcelIgnore
|
||||
private Integer staticRuleCreateUserId;
|
||||
@JsonProperty("static_rule_used_task_id")
|
||||
@ExcelIgnore
|
||||
private Integer staticRuleUsedTaskId;
|
||||
|
||||
@JsonProperty("static_rule_sip")
|
||||
@ExcelProperty("源IP地址")
|
||||
private String staticRuleSip;
|
||||
@JsonProperty("static_rule_msip")
|
||||
@ExcelProperty("源IP地址掩码")
|
||||
private String staticRuleMsip;
|
||||
@JsonProperty("static_rule_sport")
|
||||
@ExcelProperty("源端口")
|
||||
private Integer staticRuleSport;
|
||||
@JsonProperty("static_rule_msport")
|
||||
@ExcelProperty("源端口掩码")
|
||||
private Integer staticRuleMsport;
|
||||
@JsonProperty("static_rule_dip")
|
||||
@ExcelProperty("目的IP地址")
|
||||
private String staticRuleDip;
|
||||
@JsonProperty("static_rule_mdip")
|
||||
@ExcelProperty("目的IP地址掩码")
|
||||
private String staticRuleMdip;
|
||||
@JsonProperty("static_rule_dport")
|
||||
@ExcelProperty("目的端口")
|
||||
private Integer staticRuleDport;
|
||||
@JsonProperty("static_rule_mdport")
|
||||
@ExcelProperty("目的端口掩码")
|
||||
private Integer staticRuleMdport;
|
||||
@JsonProperty("static_rule_protocol")
|
||||
@ExcelProperty("协议")
|
||||
private String staticRuleProtocol;
|
||||
@JsonProperty("static_rule_mprotocol")
|
||||
@ExcelProperty("协议掩码")
|
||||
private String staticRuleMprotocol;
|
||||
@JsonProperty("static_rule_dns")
|
||||
@ExcelProperty("DNS")
|
||||
private String staticRuleDns;
|
||||
@JsonProperty("static_rule_url")
|
||||
@ExcelProperty("URL")
|
||||
private String staticRuleURL;
|
||||
|
||||
@JsonProperty("static_rule_priority")
|
||||
@ExcelProperty("优先级")
|
||||
private Integer staticRulePriority;
|
||||
@JsonProperty("static_rule_range")
|
||||
@ExcelProperty("范围")
|
||||
private String staticRuleRange;
|
||||
@JsonProperty("static_rule_frequency")
|
||||
@ExcelProperty("频率")
|
||||
private Integer staticRuleFrequency;
|
||||
@JsonProperty("static_rule_protect_level")
|
||||
private Integer staticRuleProtectLevel;
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
package com.realtime.protection.server.rule.dynamicrule;
|
||||
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import com.realtime.protection.configuration.entity.rule.dynamicrule.DynamicRuleObject;
|
||||
import com.realtime.protection.configuration.response.ResponseResult;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("dynamicrule")
|
||||
@Slf4j
|
||||
public class DynamicRuleController {
|
||||
|
||||
private final DynamicRuleService dynamicRuleService;
|
||||
|
||||
public DynamicRuleController(DynamicRuleService dynamicRuleService) {
|
||||
this.dynamicRuleService = dynamicRuleService;
|
||||
}
|
||||
|
||||
// 新增 要关联防护对象!!!!
|
||||
@RequestMapping("/new")
|
||||
public ResponseResult newDynamicRuleObject(@RequestBody @Valid DynamicRuleObject dynamicRuleObject) {
|
||||
log.info("新增动态规则: {}", dynamicRuleObject);
|
||||
//调用service新增
|
||||
Integer dynamicRuleObjectId = dynamicRuleService.newDynamicRuleObject(dynamicRuleObject);
|
||||
return ResponseResult.ok().
|
||||
setData("dynamic_rule_id", dynamicRuleObjectId)
|
||||
.setData("dynamic_rule_name", dynamicRuleObject.getDynamicRuleName())
|
||||
.setData("success", true);
|
||||
}
|
||||
|
||||
//以excel文件方式批量导入,但动态规则没有这个需求
|
||||
@PostMapping("/upload")
|
||||
public ResponseResult uploadFile(MultipartFile uploadFile) throws IOException {
|
||||
EasyExcel.read(uploadFile.getInputStream(), DynamicRuleObject.class,
|
||||
new DynamicRuleDataListener(dynamicRuleService)).sheet().doRead();
|
||||
return ResponseResult.ok();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//id删除
|
||||
@RequestMapping("/{dynamicRuleId}/delete")
|
||||
public ResponseResult deleteDynamicRuleObject(@PathVariable Integer dynamicRuleId ) {
|
||||
log.info("删除动态规则: {}", dynamicRuleId);
|
||||
//调用service删除
|
||||
dynamicRuleService.deleteDynamicRuleObject(dynamicRuleId);
|
||||
|
||||
return ResponseResult.ok();
|
||||
}
|
||||
|
||||
//批量删除
|
||||
@RequestMapping("/delete")
|
||||
public ResponseResult deleteDynamicRuleObjects(@RequestBody List<Integer> dynamicRuleIds) {
|
||||
log.info("批量删除动态规则: {}", dynamicRuleIds);
|
||||
//调用service删除
|
||||
|
||||
return ResponseResult.ok()
|
||||
.setData("success",dynamicRuleService.deleteDynamicRuleObjects(dynamicRuleIds));
|
||||
}
|
||||
|
||||
//修改
|
||||
@RequestMapping("/{dynamicRuleId}/update")
|
||||
public ResponseResult updateDynamicRuleObject(@PathVariable Integer dynamicRuleId,
|
||||
@RequestBody @Valid DynamicRuleObject dynamicRuleObject) {
|
||||
log.info("修改动态规则: {}:{}",
|
||||
dynamicRuleId, dynamicRuleObject.getDynamicRuleName());
|
||||
|
||||
dynamicRuleService.updateDynamicRuleObject(dynamicRuleId, dynamicRuleObject);
|
||||
|
||||
return ResponseResult.ok()
|
||||
.setData("dynamic_rule_id", dynamicRuleId)
|
||||
.setData("success", true);
|
||||
}
|
||||
|
||||
//id查询
|
||||
@RequestMapping("/{dynamicRuleId}/query")
|
||||
public ResponseResult queryDynamicRuleObjectById(@PathVariable Integer dynamicRuleId) {
|
||||
log.info("查询动态规则: {}", dynamicRuleId);
|
||||
//调用service查询
|
||||
return ResponseResult.ok()
|
||||
.setData("dynamic_rule", dynamicRuleService.queryDynamicRuleById(dynamicRuleId));
|
||||
}
|
||||
|
||||
//分页查询
|
||||
@RequestMapping("/query")
|
||||
public ResponseResult queryDynamicRuleObject(@RequestParam(value = "dynamic_rule_name", required = false) String dynamicRuleName,
|
||||
@RequestParam(value = "dynamic_rule_id", required = false) Integer dynamicRuleId,
|
||||
@RequestParam(value = "page", defaultValue = "1") Integer page,
|
||||
@RequestParam(value = "page_size", defaultValue = "10") Integer pageSize) {
|
||||
log.info("分页查询动态规则: {}:{}:{}:{}", dynamicRuleName, dynamicRuleId, page, pageSize);
|
||||
//调用service查询
|
||||
return ResponseResult.ok()
|
||||
.setData("dynamic_rule_list", dynamicRuleService.queryDynamicRuleObject(dynamicRuleName, dynamicRuleId, page, pageSize));
|
||||
}
|
||||
|
||||
//详情查看?? 查看什么
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.realtime.protection.server.rule.dynamicrule;
|
||||
|
||||
import com.alibaba.excel.context.AnalysisContext;
|
||||
import com.alibaba.excel.read.listener.ReadListener;
|
||||
import com.alibaba.excel.util.ListUtils;
|
||||
import com.realtime.protection.configuration.entity.rule.dynamicrule.DynamicRuleObject;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
public class DynamicRuleDataListener implements ReadListener<DynamicRuleObject> {
|
||||
private static final int batchCount = 100;
|
||||
private final List<DynamicRuleObject> cachedDataList
|
||||
= ListUtils.newArrayListWithExpectedSize(batchCount);
|
||||
|
||||
private final DynamicRuleService dynamicRuleService;
|
||||
|
||||
public DynamicRuleDataListener(DynamicRuleService dynamicRuleService) {
|
||||
this.dynamicRuleService = dynamicRuleService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(DynamicRuleObject object, AnalysisContext analysisContext) {
|
||||
log.info("解析到一条数据:{}", object.toString());
|
||||
cachedDataList.add(object);
|
||||
if (cachedDataList.size() > batchCount) {
|
||||
saveData();
|
||||
cachedDataList.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
|
||||
saveData();
|
||||
}
|
||||
|
||||
/**
|
||||
* 加上存储数据库
|
||||
*/
|
||||
private void saveData() {
|
||||
log.info("{}条数据,开始存储数据库!", cachedDataList.size());
|
||||
Boolean success = dynamicRuleService.newDynamicRuleObjects(cachedDataList);
|
||||
log.info("存储数据库成功!");
|
||||
if (!success) {
|
||||
throw new RuntimeException("Error reading data in /proobj/new");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.realtime.protection.server.rule.dynamicrule;
|
||||
|
||||
import com.realtime.protection.configuration.entity.defense.object.ProtectObject;
|
||||
import com.realtime.protection.configuration.entity.rule.dynamicrule.DynamicRuleObject;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface DynamicRuleMapper {
|
||||
|
||||
//新建动态规则
|
||||
void newDynamicRule(@Param("object") DynamicRuleObject dynamicRuleObject);
|
||||
|
||||
//新建动态规则与保护对象关联
|
||||
void newDynamicRulProtectObjectConcat(long dynamicRuleId, Integer protectObjectId);
|
||||
|
||||
void deleteDynamicRuleObject( Integer dynamicRuleId);
|
||||
|
||||
DynamicRuleObject queryDynamicRuleById(Integer dynamicRuleId);
|
||||
|
||||
List<ProtectObject> queryProtectObjectByRuleId(Integer dynamicRuleId);
|
||||
|
||||
void updateDynamicRuleObject(@Param("dynamicRuleId") Integer dynamicRuleId,@Param("object") DynamicRuleObject dynamicRuleObject);
|
||||
|
||||
void newDynamicRules(List<DynamicRuleObject> dynamicRuleObjects);
|
||||
|
||||
void deleteDynamicRules(List<Integer> dynamicRuleIds);
|
||||
|
||||
List<DynamicRuleObject> queryDynamicRuleObject(String dynamicRuleName, Integer dynamicRuleId, Integer page, Integer pageSize);
|
||||
|
||||
void deleteDynamicRuleProtectObjectConcat(Integer dynamicRuleId);
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package com.realtime.protection.server.rule.dynamicrule;
|
||||
|
||||
import com.alibaba.excel.util.ListUtils;
|
||||
import com.realtime.protection.configuration.entity.rule.dynamicrule.DynamicRuleObject;
|
||||
import com.realtime.protection.configuration.utils.SqlSessionWrapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
@Service
|
||||
public class DynamicRuleService {
|
||||
private final DynamicRuleMapper dynamicRuleMapper;
|
||||
private final SqlSessionWrapper sqlSessionWrapper;
|
||||
|
||||
public DynamicRuleService(DynamicRuleMapper dynamicRuleMapper, SqlSessionWrapper sqlSessionWrapper) {
|
||||
|
||||
this.sqlSessionWrapper = sqlSessionWrapper;
|
||||
this.dynamicRuleMapper = dynamicRuleMapper;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Integer newDynamicRuleObject(DynamicRuleObject dynamicRule) {
|
||||
dynamicRuleMapper.newDynamicRule(dynamicRule);
|
||||
|
||||
Integer dynamicRuleId = dynamicRule.getDynamicRuleId();
|
||||
|
||||
dynamicRule.getProtectObjectIds().forEach(
|
||||
protectObjectId -> dynamicRuleMapper.newDynamicRulProtectObjectConcat(dynamicRuleId, protectObjectId));
|
||||
|
||||
|
||||
return dynamicRuleId;
|
||||
}
|
||||
|
||||
//批量新建多个动态规则
|
||||
public Boolean newDynamicRuleObjects(List<DynamicRuleObject> DynamicRuleList) {
|
||||
Function<DynamicRuleMapper, Function<List<DynamicRuleObject>, Boolean>> newDynamicRuleFunction =
|
||||
mapper -> list -> {
|
||||
if (list == null || list.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
List<DynamicRuleObject> DynamicRuleIdBatch = ListUtils.newArrayListWithExpectedSize(100);
|
||||
for (DynamicRuleObject dynamicRule : DynamicRuleList) {
|
||||
dynamicRule.setDynamicRuleCreateTime(LocalDateTime.now());
|
||||
DynamicRuleIdBatch.add(dynamicRule);
|
||||
if (DynamicRuleIdBatch.size() < 100) {
|
||||
continue;
|
||||
}
|
||||
//mapper指的就是外层函数输入的参数,也就是WhiteListMapper
|
||||
mapper.newDynamicRules(DynamicRuleIdBatch);
|
||||
DynamicRuleIdBatch.clear();
|
||||
}
|
||||
if (!DynamicRuleIdBatch.isEmpty()) {
|
||||
mapper.newDynamicRules(DynamicRuleIdBatch);
|
||||
}
|
||||
return true;
|
||||
};
|
||||
//实现事务操作
|
||||
return sqlSessionWrapper.startBatchSession(DynamicRuleMapper.class, newDynamicRuleFunction, DynamicRuleList);
|
||||
}
|
||||
|
||||
|
||||
public void deleteDynamicRuleObject(Integer dynamicRuleId) {
|
||||
//不需要使用 join
|
||||
//在数据库中设置了级联删除 ON DELETE CASCADE,在删除在从父表中删除数据时自动删除子表中的数据
|
||||
dynamicRuleMapper.deleteDynamicRuleObject(dynamicRuleId);
|
||||
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public DynamicRuleObject queryDynamicRuleById(Integer dynamicRuleId) {
|
||||
|
||||
//查询DynamicRule
|
||||
DynamicRuleObject dynamicRuleObject = dynamicRuleMapper.queryDynamicRuleById(dynamicRuleId);
|
||||
//查询DynamicRule关联的ProtectObject
|
||||
dynamicRuleObject.setProtectObjects(dynamicRuleMapper.queryProtectObjectByRuleId(dynamicRuleId));
|
||||
|
||||
return dynamicRuleObject;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void updateDynamicRuleObject(Integer dynamicRuleId, DynamicRuleObject dynamicRuleObject) {
|
||||
//更新DynamicRule
|
||||
dynamicRuleObject.setDynamicRuleModifyTime(LocalDateTime.now());
|
||||
dynamicRuleMapper.updateDynamicRuleObject(dynamicRuleId, dynamicRuleObject);
|
||||
if (dynamicRuleObject.getProtectObjectIds() == null || dynamicRuleObject.getProtectObjectIds().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
//删除DynamicRule关联的ProtectObject
|
||||
dynamicRuleMapper.deleteDynamicRuleProtectObjectConcat(dynamicRuleId);
|
||||
//新增DynamicRule关联的ProtectObject
|
||||
dynamicRuleObject.getProtectObjectIds().forEach(
|
||||
protectObjectId -> dynamicRuleMapper.newDynamicRulProtectObjectConcat(dynamicRuleId, protectObjectId));
|
||||
}
|
||||
|
||||
// 批量删除
|
||||
public Boolean deleteDynamicRuleObjects(List<Integer> dynamicRuleIds) {
|
||||
Function<DynamicRuleMapper, Function<List<Integer>, Boolean>> deleteDynamicRuleFunction =
|
||||
mapper -> list -> {
|
||||
if (list == null || list.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
List<Integer> DynamicRuleIdBatch = ListUtils.newArrayListWithExpectedSize(100);
|
||||
for (Integer dynamicRuleId : dynamicRuleIds) {
|
||||
DynamicRuleIdBatch.add(dynamicRuleId);
|
||||
if (DynamicRuleIdBatch.size() < 100) {
|
||||
continue;
|
||||
}
|
||||
//mapper指的就是外层函数输入的参数,也就是WhiteListMapper
|
||||
mapper.deleteDynamicRules(DynamicRuleIdBatch);
|
||||
DynamicRuleIdBatch.clear();
|
||||
}
|
||||
if (!DynamicRuleIdBatch.isEmpty()) {
|
||||
mapper.deleteDynamicRules(DynamicRuleIdBatch);
|
||||
}
|
||||
return true;
|
||||
};
|
||||
//实现事务操作
|
||||
return sqlSessionWrapper.startBatchSession(DynamicRuleMapper.class, deleteDynamicRuleFunction, dynamicRuleIds);
|
||||
|
||||
}
|
||||
|
||||
|
||||
//分页查询基础的动态规则,暂时不返回关联的保护对象
|
||||
public List<DynamicRuleObject> queryDynamicRuleObject(String dynamicRuleName, Integer dynamicRuleId, Integer page, Integer pageSize) {
|
||||
return dynamicRuleMapper.queryDynamicRuleObject(dynamicRuleName, dynamicRuleId, page, pageSize);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,18 @@
|
||||
package com.realtime.protection.server.rule.staticrule;
|
||||
|
||||
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import com.realtime.protection.configuration.entity.rule.staticrule.StaticRuleObject;
|
||||
import com.realtime.protection.configuration.response.ResponseResult;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@@ -31,22 +37,37 @@ public class StaticRuleController {
|
||||
return ResponseResult.ok().setData("static_rule_name",object.getStaticRuleName());
|
||||
}
|
||||
|
||||
//以Excel方式批量导入静态规则
|
||||
@PostMapping("/upload")
|
||||
public ResponseResult uploadFile(MultipartFile uploadFile) throws IOException {
|
||||
EasyExcel.read(uploadFile.getInputStream(), StaticRuleObject.class,
|
||||
new StaticRuleDataListener(staticRuleService)).sheet().doRead();
|
||||
return ResponseResult.ok();
|
||||
}
|
||||
//下载模板文件
|
||||
@GetMapping("/download")
|
||||
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");
|
||||
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
|
||||
|
||||
EasyExcel.write(response.getOutputStream(), StaticRuleObject.class)
|
||||
.sheet("静态规则")
|
||||
.doWrite(List.of());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除静态规则
|
||||
* 删除静态规则(有的删了,有的没删,也返回false)
|
||||
*/
|
||||
@DeleteMapping("/{ids}")
|
||||
public ResponseResult delete(@PathVariable List<Integer> ids){
|
||||
log.info("根据id删除静态规则:{}",ids);
|
||||
//调用service删除
|
||||
// if(staticRuleService.deleteStaticRule(ids) == false){
|
||||
// return ResponseResult.error()
|
||||
// .setData("static_rule_id",ids)
|
||||
// .setData("success",false);
|
||||
// //有的删了,有的没删,也返回false
|
||||
// }
|
||||
//
|
||||
return ResponseResult.ok()
|
||||
.setData("static_rule_id",ids)
|
||||
.setData("success",true);
|
||||
.setData("success",staticRuleService.deleteStaticRules(ids));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.realtime.protection.server.rule.staticrule;
|
||||
|
||||
import com.alibaba.excel.context.AnalysisContext;
|
||||
import com.alibaba.excel.read.listener.ReadListener;
|
||||
import com.alibaba.excel.util.ListUtils;
|
||||
import com.realtime.protection.configuration.entity.rule.staticrule.StaticRuleObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
public class StaticRuleDataListener implements ReadListener<StaticRuleObject> {
|
||||
private static final int batchCount = 100;
|
||||
private final List<StaticRuleObject> cachedDataList
|
||||
= ListUtils.newArrayListWithExpectedSize(batchCount);
|
||||
|
||||
private final StaticRuleService staticRuleService;
|
||||
|
||||
public StaticRuleDataListener(StaticRuleService staticRuleService) {
|
||||
this.staticRuleService = staticRuleService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(StaticRuleObject object, AnalysisContext analysisContext) {
|
||||
log.info("解析到一条数据:{}", object.toString());
|
||||
cachedDataList.add(object);
|
||||
if (cachedDataList.size() > batchCount) {
|
||||
saveData();
|
||||
cachedDataList.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
|
||||
saveData();
|
||||
}
|
||||
|
||||
/**
|
||||
* 加上存储数据库
|
||||
*/
|
||||
private void saveData() {
|
||||
log.info("{}条数据,开始存储数据库!", cachedDataList.size());
|
||||
Boolean success = staticRuleService.newStaticRuleObjects(cachedDataList);
|
||||
log.info("存储数据库成功!");
|
||||
if (!success) {
|
||||
throw new RuntimeException("Error reading data in /proobj/new");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,10 @@ public interface StaticRuleMapper {
|
||||
|
||||
//根据主键删除静态规则
|
||||
@Delete("delete from t_static_rule where static_rule_id = #{id}")
|
||||
Boolean deleteStaticRule(Integer id);
|
||||
Boolean deleteStaticRuleById(Integer id);
|
||||
|
||||
|
||||
|
||||
|
||||
//修改静态规则
|
||||
void updateStaticRule(StaticRuleObject object);
|
||||
@@ -29,4 +32,7 @@ public interface StaticRuleMapper {
|
||||
Integer page, Integer pageSize);
|
||||
|
||||
|
||||
void deleteStaticRules(@Param("whiteListIds") List<Integer> staticRuleBatch);
|
||||
|
||||
void newStaticRules(List<StaticRuleObject> staticRuleBatch);
|
||||
}
|
||||
|
||||
@@ -1,19 +1,25 @@
|
||||
package com.realtime.protection.server.rule.staticrule;
|
||||
|
||||
import com.alibaba.excel.util.ListUtils;
|
||||
import com.realtime.protection.configuration.entity.rule.staticrule.StaticRuleObject;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import com.realtime.protection.configuration.utils.SqlSessionWrapper;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
@Service
|
||||
public class StaticRuleService {
|
||||
|
||||
private final StaticRuleMapper staticRuleMapper;
|
||||
private final SqlSessionWrapper sqlSessionWrapper;
|
||||
|
||||
public StaticRuleService(StaticRuleMapper staticRuleMapper, SqlSessionWrapper sqlSessionWrapper) {
|
||||
|
||||
public StaticRuleService(StaticRuleMapper staticRuleMapper) {
|
||||
this.staticRuleMapper = staticRuleMapper;
|
||||
this.sqlSessionWrapper = sqlSessionWrapper;
|
||||
}
|
||||
|
||||
|
||||
@@ -32,17 +38,36 @@ public class StaticRuleService {
|
||||
return object.getStaticRuleId();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Boolean deleteStaticRule(List<Integer> staticRuleIds) {
|
||||
public Object deleteStaticRules(List<Integer> staticRuleIds) {
|
||||
//判断当前静态规则是否能够删除---是否存在任务选择的静态规则??
|
||||
|
||||
//删除静态规则
|
||||
// for (Integer id : staticRuleIds) {
|
||||
// staticRuleMapper.deleteStaticRule(id);
|
||||
// }
|
||||
return staticRuleIds.stream().allMatch(staticRuleMapper::deleteStaticRule);
|
||||
|
||||
Function<StaticRuleMapper, Function<List<Integer>, Boolean>> deleteStaticRulesFunction =
|
||||
mapper -> list -> {
|
||||
if (list == null || list.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
List<Integer> staticRuleBatch = ListUtils.newArrayListWithExpectedSize(100);
|
||||
for (Integer staticRuleId : list) {
|
||||
staticRuleBatch.add(staticRuleId);
|
||||
if (staticRuleBatch.size() < 100) {
|
||||
continue;
|
||||
}
|
||||
//mapper指的就是外层函数输入的参数,也就是WhiteListMapper
|
||||
mapper.deleteStaticRules(staticRuleBatch);
|
||||
staticRuleBatch.clear();
|
||||
}
|
||||
if (!staticRuleBatch.isEmpty()) {
|
||||
mapper.deleteStaticRules(staticRuleBatch);
|
||||
}
|
||||
return true;
|
||||
};
|
||||
//实现事务操作
|
||||
return sqlSessionWrapper.startBatchSession(StaticRuleMapper.class, deleteStaticRulesFunction, staticRuleIds);
|
||||
}
|
||||
|
||||
public void updateStaticRule(StaticRuleObject object) {
|
||||
@@ -66,4 +91,32 @@ public class StaticRuleService {
|
||||
return staticRuleMapper.queryStaticRule(static_rule_name,static_rule_id,page,pageSize);
|
||||
}
|
||||
|
||||
public Boolean newStaticRuleObjects(List<StaticRuleObject> staticRuleList) {
|
||||
Function<StaticRuleMapper, Function<List<StaticRuleObject>, Boolean>> newStaticRuleFunction =
|
||||
mapper -> list -> {
|
||||
if (list == null || list.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
List<StaticRuleObject> StaticRuleBatch = ListUtils.newArrayListWithExpectedSize(100);
|
||||
for (StaticRuleObject staticRule : staticRuleList) {
|
||||
staticRule.setStaticRuleCreateTime(LocalDateTime.now());
|
||||
StaticRuleBatch.add(staticRule);
|
||||
if (StaticRuleBatch.size() < 100) {
|
||||
continue;
|
||||
}
|
||||
//mapper指的就是外层函数输入的参数,也就是WhiteListMapper
|
||||
mapper.newStaticRules(StaticRuleBatch);
|
||||
StaticRuleBatch.clear();
|
||||
}
|
||||
if (!StaticRuleBatch.isEmpty()) {
|
||||
mapper.newStaticRules(StaticRuleBatch);
|
||||
}
|
||||
return true;
|
||||
};
|
||||
//实现事务操作
|
||||
return sqlSessionWrapper.startBatchSession(StaticRuleMapper.class, newStaticRuleFunction, staticRuleList);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,11 +3,14 @@ package com.realtime.protection.server.whitelist;
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import com.realtime.protection.configuration.entity.whitelist.WhiteListObject;
|
||||
import com.realtime.protection.configuration.response.ResponseResult;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@@ -37,6 +40,18 @@ public class WhiteListController {
|
||||
new WhiteListDataListener(whiteListService)).sheet().doRead();
|
||||
return ResponseResult.ok();
|
||||
}
|
||||
//下载模板文件
|
||||
@GetMapping("/download")
|
||||
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");
|
||||
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
|
||||
|
||||
EasyExcel.write(response.getOutputStream(), WhiteListObject.class)
|
||||
.sheet("白名单")
|
||||
.doWrite(List.of());
|
||||
}
|
||||
|
||||
@RequestMapping("/query")
|
||||
public ResponseResult queryWhiteListObject(@RequestParam(value = "whiteobj_name", required = false)String whiteListName,
|
||||
@@ -87,7 +102,6 @@ public class WhiteListController {
|
||||
//还要return 白名单名称吗,还要在查表看他的名称
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/delete")
|
||||
public ResponseResult deleteWhiteListObjects( @RequestBody List<Integer> whiteListObjIds) {
|
||||
//Post不支持body为json。。。 body只能是[9,10]
|
||||
@@ -126,17 +140,17 @@ public class WhiteListController {
|
||||
}
|
||||
|
||||
//修改审核状态
|
||||
@RequestMapping("/{id}/audit/{status}")
|
||||
@RequestMapping("/{id}/audit/{auditStatus}")
|
||||
public ResponseResult updateWhiteListObjectAuditStatus(@PathVariable Integer id,
|
||||
@PathVariable Integer status) {
|
||||
if (id <= 0 || status < 0 || status > 2) {
|
||||
@PathVariable Integer auditStatus) {
|
||||
if (id <= 0 || auditStatus < 0 || auditStatus > 2) {
|
||||
return new ResponseResult(400, "id or status is invalid")
|
||||
.setData("whiteobj_id", id)
|
||||
.setData("success", false);
|
||||
}
|
||||
|
||||
return ResponseResult.ok()
|
||||
.setDataMap(whiteListService.updateWhiteListObjectAuditStatus(id, status))
|
||||
.setDataMap(whiteListService.updateWhiteListObjectAuditStatus(id, auditStatus))
|
||||
.setData("whiteobj_id", id);
|
||||
}
|
||||
|
||||
|
||||
158
src/main/resources/mappers/DynamicRuleMapper.xml
Normal file
158
src/main/resources/mappers/DynamicRuleMapper.xml
Normal file
@@ -0,0 +1,158 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.realtime.protection.server.rule.dynamicrule.DynamicRuleMapper">
|
||||
|
||||
<!-- useGeneratedKeys="true": 这个属性指定了是否要获取数据库生成的主键。如果设置为true,MyBatis会使用JDBC的getGeneratedKeys方法来获取由数据库自动生成的主键。-->
|
||||
<!-- keyProperty="id": 这个属性指定了要将生成的主键赋值给哪个属性。在这个例子中,生成的主键将被赋值给名为id的属性。-->
|
||||
<insert id="newDynamicRule" useGeneratedKeys="true" keyProperty="dynamicRuleId">
|
||||
|
||||
insert into t_dynamic_rule
|
||||
(dynamic_rule_name,
|
||||
create_time, modify_time, dynamic_rule_create_username,
|
||||
dynamic_rule_create_depart, dynamic_rule_source_system,
|
||||
dynamic_rule_event_type, dynamic_rule_protec_level,
|
||||
dynamic_rule_priority, dynamic_rule_range,
|
||||
dynamic_rule_frequency, dynamic_rule_create_user_id)
|
||||
values
|
||||
(#{object.dynamicRuleName},
|
||||
#{object.dynamicRuleCreateTime},#{object.dynamicRuleModifyTime},
|
||||
#{object.dynamicRuleCreateUsername},#{object.dynamicRuleCreateDepart},
|
||||
#{object.dynamicRuleSourceSystem},
|
||||
#{object.dynamicRuleEventType}, #{object.dynamicRuleProtectLevel},
|
||||
#{object.dynamicRulePriority}, #{object.dynamicRuleRange},
|
||||
#{object.dynamicRuleFrequency},
|
||||
#{object.dynamicRuleCreateUserId})
|
||||
|
||||
</insert>
|
||||
|
||||
<insert id="newDynamicRulProtectObjectConcat">
|
||||
insert into
|
||||
t_protect_object_dynamic_rule_conn(dynamic_rule_id, protect_object_id)
|
||||
values (#{dynamicRuleId}, #{protectObjectId})
|
||||
</insert>
|
||||
<insert id="newDynamicRules">
|
||||
insert into t_dynamic_rule
|
||||
(dynamic_rule_name,
|
||||
create_time, modify_time, dynamic_rule_create_username,
|
||||
dynamic_rule_create_depart, dynamic_rule_source_system,
|
||||
dynamic_rule_event_type, dynamic_rule_protec_level,
|
||||
dynamic_rule_priority, dynamic_rule_range,
|
||||
dynamic_rule_frequency, dynamic_rule_create_user_id
|
||||
)
|
||||
values
|
||||
<foreach collection="dynamicRuleObjects" item="object" separator=",">
|
||||
(#{object.dynamicRuleName},
|
||||
#{object.dynamicRuleCreateTime},#{object.dynamicRuleModifyTime},
|
||||
#{object.dynamicRuleCreateUsername},#{object.dynamicRuleCreateDepart},
|
||||
#{object.dynamicRuleSourceSystem},
|
||||
#{object.dynamicRuleEventType}, #{object.dynamicRuleProtectLevel},
|
||||
#{object.dynamicRulePriority}, #{object.dynamicRuleRange},
|
||||
#{object.dynamicRuleFrequency},
|
||||
#{object.dynamicRuleCreateUserId})
|
||||
</foreach>
|
||||
</insert>
|
||||
|
||||
|
||||
<!-- join删除-->
|
||||
<!-- # delete t_dynamic_rule, t_protect_object_dynamic_rule_conn-->
|
||||
<!-- # from t_dynamic_rule-->
|
||||
<!-- # inner join t_protect_object_dynamic_rule_conn-->
|
||||
<!-- # on t_dynamic_rule.dynamic_rule_id = t_protect_object_dynamic_rule_conn.dynamic_rule_id-->
|
||||
<!-- # where t_dynamic_rule.dynamic_rule_id = #{dynamicRuleId}-->
|
||||
<delete id="deleteDynamicRuleObject">
|
||||
delete from t_dynamic_rule
|
||||
where dynamic_rule_id = #{dynamicRuleId}
|
||||
</delete>
|
||||
<!-- 用于update时删去之前的关联-->
|
||||
<delete id="deleteDynamicRuleProtectObjectConcat">
|
||||
delete from t_protect_object_dynamic_rule_conn
|
||||
where dynamic_rule_id = #{dynamicRuleId}
|
||||
</delete>
|
||||
|
||||
<delete id="deleteDynamicRules">
|
||||
delete from t_dynamic_rule
|
||||
where dynamic_rule_id in
|
||||
<foreach collection="dynamicRuleIds" item="id" open="(" separator="," close=")">
|
||||
#{id}
|
||||
</foreach>
|
||||
</delete>
|
||||
|
||||
|
||||
<update id="updateDynamicRuleObject">
|
||||
update t_dynamic_rule
|
||||
set
|
||||
<if test="object.dynamicRuleName != null"> dynamic_rule_name = #{object.dynamicRuleName},</if>
|
||||
<if test="object.dynamicRuleModifyTime != null"> modify_time = #{object.dynamicRuleModifyTime},</if>
|
||||
<if test="object.dynamicRuleSourceSystem != null"> dynamic_rule_source_system = #{object.dynamicRuleSourceSystem},</if>
|
||||
<if test="object.dynamicRuleEventType != null"> dynamic_rule_event_type = #{object.dynamicRuleEventType},</if>
|
||||
<if test="object.dynamicRuleProtectLevel != null"> dynamic_rule_protec_level = #{object.dynamicRuleProtectLevel},</if>
|
||||
<if test="object.dynamicRulePriority != null"> dynamic_rule_priority = #{object.dynamicRulePriority},</if>
|
||||
<if test="object.dynamicRuleRange != null"> dynamic_rule_range = #{object.dynamicRuleRange},</if>
|
||||
<if test="object.dynamicRuleFrequency != null"> dynamic_rule_frequency = #{object.dynamicRuleFrequency}</if>
|
||||
where
|
||||
dynamic_rule_id = #{dynamicRuleId}
|
||||
</update>
|
||||
|
||||
|
||||
<resultMap id="dynamicRuleMap" type="com.realtime.protection.configuration.entity.rule.dynamicrule.DynamicRuleObject">
|
||||
<id column="dynamic_rule_id" property="dynamicRuleId"/>
|
||||
<result column="dynamic_rule_name" property="dynamicRuleName"/>
|
||||
<result column="create_time" property="dynamicRuleCreateTime"/>
|
||||
<result column="modify_time" property="dynamicRuleModifyTime"/>
|
||||
<result column="dynamic_rule_create_username" property="dynamicRuleCreateUsername"/>
|
||||
<result column="dynamic_rule_create_depart" property="dynamicRuleCreateDepart"/>
|
||||
<result column="dynamic_rule_source_system" property="dynamicRuleSourceSystem"/>
|
||||
<result column="dynamic_rule_event_type" property="dynamicRuleEventType"/>
|
||||
<result column="dynamic_rule_protec_level" property="dynamicRuleProtectLevel"/>
|
||||
<result column="dynamic_rule_priority" property="dynamicRulePriority"/>
|
||||
<result column="dynamic_rule_range" property="dynamicRuleRange"/>
|
||||
<result column="dynamic_rule_frequency" property="dynamicRuleFrequency"/>
|
||||
<result column="dynamic_rule_create_user_id" property="dynamicRuleCreateUserId"/>
|
||||
</resultMap>
|
||||
|
||||
<select id="queryDynamicRuleById" resultMap="dynamicRuleMap">
|
||||
select *
|
||||
from t_dynamic_rule dynamicRule
|
||||
where dynamicRule.dynamic_rule_id = #{dynamicRuleId}
|
||||
|
||||
</select>
|
||||
|
||||
<resultMap id="protectObjectMap" type="com.realtime.protection.configuration.entity.defense.object.ProtectObject">
|
||||
<id column="protect_object_id" property="protectObjectId"/>
|
||||
<result column="protect_object_name" property="protectObjectName"/>
|
||||
<result column="protect_object_system_name" property="protectObjectSystemName"/>
|
||||
<result column="protect_object_ip" property="protectObjectIPAddress"/>
|
||||
<result column="protect_object_port" property="protectObjectPort"/>
|
||||
<result column="protect_object_url" property="protectObjectURL"/>
|
||||
<result column="protect_object_protocol" property="protectObjectProtocol"/>
|
||||
<result column="protect_object_audit_status" property="protectObjectAuditStatus"/>
|
||||
</resultMap>
|
||||
|
||||
<select id="queryProtectObjectByRuleId"
|
||||
resultMap="protectObjectMap">
|
||||
select t_protect_object.*
|
||||
from t_protect_object
|
||||
inner join t_protect_object_dynamic_rule_conn
|
||||
on t_protect_object.protect_object_id = t_protect_object_dynamic_rule_conn.protect_object_id
|
||||
where t_protect_object_dynamic_rule_conn.dynamic_rule_id = #{dynamicRuleId}
|
||||
</select>
|
||||
|
||||
<select id="queryDynamicRuleObject"
|
||||
resultMap="dynamicRuleMap">
|
||||
select *
|
||||
from t_dynamic_rule
|
||||
<where>
|
||||
<if test="dynamicRuleName != null">
|
||||
dynamic_rule_name like concat('%', #{dynamicRuleName}, '%')
|
||||
</if>
|
||||
<if test="dynamicRuleId != null">
|
||||
and dynamic_rule_id = #{dynamicRuleId}
|
||||
</if>
|
||||
</where>
|
||||
LIMIT ${(page - 1) * pageSize}, #{pageSize}
|
||||
</select>
|
||||
|
||||
|
||||
</mapper>
|
||||
@@ -14,17 +14,37 @@
|
||||
static_rule_dip, static_rule_mdip, static_rule_dport, static_rule_mdport,
|
||||
static_rule_protocol, static_rule_mprotocol, static_rule_dns,
|
||||
static_rule_url, static_rule_priority, static_rule_range,
|
||||
static_rule_frequency, static_rule_protect_level, static_rule_audit_status)
|
||||
static_rule_frequency, static_rule_audit_status)
|
||||
values (#{object.staticRuleName}, #{object.staticRuleCreateTime}, #{object.staticRuleCreateUsername},
|
||||
#{object.staticRuleCreateDepart}, #{object.staticRuleCreateUserId}, INET_ATON(#{object.staticRuleSip}),
|
||||
INET_ATON(#{object.staticRuleMsip}), #{object.staticRuleSport}, #{object.staticRuleMsport},
|
||||
INET_ATON(#{object.staticRuleDip}), INET_ATON(#{object.staticRuleMdip}), #{object.staticRuleDport},
|
||||
#{object.staticRuleMdport}, #{object.staticRuleProtocol}, #{object.staticRuleMprotocol},
|
||||
#{object.staticRuleDns}, #{object.staticRuleURL}, #{object.staticRulePriority},
|
||||
#{object.staticRuleRange}, #{object.staticRuleFrequency}, #{object.staticRuleProtectLevel},
|
||||
#{object.staticRuleRange}, #{object.staticRuleFrequency},
|
||||
#{object.staticRuleAuditStatus})
|
||||
</insert>
|
||||
|
||||
<insert id="newStaticRules">
|
||||
insert into t_static_rule(static_rule_name, static_rule_create_time,
|
||||
static_rule_create_username, static_rule_create_depart,
|
||||
static_rule_create_user_id, static_rule_sip, static_rule_msip,
|
||||
static_rule_sport, static_rule_msport,
|
||||
static_rule_dip, static_rule_mdip, static_rule_dport, static_rule_mdport,
|
||||
static_rule_protocol, static_rule_mprotocol, static_rule_dns,
|
||||
static_rule_url, static_rule_priority, static_rule_range,
|
||||
static_rule_frequency, static_rule_audit_status)
|
||||
values
|
||||
<foreach collection="staticRuleBatch" item="object" separator=",">
|
||||
(#{object.staticRuleName}, #{object.staticRuleCreateTime}, #{object.staticRuleCreateUsername},
|
||||
#{object.staticRuleCreateDepart}, #{object.staticRuleCreateUserId}, INET_ATON(#{object.staticRuleSip}),
|
||||
INET_ATON(#{object.staticRuleMsip}), #{object.staticRuleSport}, #{object.staticRuleMsport},
|
||||
INET_ATON(#{object.staticRuleDip}), INET_ATON(#{object.staticRuleMdip}), #{object.staticRuleDport},
|
||||
#{object.staticRuleMdport}, #{object.staticRuleProtocol}, #{object.staticRuleMprotocol},
|
||||
#{object.staticRuleDns}, #{object.staticRuleURL}, #{object.staticRulePriority},
|
||||
#{object.staticRuleRange}, #{object.staticRuleFrequency},
|
||||
0)
|
||||
</foreach>
|
||||
</insert>
|
||||
|
||||
|
||||
<update id="updateStaticRule">
|
||||
@@ -54,12 +74,13 @@
|
||||
<if test="staticRuleFrequency != null and staticRuleFrequency != ''">
|
||||
static_rule_frequency = #{staticRuleFrequency},
|
||||
</if>
|
||||
<if test="staticRuleProtectLevel != null and staticRuleProtectLevel != ''">
|
||||
static_rule_protect_level = #{staticRuleProtectLevel},
|
||||
</if>
|
||||
|
||||
</set>
|
||||
where static_rule_id = #{staticRuleId}
|
||||
</update>
|
||||
<delete id="deleteStaticRules">
|
||||
|
||||
</delete>
|
||||
|
||||
<resultMap id="staticRuleMap" type="com.realtime.protection.configuration.entity.rule.staticrule.StaticRuleObject">
|
||||
<id column="static_rule_id" property="staticRuleId"/>
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.realtime.protection.server.rule.dynamic;
|
||||
|
||||
import com.realtime.protection.configuration.entity.rule.dynamicrule.DynamicRuleObject;
|
||||
import com.realtime.protection.server.rule.dynamicrule.DynamicRuleService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@SpringBootTest
|
||||
public class DynamicRuleServiceTest {
|
||||
private final DynamicRuleService dynamicRuleService;
|
||||
@Autowired
|
||||
public DynamicRuleServiceTest(DynamicRuleService dynamicRuleService) {
|
||||
this.dynamicRuleService = dynamicRuleService;
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNewDynamicRule() {
|
||||
DynamicRuleObject object = new DynamicRuleObject();
|
||||
object.setDynamicRuleName("UpdateDynamicRule2");
|
||||
object.setDynamicRuleEventType(1);
|
||||
object.setDynamicRuleFrequency(1);
|
||||
object.setDynamicRulePriority(1);
|
||||
object.setDynamicRuleRange("北京");
|
||||
object.setDynamicRuleProtectLevel(2);
|
||||
object.setDynamicRuleSourceSystem(1);
|
||||
object.setProtectObjectIds(List.of(new Integer[]{5521, 5520}));
|
||||
|
||||
|
||||
Integer objectId = dynamicRuleService.newDynamicRuleObject(object);
|
||||
assertTrue(objectId > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteDynamicRule() {
|
||||
dynamicRuleService.deleteDynamicRuleObject(8);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testQueryDynamicRule() {
|
||||
DynamicRuleObject object = dynamicRuleService.queryDynamicRuleById(9);
|
||||
System.out.println(object);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateDynamicRule() {
|
||||
DynamicRuleObject object = new DynamicRuleObject();
|
||||
object.setDynamicRuleName("UpdateDynamicRule2");
|
||||
object.setDynamicRuleEventType(1);
|
||||
object.setDynamicRuleFrequency(1);
|
||||
object.setDynamicRulePriority(1);
|
||||
object.setDynamicRuleRange("北京");
|
||||
object.setDynamicRuleProtectLevel(2);
|
||||
object.setDynamicRuleSourceSystem(1);
|
||||
object.setProtectObjectIds(List.of(new Integer[]{5521, 5520}));
|
||||
|
||||
dynamicRuleService.updateDynamicRuleObject(9, object);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testQueryDynamicRuleObject() {
|
||||
List<DynamicRuleObject> objects = dynamicRuleService.queryDynamicRuleObject( null,null,1, 10);
|
||||
System.out.println(objects);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.realtime.protection.server.rule.staticrule;
|
||||
|
||||
import com.realtime.protection.configuration.entity.rule.staticrule.StaticRuleObject;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
@@ -13,8 +14,31 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest
|
||||
public class StaticRuleServiceTest {
|
||||
private final StaticRuleService staticRuleService;
|
||||
private StaticRuleObject staticRuleTest;
|
||||
@Autowired
|
||||
private StaticRuleService staticRuleService;
|
||||
public StaticRuleServiceTest(StaticRuleService staticRuleService) {
|
||||
this.staticRuleService = staticRuleService;
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
staticRuleTest = new StaticRuleObject();
|
||||
staticRuleTest.setStaticRuleName("test_staticrule");
|
||||
staticRuleTest.setStaticRuleCreateTime(LocalDateTime.now());
|
||||
staticRuleTest.setStaticRuleCreateUsername("mh");
|
||||
staticRuleTest.setStaticRuleCreateDepart("mmeess");
|
||||
staticRuleTest.setStaticRuleCreateUserId(2);
|
||||
staticRuleTest.setStaticRuleAuditStatus(0);
|
||||
|
||||
staticRuleTest.setStaticRuleSip("1.1.2.3");
|
||||
staticRuleTest.setStaticRuleSport(80);
|
||||
|
||||
staticRuleTest.setStaticRulePriority(1);
|
||||
staticRuleTest.setStaticRuleFrequency(1);
|
||||
staticRuleTest.setStaticRuleRange("北京");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNewStaticRule(){
|
||||
@@ -34,12 +58,21 @@ public class StaticRuleServiceTest {
|
||||
object.setStaticRulePriority(2);
|
||||
object.setStaticRuleFrequency(1);
|
||||
object.setStaticRuleRange("北京");
|
||||
object.setStaticRuleProtectLevel(2);
|
||||
// object.setStaticRuleProtectLevel(2);
|
||||
|
||||
Integer id = staticRuleService.newStaticRuleObject(object);
|
||||
}
|
||||
//assertTrue(id>0);
|
||||
}
|
||||
@Test
|
||||
void testNewStaticRules(){
|
||||
List<StaticRuleObject> staticRuleObjects = new ArrayList<>();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
staticRuleObjects.add(staticRuleTest);
|
||||
}
|
||||
Boolean success = staticRuleService.newStaticRuleObjects(staticRuleObjects);
|
||||
assertTrue(success);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteStaticRule(){
|
||||
@@ -48,7 +81,7 @@ public class StaticRuleServiceTest {
|
||||
list.add(1);
|
||||
while(i<2) {
|
||||
i++;
|
||||
staticRuleService.deleteStaticRule(list);
|
||||
staticRuleService.deleteStaticRules(list);
|
||||
}
|
||||
}
|
||||
@Test
|
||||
@@ -68,7 +101,7 @@ public class StaticRuleServiceTest {
|
||||
object.setStaticRulePriority(2);
|
||||
object.setStaticRuleFrequency(1);
|
||||
object.setStaticRuleRange("北京");
|
||||
object.setStaticRuleProtectLevel(2);
|
||||
// object.setStaticRuleProtectLevel(2);
|
||||
|
||||
staticRuleService.updateStaticRule(object);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user