审计日志文件备份、文件恢复
This commit is contained in:
@@ -0,0 +1,27 @@
|
|||||||
|
package com.njcn.system.enums;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @version 1.0.0
|
||||||
|
* @author: chenchao
|
||||||
|
* @date: 2022/07/15 16:21
|
||||||
|
*/
|
||||||
|
public enum AuditLogEnum {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 审计日志模块异常响应码
|
||||||
|
*/
|
||||||
|
NOT_FIND_FILE("A0300", "文件未备份,请先备份文件"),
|
||||||
|
LOG_EXCEPTION("A0301", "导入旧日志文件异常")
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
|
private final String code;
|
||||||
|
|
||||||
|
private final String message;
|
||||||
|
|
||||||
|
AuditLogEnum(String code, String message) {
|
||||||
|
this.code = code;
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -7,7 +7,6 @@ import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
|||||||
import com.njcn.common.pojo.response.HttpResult;
|
import com.njcn.common.pojo.response.HttpResult;
|
||||||
import com.njcn.common.utils.HttpResultUtil;
|
import com.njcn.common.utils.HttpResultUtil;
|
||||||
import com.njcn.system.pojo.param.AuditParam;
|
import com.njcn.system.pojo.param.AuditParam;
|
||||||
import com.njcn.system.pojo.vo.AuditLogCusVO;
|
|
||||||
import com.njcn.system.pojo.vo.AuditLogVO;
|
import com.njcn.system.pojo.vo.AuditLogVO;
|
||||||
import com.njcn.system.service.AuditService;
|
import com.njcn.system.service.AuditService;
|
||||||
import com.njcn.web.controller.BaseController;
|
import com.njcn.web.controller.BaseController;
|
||||||
@@ -57,15 +56,24 @@ public class AuditController extends BaseController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
|
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
|
||||||
@PostMapping("/censusAuditLog")
|
@PostMapping("/recoverLogFile")
|
||||||
@ApiOperation("审计日志统计")
|
@ApiOperation("日志文件恢复")
|
||||||
@ApiImplicitParam(name = "auditParam", value = "审计日志参数", required = true)
|
public HttpResult recoverLogFile() {
|
||||||
public HttpResult<Page<AuditLogCusVO>> censusAuditLog(@RequestBody @Validated AuditParam auditParam){
|
String methodDescribe = getMethodDescribe("recoverLogFile");
|
||||||
String methodDescribe = getMethodDescribe("censusAuditLog");
|
boolean res = auditService.recoverLogFile();
|
||||||
Page<AuditLogCusVO> result = auditService.censusAuditLog(auditParam);
|
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,res,methodDescribe);
|
||||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,result,methodDescribe);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @OperateInfo(info = LogEnum.BUSINESS_COMMON)
|
||||||
|
// @PostMapping("/censusAuditLog")
|
||||||
|
// @ApiOperation("审计日志统计")
|
||||||
|
// @ApiImplicitParam(name = "auditParam", value = "审计日志参数", required = true)
|
||||||
|
// public HttpResult<Page<AuditLogCusVO>> censusAuditLog(@RequestBody @Validated AuditParam auditParam){
|
||||||
|
// String methodDescribe = getMethodDescribe("censusAuditLog");
|
||||||
|
// Page<AuditLogCusVO> result = auditService.censusAuditLog(auditParam);
|
||||||
|
// return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,result,methodDescribe);
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.njcn.system.mapper;
|
package com.njcn.system.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import com.njcn.system.pojo.dto.excel.UserLogExcel;
|
||||||
import com.njcn.system.pojo.param.AuditParam;
|
import com.njcn.system.pojo.param.AuditParam;
|
||||||
import com.njcn.system.pojo.po.UserLog;
|
import com.njcn.system.pojo.po.UserLog;
|
||||||
import org.apache.ibatis.annotations.Param;
|
import org.apache.ibatis.annotations.Param;
|
||||||
@@ -18,5 +20,9 @@ public interface AuditMapper {
|
|||||||
List<UserLog> selectAuditLog(@Param("auditParam") AuditParam auditParam);
|
List<UserLog> selectAuditLog(@Param("auditParam") AuditParam auditParam);
|
||||||
|
|
||||||
int getCount(@Param("auditParam") AuditParam auditParam);
|
int getCount(@Param("auditParam") AuditParam auditParam);
|
||||||
|
/**
|
||||||
|
* 查询需要备份的审计日志
|
||||||
|
*/
|
||||||
|
List<UserLogExcel> queryExportUser(@Param("ew") QueryWrapper<UserLogExcel> queryWrapper);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
SELECT
|
SELECT
|
||||||
id,
|
id,
|
||||||
user_name userName,
|
user_name userName,
|
||||||
|
login_name loginName,
|
||||||
ip,
|
ip,
|
||||||
operate,
|
operate,
|
||||||
operate_type operateType,
|
operate_type operateType,
|
||||||
@@ -43,4 +44,27 @@
|
|||||||
AND id >= (select id from sys_user_log order by id limit #{auditParam.pageNum}, 1) limit #{auditParam.pageSize}
|
AND id >= (select id from sys_user_log order by id limit #{auditParam.pageNum}, 1) limit #{auditParam.pageSize}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<select id="queryExportUser" resultType="UserLogExcel">
|
||||||
|
SELECT
|
||||||
|
id,
|
||||||
|
login_name loginName,
|
||||||
|
user_name userName,
|
||||||
|
ip,
|
||||||
|
operate,
|
||||||
|
operate_type operateType,
|
||||||
|
result,
|
||||||
|
fail_reason failReason,
|
||||||
|
level,
|
||||||
|
type,
|
||||||
|
service_name serviceName,
|
||||||
|
state,
|
||||||
|
create_by createBy,
|
||||||
|
create_time createTime,
|
||||||
|
update_by updateBy,
|
||||||
|
update_time updateTime
|
||||||
|
FROM sys_user_log
|
||||||
|
Where ${ew.sqlSegment}
|
||||||
|
</select>
|
||||||
|
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
@@ -24,6 +24,11 @@ public interface AuditService extends IService<UserLog> {
|
|||||||
*/
|
*/
|
||||||
void logFileWriter();
|
void logFileWriter();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日志文件数据恢复
|
||||||
|
*/
|
||||||
|
boolean recoverLogFile();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 分页获取审计日志统计列表
|
* 分页获取审计日志统计列表
|
||||||
* @param auditParam
|
* @param auditParam
|
||||||
|
|||||||
@@ -1,22 +1,38 @@
|
|||||||
package com.njcn.system.service.impl;
|
package com.njcn.system.service.impl;
|
||||||
|
|
||||||
|
import cn.afterturn.easypoi.excel.entity.ImportParams;
|
||||||
|
import cn.afterturn.easypoi.excel.imports.ExcelImportService;
|
||||||
|
import cn.afterturn.easypoi.exception.excel.ExcelImportException;
|
||||||
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.njcn.common.pojo.exception.BusinessException;
|
||||||
|
import com.njcn.poi.excel.ExcelUtil;
|
||||||
import com.njcn.system.config.SystemBaseConfig;
|
import com.njcn.system.config.SystemBaseConfig;
|
||||||
|
import com.njcn.system.enums.AuditLogEnum;
|
||||||
import com.njcn.system.mapper.AuditMapper;
|
import com.njcn.system.mapper.AuditMapper;
|
||||||
import com.njcn.system.mapper.UserLogMapper;
|
import com.njcn.system.mapper.UserLogMapper;
|
||||||
|
import com.njcn.system.pojo.dto.excel.UserLogExcel;
|
||||||
import com.njcn.system.pojo.param.AuditParam;
|
import com.njcn.system.pojo.param.AuditParam;
|
||||||
import com.njcn.system.pojo.po.UserLog;
|
import com.njcn.system.pojo.po.UserLog;
|
||||||
import com.njcn.system.pojo.vo.AuditLogCusVO;
|
import com.njcn.system.pojo.vo.AuditLogCusVO;
|
||||||
import com.njcn.system.pojo.vo.AuditLogVO;
|
import com.njcn.system.pojo.vo.AuditLogVO;
|
||||||
import com.njcn.system.service.AuditService;
|
import com.njcn.system.service.AuditService;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.compress.utils.IOUtils;
|
||||||
|
import org.apache.commons.fileupload.FileItem;
|
||||||
|
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.time.temporal.TemporalAdjusters;
|
import java.time.temporal.TemporalAdjusters;
|
||||||
@@ -36,6 +52,8 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
|||||||
|
|
||||||
private final UserLogMapper userLogMapper;
|
private final UserLogMapper userLogMapper;
|
||||||
|
|
||||||
|
private final SystemBaseConfig SystemBaseConfig;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Page<AuditLogVO> getAuditLog(AuditParam auditParam) {
|
public Page<AuditLogVO> getAuditLog(AuditParam auditParam) {
|
||||||
List<AuditLogVO> auditLogVOS = new ArrayList<>();
|
List<AuditLogVO> auditLogVOS = new ArrayList<>();
|
||||||
@@ -58,10 +76,14 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
|||||||
AuditLogVO auditLogVO = new AuditLogVO();
|
AuditLogVO auditLogVO = new AuditLogVO();
|
||||||
String updateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(userLog.getUpdateTime());
|
String updateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(userLog.getUpdateTime());
|
||||||
auditLogVO.setTime(updateTime);
|
auditLogVO.setTime(updateTime);
|
||||||
auditLogVO.setUserName(userLog.getUserName());
|
if (userLog.getUserName().isEmpty()) {
|
||||||
|
auditLogVO.setUserName(userLog.getLoginName());
|
||||||
|
} else {
|
||||||
|
auditLogVO.setUserName(userLog.getUserName());
|
||||||
|
}
|
||||||
auditLogVO.setOperate(userLog.getOperate());
|
auditLogVO.setOperate(userLog.getOperate());
|
||||||
StringBuilder describe = new StringBuilder();
|
StringBuilder describe = new StringBuilder();
|
||||||
describe.append(userLog.getUserName()).append("在").append(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm").format(userLog.getUpdateTime())).append("在").append(userLog.getIp()).append("执行了").append(userLog.getOperate()).append(",结果为");
|
describe.append(auditLogVO.getUserName()).append("在").append(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm").format(userLog.getUpdateTime())).append("在").append(userLog.getIp()).append("执行了").append(userLog.getOperate()).append(",结果为");
|
||||||
if (userLog.getResult()==1) {
|
if (userLog.getResult()==1) {
|
||||||
describe.append("成功");
|
describe.append("成功");
|
||||||
}
|
}
|
||||||
@@ -89,27 +111,101 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void logFileWriter() {
|
public void logFileWriter() {
|
||||||
SystemBaseConfig SystemBaseConfig = null;
|
|
||||||
LocalDate nowDate = LocalDate.now();
|
LocalDate nowDate = LocalDate.now();
|
||||||
LocalDate agoDate = nowDate.minusMonths(6).with(TemporalAdjusters.firstDayOfMonth());
|
LocalDate agoDate = nowDate.minusMonths(6).with(TemporalAdjusters.firstDayOfMonth());
|
||||||
String date = agoDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
|
String date = agoDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
|
||||||
|
|
||||||
// String time = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(agoDate);
|
// String time = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(agoDate);
|
||||||
// queryWrapper.between("sys_user_log.update_time","2022-02-01","2022-07-31");
|
// queryWrapper.between("sys_user_log.update_time","2022-02-01","2022-07-31");
|
||||||
|
|
||||||
QueryWrapper<UserLog> queryWrapper = new QueryWrapper<>();
|
QueryWrapper<UserLogExcel> queryWrapper = new QueryWrapper<>();
|
||||||
queryWrapper.ge("sys_user_log.update_time",date);
|
queryWrapper.ge("sys_user_log.update_time",date);
|
||||||
List<UserLog> userLogs = userLogMapper.selectList(queryWrapper);
|
List<UserLogExcel> excelList = auditMapper.queryExportUser(queryWrapper);
|
||||||
try {
|
|
||||||
File file = new File(SystemBaseConfig.getTempPath()+ File.separator + nowDate +" sys_user_log.txt");
|
File filePath = new File(SystemBaseConfig.getTempPath());
|
||||||
//将list写入文件并生成路径
|
filePath.mkdirs();
|
||||||
FileUtils.writeLines(file, userLogs, true);
|
File file = new File(filePath.getPath() + File.separator + nowDate +" sys_user_log.xlsx");
|
||||||
//读取文件大小
|
ExcelUtil.exportExcelWithTargetFile(file, UserLogExcel.class, excelList);
|
||||||
long sizeOf = FileUtils.sizeOf(new File(file.getPath()));
|
|
||||||
|
// QueryWrapper<UserLog> queryWrapper = new QueryWrapper<>();
|
||||||
|
// queryWrapper.ge("sys_user_log.update_time",date);
|
||||||
|
// List<UserLog> userLogs = userLogMapper.selectList(queryWrapper);
|
||||||
|
// try {
|
||||||
|
// File file = new File(SystemBaseConfig.getTempPath()+ File.separator + nowDate +" sys_user_log.txt");
|
||||||
|
// //将list写入文件并生成路径
|
||||||
|
// FileUtils.writeLines(file, userLogs, true);
|
||||||
|
// //读取文件大小
|
||||||
|
// long sizeOf = FileUtils.sizeOf(new File(file.getPath()));
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// e.printStackTrace();
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static MultipartFile getMultipartFile(File file) {
|
||||||
|
FileItem item = new DiskFileItemFactory().createItem("file"
|
||||||
|
, MediaType.MULTIPART_FORM_DATA_VALUE
|
||||||
|
, true
|
||||||
|
, file.getName());
|
||||||
|
try (InputStream input = new FileInputStream(file);
|
||||||
|
OutputStream os = item.getOutputStream()) {
|
||||||
|
// 流转移
|
||||||
|
IOUtils.copy(input, os);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
throw new IllegalArgumentException("Invalid file: " + e, e);
|
||||||
|
}
|
||||||
|
return new CommonsMultipartFile(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean recoverLogFile() {
|
||||||
|
LocalDate nowDate = LocalDate.now();
|
||||||
|
File file = new File(SystemBaseConfig.getTempPath()+ File.separator + nowDate +" sys_user_log.xlsx");
|
||||||
|
if (!file.exists()) {
|
||||||
|
throw new BusinessException(AuditLogEnum.NOT_FIND_FILE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ImportParams params = new ImportParams();
|
||||||
|
params.setHeadRows(1);//表头
|
||||||
|
params.setTitleRows(0);//标题行数
|
||||||
|
params.setNeedVerify(false);
|
||||||
|
//第一个sheet为台账信息
|
||||||
|
params.setStartSheetIndex(0);
|
||||||
|
params.setSheetNum(1);
|
||||||
|
|
||||||
|
FileInputStream in = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
in = new FileInputStream(file);
|
||||||
|
List<UserLogExcel> excelList = new ExcelImportService().importExcelByIs(in, UserLogExcel.class, params, false).getList();
|
||||||
|
if (excelList.get(0).getId().isEmpty()) {
|
||||||
|
throw new BusinessException(AuditLogEnum.LOG_EXCEPTION);
|
||||||
|
}
|
||||||
|
for (UserLogExcel excel: excelList) {
|
||||||
|
UserLog userLog = new UserLog();
|
||||||
|
BeanUtil.copyProperties(excel,userLog);
|
||||||
|
this.saveOrUpdate(userLog);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
throw new ExcelImportException(e.getMessage(), e);
|
||||||
|
} finally {
|
||||||
|
IOUtils.closeQuietly(in);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// try {
|
||||||
|
// // FileInputStream in = new FileInputStream(file);
|
||||||
|
// MultipartFile multipartFile = getMultipartFile(file);
|
||||||
|
//
|
||||||
|
// List<Object> list = ExcelImportUtil.importExcelMore(multipartFile.getInputStream(), UserLogExcel.class, params).getList();
|
||||||
|
//
|
||||||
|
// System.out.println(list);
|
||||||
|
//
|
||||||
|
// // this.saveOrUpdate(excelList.get(0));
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// throw new BusinessException(AuditLogEnum.LOG_EXCEPTION);
|
||||||
|
// }
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user