1. 添加防护对象文件上传下载配置

2. 修改SqlSessionWrapper,添加注释
This commit is contained in:
松岳 陈
2024-01-07 17:54:28 +08:00
parent 0fb8dd87fe
commit db02907f0a
8 changed files with 85 additions and 33 deletions

View File

@@ -23,10 +23,10 @@ repositories {
dependencies { dependencies {
// SpringBoot原生依赖 // SpringBoot原生依赖
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.3' implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.3'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
compileOnly 'org.projectlombok:lombok' compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools' developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.mysql:mysql-connector-j' runtimeOnly 'com.mysql:mysql-connector-j'

View File

@@ -1,5 +1,7 @@
package com.realtime.protection.configuration.entity.defense.object; package com.realtime.protection.configuration.entity.defense.object;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Max; import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min; import jakarta.validation.constraints.Min;
@@ -10,42 +12,53 @@ import lombok.Data;
@Data @Data
public class ProtectObject { public class ProtectObject {
@JsonProperty("proobj_id") @JsonProperty("proobj_id")
@ExcelIgnore
private Integer protectObjectId; private Integer protectObjectId;
@JsonProperty("proobj_name") @JsonProperty("proobj_name")
@NotNull(message = "proobj_name should not be empty.") @NotNull(message = "proobj_name should not be empty.")
@ExcelProperty("名称")
private String protectObjectName; private String protectObjectName;
@JsonProperty("proobj_system_name") @JsonProperty("proobj_system_name")
@ExcelProperty("操作系统名称")
private String protectObjectSystemName; private String protectObjectSystemName;
@JsonProperty("proobj_ip_address") @JsonProperty("proobj_ip_address")
@Pattern(regexp = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$", message = "Invalid IPv4 Address") @Pattern(regexp = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$", message = "Invalid IPv4 Address")
@ExcelProperty("IP地址")
private String protectObjectIPAddress; private String protectObjectIPAddress;
@JsonProperty("proobj_port") @JsonProperty("proobj_port")
@NotNull(message = "proobj_port should not be empty.") @NotNull(message = "proobj_port should not be empty.")
@Max(value = 65535, message = "port should not be more than 65535") @Max(value = 65535, message = "port should not be more than 65535")
@Min(value = 1, message = "port should not be less than 1") @Min(value = 1, message = "port should not be less than 1")
@ExcelProperty("端口")
private Integer protectObjectPort; private Integer protectObjectPort;
@JsonProperty("proobj_url") @JsonProperty("proobj_url")
@NotNull(message = "proobj_url should not be empty.") @NotNull(message = "proobj_url should not be empty.")
@ExcelProperty("URL")
private String protectObjectURL; private String protectObjectURL;
@JsonProperty("proobj_protocol") @JsonProperty("proobj_protocol")
@NotNull(message = "proobj_protocol should not be empty.") @NotNull(message = "proobj_protocol should not be empty.")
@ExcelProperty("协议")
private String protectObjectProtocol; private String protectObjectProtocol;
@JsonProperty("proobj_audit_status") @JsonProperty("proobj_audit_status")
@ExcelIgnore
private Integer protectObjectAuditStatus; private Integer protectObjectAuditStatus;
@JsonProperty("proobj_create_username") @JsonProperty("proobj_create_username")
@ExcelIgnore
private String protectObjectCreateUsername; private String protectObjectCreateUsername;
@JsonProperty("proobj_create_depart") @JsonProperty("proobj_create_depart")
@ExcelIgnore
private String protectObjectCreateDepart; private String protectObjectCreateDepart;
@JsonProperty("proobj_create_userid") @JsonProperty("proobj_create_userid")
@ExcelIgnore
private Integer protectObjectCreateUserId; private Integer protectObjectCreateUserId;
} }

View File

@@ -16,13 +16,22 @@ public class SqlSessionWrapper {
this.sqlSessionFactory = sqlSessionFactory; this.sqlSessionFactory = sqlSessionFactory;
} }
/** 启动批量SQL会话
* @param mapperClass MyBatis Mapper类型
* @param batchFunction 批量函数(批量添加、批量删除、批量更新等)
* @param arguments 函数附带的所有参数可以使用Map进行包装
* @param <M> Mapper class
* @param <I> Function input
* @param <O> Function output
* @return 被包装的批量函数返回值
*/
public <M, I, O> O startBatchSession(Class<M> mapperClass, public <M, I, O> O startBatchSession(Class<M> mapperClass,
Function<M, Function<I, O>> wrappedFunction, Function<M, Function<I, O>> batchFunction,
I arguments) { I arguments) {
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false); SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
M mapper = sqlSession.getMapper(mapperClass); M mapper = sqlSession.getMapper(mapperClass);
try { try {
O result = wrappedFunction.apply(mapper).apply(arguments); O result = batchFunction.apply(mapper).apply(arguments);
sqlSession.commit(); sqlSession.commit();
sqlSession.clearCache(); sqlSession.clearCache();

View File

@@ -5,7 +5,6 @@ import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.ListUtils;
import com.realtime.protection.configuration.entity.defense.object.ProtectObject; import com.realtime.protection.configuration.entity.defense.object.ProtectObject;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List; import java.util.List;
@@ -36,7 +35,7 @@ public class ProjectObjectDataListener implements ReadListener<ProtectObject> {
private void saveData() { private void saveData() {
Boolean success = protectObjectService.newProtectObjects(cachedDataList); Boolean success = protectObjectService.newProtectObjects(cachedDataList);
if (!success) { if (!success) {
throw new RuntimeException("Error reading data in newProtectObjects"); throw new RuntimeException("Error reading data in /proobj/new");
} }
} }
} }

View File

@@ -1,15 +1,18 @@
package com.realtime.protection.server.defense.object; package com.realtime.protection.server.defense.object;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.realtime.protection.configuration.entity.defense.object.ProtectObject; import com.realtime.protection.configuration.entity.defense.object.ProtectObject;
import com.realtime.protection.configuration.response.ResponseResult; import com.realtime.protection.configuration.response.ResponseResult;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import jakarta.validation.constraints.Min; import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.IOException; import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
@RestController @RestController
@@ -38,13 +41,25 @@ public class ProtectObjectController {
.setData("success", true); .setData("success", true);
} }
@PutMapping("/new") @PostMapping("/upload")
public ResponseResult newProtectObjectFromFile(MultipartFile updateFile) throws IOException { public ResponseResult uploadFile(MultipartFile uploadFile) throws IOException {
EasyExcel.read(updateFile.getInputStream(), ProtectObject.class, EasyExcel.read(uploadFile.getInputStream(), ProtectObject.class,
new ProjectObjectDataListener(protectObjectService)).sheet().doRead(); new ProjectObjectDataListener(protectObjectService)).sheet().doRead();
return ResponseResult.ok(); 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(), ProtectObject.class)
.sheet("防护对象")
.doWrite(List.of());
}
@GetMapping("/query") @GetMapping("/query")
public ResponseResult queryProtectObjects(@RequestParam(value = "proobj_name", required = false) public ResponseResult queryProtectObjects(@RequestParam(value = "proobj_name", required = false)
String protectObjectName, String protectObjectName,
@@ -88,7 +103,7 @@ public class ProtectObjectController {
} }
@PostMapping("/delete") @PostMapping("/delete")
public ResponseResult deleteProtectObject(@RequestBody @JsonProperty("proobj_ids") List<Integer> protectObjectIds) { public ResponseResult deleteProtectObject(@RequestBody List<Integer> protectObjectIds) {
return ResponseResult.ok() return ResponseResult.ok()
.setData("proobj_ids", protectObjectIds) .setData("proobj_ids", protectObjectIds)
.setData("success", protectObjectService.deleteProtectObjects(protectObjectIds)); .setData("success", protectObjectService.deleteProtectObjects(protectObjectIds));

View File

@@ -23,7 +23,7 @@ public interface ProtectObjectMapper {
Boolean deleteProtectObject(@Param("proobj_id") Integer protectObjectId); Boolean deleteProtectObject(@Param("proobj_id") Integer protectObjectId);
Boolean deleteProtectObjects(@Param("proobj_ids") List<Integer> protectObjectIds); void deleteProtectObjects(@Param("proobj_ids") List<Integer> protectObjectIds);
Boolean changeProtectObjectAuditStatus(@Param("proobj_id") Integer protectObjectId, Boolean changeProtectObjectAuditStatus(@Param("proobj_id") Integer protectObjectId,
@Param("proobj_audit_status") Integer protectObjectAuditStatus); @Param("proobj_audit_status") Integer protectObjectAuditStatus);

View File

@@ -4,6 +4,7 @@ import com.alibaba.excel.util.ListUtils;
import com.realtime.protection.configuration.entity.defense.object.ProtectObject; import com.realtime.protection.configuration.entity.defense.object.ProtectObject;
import com.realtime.protection.configuration.utils.AuditStatusValidator; import com.realtime.protection.configuration.utils.AuditStatusValidator;
import com.realtime.protection.configuration.utils.SqlSessionWrapper; import com.realtime.protection.configuration.utils.SqlSessionWrapper;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@@ -34,16 +35,22 @@ public class ProtectObjectService {
public Boolean newProtectObjects(List<ProtectObject> protectObjectList) { public Boolean newProtectObjects(List<ProtectObject> protectObjectList) {
Function<ProtectObjectMapper, Function<List<ProtectObject>, Boolean>> newProtectObjectFunction = mapper -> list -> { Function<ProtectObjectMapper, Function<List<ProtectObject>, Boolean>> newProtectObjectFunction = mapper -> list -> {
if (list == null || list.isEmpty()) {
return false;
}
List<ProtectObject> protectObjectBatch = ListUtils.newArrayListWithExpectedSize(100); List<ProtectObject> protectObjectBatch = ListUtils.newArrayListWithExpectedSize(100);
for (ProtectObject protectObject : protectObjectList) { for (ProtectObject protectObject : protectObjectList) {
protectObjectBatch.add(protectObject); protectObjectBatch.add(protectObject);
if (protectObjectBatch.size() < 1000) { if (protectObjectBatch.size() < 100) {
continue; continue;
} }
mapper.newProtectObjects(protectObjectBatch); mapper.newProtectObjects(protectObjectBatch);
protectObjectBatch.clear(); protectObjectBatch.clear();
} }
if (!protectObjectBatch.isEmpty()) {
mapper.newProtectObjects(protectObjectBatch); mapper.newProtectObjects(protectObjectBatch);
}
return true; return true;
}; };
@@ -67,10 +74,12 @@ public class ProtectObjectService {
} }
public Boolean deleteProtectObjects(List<Integer> protectObjectIds) { public Boolean deleteProtectObjects(List<Integer> protectObjectIds) {
Function<ProtectObjectMapper, Function<List<Integer>, Void>> deleteProtectObjectFunction = mapper -> list -> { Function<ProtectObjectMapper, Function<List<Integer>, Boolean>> deleteProtectObjectFunction = mapper -> list -> {
if (list == null || list.isEmpty()) { if (list == null || list.isEmpty()) {
return null; return false;
} }
boolean success = true;
Integer result;
List<Integer> protectObjectBatch = ListUtils.newArrayListWithExpectedSize(100); List<Integer> protectObjectBatch = ListUtils.newArrayListWithExpectedSize(100);
for (Integer protectObjectId : list) { for (Integer protectObjectId : list) {
@@ -78,15 +87,16 @@ public class ProtectObjectService {
if (protectObjectBatch.size() < 100) { if (protectObjectBatch.size() < 100) {
continue; continue;
} }
mapper.deleteProtectObjects(protectObjectIds); mapper.deleteProtectObjects(protectObjectBatch);
protectObjectBatch.clear(); protectObjectBatch.clear();
} }
mapper.deleteProtectObjects(protectObjectBatch); if (!protectObjectBatch.isEmpty()) {
return null; mapper.deleteProtectObjects(protectObjectBatch);;
}
return success;
}; };
sqlSessionWrapper.startBatchSession(ProtectObjectMapper.class, deleteProtectObjectFunction, protectObjectIds); return sqlSessionWrapper.startBatchSession(ProtectObjectMapper.class, deleteProtectObjectFunction, protectObjectIds);
return true;
} }
@Transactional @Transactional

View File

@@ -24,12 +24,6 @@ class ProtectObjectServiceTest {
this.protectObjectService = protectObjectService; this.protectObjectService = protectObjectService;
} }
@Test
void testDeleteProtectObject() {
Boolean success = protectObjectService.deleteProtectObjects(List.of(1, 2, 3, 4));
assertFalse(success);
}
@BeforeEach @BeforeEach
void setUp() { void setUp() {
protectObject = new ProtectObject(); protectObject = new ProtectObject();
@@ -56,7 +50,7 @@ class ProtectObjectServiceTest {
@Test @Test
void newProtectObjects() { void newProtectObjects() {
List<ProtectObject> protectObjects = new ArrayList<>(); List<ProtectObject> protectObjects = new ArrayList<>();
for (int i = 0; i < 100; i++) { for (int i = 0; i < 1000; i++) {
protectObjects.add(protectObject); protectObjects.add(protectObject);
} }
@@ -66,10 +60,12 @@ class ProtectObjectServiceTest {
@Test @Test
void updateProtectObject() { void updateProtectObject() {
Integer testId = 300;
protectObject.setProtectObjectName("x-1-1"); protectObject.setProtectObjectName("x-1-1");
protectObject.setProtectObjectId(10); protectObject.setProtectObjectId(testId);
assertTrue(protectObjectService.updateProtectObject(protectObject)); assertTrue(protectObjectService.updateProtectObject(protectObject));
assertEquals("x-1-1", protectObjectService.queryProtectObject(10).getProtectObjectName()); assertEquals("x-1-1", protectObjectService.queryProtectObject(testId).getProtectObjectName());
} }
@Test @Test
@@ -81,16 +77,26 @@ class ProtectObjectServiceTest {
break; break;
} }
} }
assertTrue(protectObjectService.deleteProtectObject(testNum)); assertTrue(protectObjectService.deleteProtectObject(testNum));
assertNull(protectObjectService.queryProtectObject(testNum)); assertNull(protectObjectService.queryProtectObject(testNum));
} }
@Test @Test
void deleteProtectObjects() { void deleteProtectObjects() {
assertTrue(protectObjectService.deleteProtectObjects(List.of(270, 271))); ArrayList<Integer> testNums = new ArrayList<>();
assertNull(protectObjectService.queryProtectObject(270)); for (int i = 0; i < 100000; i++ ) {
assertNull(protectObjectService.queryProtectObject(271)); if (protectObjectService.queryProtectObject(i) != null) {
testNums.add(i);
if (testNums.size() > 5) {
break;
}
}
}
assertTrue(protectObjectService.deleteProtectObjects(testNums));
for (Integer testNum : testNums) {
assertNull(protectObjectService.queryProtectObject(testNum));
}
} }
@Test @Test