1.完成的excel批量新增、按id删除、批量删除、按id查询、分页查询、按id新增、修改审核状态

(excel批量新增还未http请求测试)
(批量新增、批量删除、修改审核状态已复用sy代码)
2、查询ip是否存在于白名单功能初步实现,仍不完善
This commit is contained in:
Hao Miao
2024-01-08 00:22:14 +08:00
parent db02907f0a
commit 1e9fe37d0d
10 changed files with 524 additions and 21 deletions

View File

@@ -1,33 +1,49 @@
package com.realtime.protection.configuration.entity.whitelist;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import lombok.Data;
@Data
public class WhiteListObject {
@JsonProperty("whiteobj_id")
@ExcelIgnore
private int whiteListId;
@NotNull
@JsonProperty("whiteobj_name")
@ExcelProperty("名称")
private String whiteListName;
@JsonProperty("whiteobj_system_name")
@ExcelProperty("系统名称")
private String whiteListSystemName;
@JsonProperty("whiteobj_ip_address")
@Pattern(regexp = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$", message = "Invalid IPv4 Address")
@ExcelProperty("IP地址")
private String whiteListIP;
@JsonProperty("whiteobj_port")
@Max(value = 65535, message = "port should not be more than 65535")
@Min(value = 1, message = "port should not be less than 1")
@ExcelProperty("端口")
private int whiteListPort;
@JsonProperty("whiteobj_url")
@ExcelProperty("URL")
private String whiteListUrl;
@JsonProperty("whiteobj_protocol")
@ExcelProperty("协议")
private String whiteListProtocol;
@JsonProperty("audit_status")
@ExcelIgnore
private String whiteListAuditStatus;
}

View File

@@ -38,8 +38,15 @@ public class StaticRuleController {
public ResponseResult delete(@PathVariable List<Integer> ids){
log.info("根据id删除静态规则:{}",ids);
//调用service删除
staticRuleService.deleteStaticRule(ids);
return ResponseResult.ok();
// 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);
}
/**

View File

@@ -9,22 +9,24 @@ import java.util.List;
@Mapper
public interface StaticRuleMapper {
/**
* 新建静态规则
*/
//新建静态规则
void newStaticRuleObject(@Param("object") StaticRuleObject object);
/**
* 根据主键删除菜品数据
*/
//根据主键删除静态规则
@Delete("delete from t_static_rule where static_rule_id = #{id}")
void deleteStaticRule(Integer id);
Boolean deleteStaticRule(Integer id);
//修改静态规则
void updateStaticRule(StaticRuleObject object);
//按id查询静态规则
//@Select("select * from t_static_rule where static_rule_id = #{id}")
StaticRuleObject queryStaticRuleById(Integer id);
//多页查询静态规则
List<StaticRuleObject> queryStaticRule(String static_rule_name, Integer static_rule_id,
Integer page, Integer pageSize);
}

View File

@@ -1,16 +1,20 @@
package com.realtime.protection.server.rule.staticrule;
import com.realtime.protection.configuration.entity.rule.staticrule.StaticRuleObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class StaticRuleService {
@Autowired
private StaticRuleMapper staticRuleMapper;
private final StaticRuleMapper staticRuleMapper;
public StaticRuleService(StaticRuleMapper staticRuleMapper) {
this.staticRuleMapper = staticRuleMapper;
}
/*
@@ -28,13 +32,16 @@ public class StaticRuleService {
return object.getStaticRuleId();
}
public void deleteStaticRule(List<Integer> ids) {
@Transactional
public Boolean deleteStaticRule(List<Integer> staticRuleIds) {
//判断当前静态规则是否能够删除---是否存在任务选择的静态规则??
//删除静态规则
for (Integer id : ids) {
staticRuleMapper.deleteStaticRule(id);
}
// for (Integer id : staticRuleIds) {
// staticRuleMapper.deleteStaticRule(id);
// }
return staticRuleIds.stream().allMatch(staticRuleMapper::deleteStaticRule);
}
@@ -47,6 +54,7 @@ public class StaticRuleService {
}
public StaticRuleObject queryStaticRuleById(Integer id) {
return staticRuleMapper.queryStaticRuleById(id);
}

View File

@@ -1,10 +1,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 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 org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
@RestController
@RequestMapping("/whiteobj")
@@ -25,4 +29,117 @@ public class WhiteListController {
.setData("whiteobj_id", whiteListObjectId)
.setData("success", true);
}
//以excel文件方式批量导入
@PostMapping("/upload")
public ResponseResult uploadFile(MultipartFile uploadFile) throws IOException {
EasyExcel.read(uploadFile.getInputStream(), WhiteListObject.class,
new WhiteListDataListener(whiteListService)).sheet().doRead();
return ResponseResult.ok();
}
@RequestMapping("/query")
public ResponseResult queryWhiteListObject(@RequestParam(value = "whiteobj_name", required = false)String whiteListName,
@RequestParam(value = "page_size", required = false)Integer whiteListId,
@RequestParam(value = "page", defaultValue = "1")Integer page,
@RequestParam(value = "page_size", defaultValue = "10")Integer pageSize) {
if (page <= 0 || pageSize <= 0) {
return new ResponseResult(400, "page or page_size is invalid")
.setData("whiteobj_list", null);
}
return ResponseResult.ok()
.setData("whiteobj_list", whiteListService.queryWhiteListObject(whiteListName, whiteListId, page, pageSize));
}
@RequestMapping("/{id}/query")
public ResponseResult queryWhiteListObjectById(@PathVariable Integer id) {
if (id <= 0) {
return new ResponseResult(400, "id is invalid")
.setData("whiteobj_list", null);
}
WhiteListObject whiteListObject = whiteListService.queryWhiteListObjectById(id);
if (whiteListObject == null) {
return new ResponseResult(400, "id is invalid")
.setData("whiteobj_list", null);
}
return ResponseResult.ok()
.setData("whiteobj_list", whiteListObject);
}
//根据id删除
@DeleteMapping("/{id}/delete")
public ResponseResult deleteWhiteListObjectById(@PathVariable Integer id) {
if (id <= 0) {
return new ResponseResult(400, "id is invalid")
.setData("whiteobj_id", id)
.setData("success", false);
}
Integer num = whiteListService.deleteWhiteListObjectById(id);
if (num == 0) {
return new ResponseResult(400, "id is invalid")
.setData("whiteobj_id", id)
.setData("success", false);
}
return ResponseResult.ok()
.setData("whiteobj_id", id)
.setData("success", true);
//还要return 白名单名称吗,还要在查表看他的名称
}
@PostMapping("/delete")
public ResponseResult deleteWhiteListObjects( @RequestBody List<Integer> whiteListObjIds) {
//Post不支持body为json。。。 body只能是[9,10]
return ResponseResult.ok()
.setData("whiteobj_list", whiteListObjIds)
.setData("success", whiteListService.deleteWhiteListObjects(whiteListObjIds));
}
@PostMapping ("/{id}/update")
public ResponseResult updateWhiteListObject(@PathVariable Integer id,
@RequestBody WhiteListObject object) {
object.setWhiteListId(id);
Integer num = whiteListService.updateWhiteListObject(object);
if (num == 0) {
return new ResponseResult(400, "id is invalid")
.setData("whiteobj_list", null);
}
return ResponseResult.ok();
}
//查询ip是否存在于白名单
@PostMapping("/exist")
public ResponseResult existWhiteListObject(@RequestBody List<Integer> ruleIds) {
//是请求规则的id然后判断这个id所属的ip是否在白名单中吗
//静态应该可以,但动态的,动态是实时过来告警信息,不存储规则? 存的话也行那这里要区分id是静态的还是动态的
//这里先走通静态的要获取规则的源IP和目的IP去白名单select看有没有(有的还有IP掩码,暂未实现)
//返回涉及IP在白名单中的id
List<String> ruleInWhiteListIds = whiteListService.existWhiteListObject(ruleIds);
return ResponseResult.ok()
.setData("ip_list", ruleInWhiteListIds);
}
//修改审核状态
@RequestMapping("/{id}/audit/{status}")
public ResponseResult updateWhiteListObjectAuditStatus(@PathVariable Integer id,
@PathVariable Integer status) {
if (id <= 0 || status < 0 || status > 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))
.setData("whiteobj_id", id);
}
}

View File

@@ -0,0 +1,51 @@
package com.realtime.protection.server.whitelist;
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.whitelist.WhiteListObject;
import lombok.extern.slf4j.Slf4j;
//import com.alibaba.fastjson2.JSON;
import java.util.List;
@Slf4j
public class WhiteListDataListener implements ReadListener<WhiteListObject> {
private static final int batchCount = 100;
private final List<WhiteListObject> cachedDataList = ListUtils.newArrayListWithExpectedSize(batchCount);
private final WhiteListService whiteListService;
public WhiteListDataListener(WhiteListService whiteListService) {
this.whiteListService = whiteListService;
}
@Override
public void invoke(WhiteListObject 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 = whiteListService.newWhiteListObjects(cachedDataList);
log.info("存储数据库成功!");
if (!success) {
throw new RuntimeException("Error reading data in /proobj/new");
}
}
}

View File

@@ -1,11 +1,35 @@
package com.realtime.protection.server.whitelist;
import com.realtime.protection.configuration.entity.rule.staticrule.StaticRuleObject;
import com.realtime.protection.configuration.entity.whitelist.WhiteListObject;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface WhiteListMapper {
//新建
void newWhiteListObject(@Param("object") WhiteListObject object);
//分页查询
List<WhiteListObject> queryWhiteListObject(String whiteListName, Integer whiteListId, Integer page, Integer pageSize);
//根据主键查询
WhiteListObject queryWhiteListObjectById(Integer id);
//根据主键删除
@Delete("delete from t_white_list where white_list_id = #{id}")
Integer deleteWhiteListObject(Integer id);
Integer updateWhiteListObject(@Param("object") WhiteListObject object);
String existWhiteListObject(@Param("staticRuleObject")StaticRuleObject staticRuleObject);
Integer queryWhiteListObjectAuditStuatusById(Integer id);
Boolean updateWhiteListObjectAuditStatus(Integer id, Integer status);
void newWhiteListObjects(@Param("whiteListObjects")List<WhiteListObject> whiteListBatch);
void deleteWhiteListObjects(@Param("whiteListIds") List<Integer> whiteListBatch);
}

View File

@@ -1,21 +1,151 @@
package com.realtime.protection.server.whitelist;
import com.alibaba.excel.util.ListUtils;
import com.realtime.protection.configuration.entity.rule.staticrule.StaticRuleObject;
import com.realtime.protection.configuration.entity.whitelist.WhiteListObject;
import com.realtime.protection.configuration.utils.AuditStatusValidator;
import com.realtime.protection.configuration.utils.SqlSessionWrapper;
import com.realtime.protection.server.rule.staticrule.StaticRuleMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
@Service
public class WhiteListService {
private final WhiteListMapper whiteListMapper;
private final StaticRuleMapper staticRuleMapper;
public WhiteListService(WhiteListMapper whiteListMapper) {
private final SqlSessionWrapper sqlSessionWrapper;
public WhiteListService(WhiteListMapper whiteListMapper,
StaticRuleMapper staticRuleMapper,
SqlSessionWrapper sqlSessionWrapper) {
this.whiteListMapper = whiteListMapper;
this.staticRuleMapper = staticRuleMapper;
this.sqlSessionWrapper = sqlSessionWrapper;
}
//新建一个whitelist
public Integer newWhiteListObject(WhiteListObject object) {
whiteListMapper.newWhiteListObject(object);
return object.getWhiteListId();
}
//批量新建多个whitelist
public Boolean newWhiteListObjects(List<WhiteListObject> whiteListObjectList) {
Function<WhiteListMapper, Function<List<WhiteListObject>, Boolean>> newWhiteListObjectFunction =
mapper -> list -> {
if (list == null || list.isEmpty()) {
return false;
}
List<WhiteListObject> WhiteListBatch = ListUtils.newArrayListWithExpectedSize(100);
for (WhiteListObject whiteListObject : whiteListObjectList) {
WhiteListBatch.add(whiteListObject);
if (WhiteListBatch.size() < 100) {
continue;
}
//mapper指的就是外层函数输入的参数也就是WhiteListMapper
mapper.newWhiteListObjects(WhiteListBatch);
WhiteListBatch.clear();
}
if (!WhiteListBatch.isEmpty()) {
mapper.newWhiteListObjects(WhiteListBatch);
}
return true;
};
//实现事务操作
return sqlSessionWrapper.startBatchSession(WhiteListMapper.class, newWhiteListObjectFunction, whiteListObjectList);
}
public Integer deleteWhiteListObjectById(Integer id) {
return whiteListMapper.deleteWhiteListObject(id);
}
//@Transactional
public Boolean deleteWhiteListObjects(List<Integer> whiteListObjIds) {
// for (Integer id : whiteobj_ids) {
// whiteListMapper.deleteWhiteListObject(id);
// }
Function<WhiteListMapper, Function<List<Integer>, Boolean>> deleteWhiteListObjectFunction =
mapper -> list -> {
if (list == null || list.isEmpty()) {
return false;
}
List<Integer> WhiteListBatch = ListUtils.newArrayListWithExpectedSize(100);
for (Integer whiteListObjId : list) {
WhiteListBatch.add(whiteListObjId);
if (WhiteListBatch.size() < 100) {
continue;
}
//mapper指的就是外层函数输入的参数也就是WhiteListMapper
mapper.deleteWhiteListObjects(WhiteListBatch);
WhiteListBatch.clear();
}
if (!WhiteListBatch.isEmpty()) {
mapper.deleteWhiteListObjects(WhiteListBatch);
}
return true;
};
//实现事务操作
return sqlSessionWrapper.startBatchSession(WhiteListMapper.class, deleteWhiteListObjectFunction, whiteListObjIds);
}
public Integer updateWhiteListObject(WhiteListObject object) {
return whiteListMapper.updateWhiteListObject(object);
}
public List<WhiteListObject> queryWhiteListObject(String whiteListName, Integer whiteListId,
Integer page, Integer pageSize) {
return whiteListMapper.queryWhiteListObject(whiteListName, whiteListId, page, pageSize);
}
public WhiteListObject queryWhiteListObjectById(Integer id) {
return whiteListMapper.queryWhiteListObjectById(id);
}
public List<String> existWhiteListObject(List<Integer> ruleIds) {
List<String> ip_list = new ArrayList<>();
for (Integer id : ruleIds) {
StaticRuleObject staticRuleObject = staticRuleMapper.queryStaticRuleById(id);
if (staticRuleObject != null) {
String whiteListIp = whiteListMapper.existWhiteListObject(staticRuleObject);
if (whiteListIp != null) {
ip_list.add(whiteListIp);
}
}
}
return ip_list;
}
@Transactional
public Map<String, Object> updateWhiteListObjectAuditStatus(Integer id, Integer auditStatus) {
//查询目前curStatus
Integer originalAuditStatus = whiteListMapper.queryWhiteListObjectAuditStuatusById(id);
//判断是否可以修改
if (!AuditStatusValidator.setOriginal(originalAuditStatus).checkValidate(auditStatus)) {
throw new IllegalArgumentException("invalid audit status");
}
Boolean success = whiteListMapper.updateWhiteListObjectAuditStatus(id, auditStatus);
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("success", success);
resultMap.put("audit_status", auditStatus);
return resultMap;
}
}