1、DynamicRule实现新增、删除、修改、id查询、分页查询功能。并在crud时与ProtectObject关联。

2、StaticRule添加批量导入、模板文件下载功能,使用sqlSessionWrapper重写批量删除
3、WhiteList添加模板文件下载功能
This commit is contained in:
Hao Miao
2024-01-11 11:41:50 +08:00
parent a04b83e4c3
commit 930ba8b5ac
15 changed files with 862 additions and 33 deletions

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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));
}
//详情查看?? 查看什么
}

View File

@@ -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");
}
}
}

View File

@@ -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);
}

View File

@@ -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);
}
}

View File

@@ -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));
}
/**

View File

@@ -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");
}
}
}

View File

@@ -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);
}

View File

@@ -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);
}
}

View File

@@ -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);
}