|
@@ -0,0 +1,285 @@
|
|
|
|
+package com.jiayue.ssi.job;
|
|
|
|
+
|
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
|
+import cn.hutool.core.text.csv.CsvUtil;
|
|
|
|
+import cn.hutool.core.text.csv.CsvWriter;
|
|
|
|
+import cn.hutool.core.util.CharsetUtil;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
+import com.jiayue.ssi.constant.SecretKeyConstants;
|
|
|
|
+import com.jiayue.ssi.entity.*;
|
|
|
|
+import com.jiayue.ssi.entity.server.Jvm;
|
|
|
|
+import com.jiayue.ssi.service.SysAlarmService;
|
|
|
|
+import com.jiayue.ssi.service.SysLogininforService;
|
|
|
|
+import com.jiayue.ssi.service.SysOperLogService;
|
|
|
|
+import com.jiayue.ssi.service.SysPolicyService;
|
|
|
|
+import com.jiayue.ssi.util.DateUtils;
|
|
|
|
+import com.jiayue.ssi.util.FileUtil;
|
|
|
|
+import com.jiayue.ssi.util.ResponseVO;
|
|
|
|
+import com.jiayue.ssi.util.SM2CryptUtils;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Calendar;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+* 自动备份审计日志
|
|
|
|
+*
|
|
|
|
+* @author xsl
|
|
|
|
+* @since 2023/04/06
|
|
|
|
+*/
|
|
|
|
+@Service
|
|
|
|
+@EnableScheduling
|
|
|
|
+@Slf4j
|
|
|
|
+public class AutoAuditBak {
|
|
|
|
+ @Autowired
|
|
|
|
+ SysLogininforService sysLogininforService;
|
|
|
|
+ @Autowired
|
|
|
|
+ SysPolicyService sysPolicyService;
|
|
|
|
+ @Autowired
|
|
|
|
+ SysOperLogService sysOperLogService;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 每30分钟执行一次扫描
|
|
|
|
+ */
|
|
|
|
+ @Scheduled(cron = "0 0/1 * * * ?")
|
|
|
|
+ public void auditBak() throws Exception{
|
|
|
|
+ SysPolicy sysPolicy = sysPolicyService.getOne(new QueryWrapper<>());
|
|
|
|
+ // 保留月份数
|
|
|
|
+ int auditLog = sysPolicy.getAuditLog();
|
|
|
|
+ // 先获取要备份截止时间点
|
|
|
|
+ Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
|
|
|
|
+ calendar.add(Calendar.MONTH, -(auditLog+1));//当前时间减去一个月,即一个月前的时间
|
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));//获取月份最后一天
|
|
|
|
+ try {
|
|
|
|
+ // 登录日志备份
|
|
|
|
+ createSysLogininforCsvFile(calendar);
|
|
|
|
+ }
|
|
|
|
+ catch (Exception e){
|
|
|
|
+ log.error("登录日志备份出错",e);
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ // 操作日志备份
|
|
|
|
+ createSysOperCsvFile(calendar);
|
|
|
|
+ }
|
|
|
|
+ catch (Exception e){
|
|
|
|
+ log.error("操作日志备份出错",e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 生成登录日志csv文件
|
|
|
|
+ *
|
|
|
|
+ * @param calendar 备份截止时间
|
|
|
|
+ */
|
|
|
|
+ public void createSysLogininforCsvFile(Calendar calendar) throws Exception{
|
|
|
|
+ log.info("执行登录日志备份");
|
|
|
|
+ QueryWrapper<SysLogininfor> sysLogininforQueryWrapper = new QueryWrapper<>();
|
|
|
|
+ sysLogininforQueryWrapper.le("create_time", DateUtils.getDayLastTime(calendar.getTime()));
|
|
|
|
+ List<SysLogininfor> sysLogininforList = sysLogininforService.list(sysLogininforQueryWrapper);
|
|
|
|
+ // 生成csv备份
|
|
|
|
+ if (sysLogininforList.size()>0){
|
|
|
|
+ // 备份文件名
|
|
|
|
+ String fileName = DateUtil.format(DateUtils.getDayLastTime(calendar.getTime()),"yyyy-MM-dd") + "-" + "SysLogininfor" + ".csv";
|
|
|
|
+ //第一行
|
|
|
|
+ String[] listName = new String[15];
|
|
|
|
+ listName[0] = "infoId";
|
|
|
|
+ listName[1] = "userName";
|
|
|
|
+ listName[2] = "status";
|
|
|
|
+ listName[3] = "ipaddr";
|
|
|
|
+ listName[4] = "loginLocation";
|
|
|
|
+ listName[5] = "browser";
|
|
|
|
+ listName[6] = "os";
|
|
|
|
+ listName[7] = "msg";
|
|
|
|
+ listName[8] = "loginTime";
|
|
|
|
+ listName[9] = "delFlag";
|
|
|
|
+ listName[10] = "createBy";
|
|
|
|
+ listName[11] = "createTime";
|
|
|
|
+ listName[12] = "updateBy";
|
|
|
|
+ listName[13] = "updateTime";
|
|
|
|
+ listName[14] = "remark";
|
|
|
|
+ //获取String[]类型的数据至result中
|
|
|
|
+ List<String> result = new ArrayList<>();
|
|
|
|
+ String listNameStr = "";
|
|
|
|
+ for (int i=0;i<listName.length;i++){
|
|
|
|
+ listNameStr = listNameStr + listName[i]+"|";
|
|
|
|
+ }
|
|
|
|
+ String headEncrypt = SM2CryptUtils.encrypt(listNameStr.substring(0,listNameStr.length()-1), SecretKeyConstants.CLIENT_PUBLIC_KEY);
|
|
|
|
+// System.out.println("加密:"+headEncrypt);
|
|
|
|
+// String headText = SM2CryptUtils.decrypt(headEncrypt,SecretKeyConstants.CLIENT_PRIVATE_KEY);
|
|
|
|
+// System.out.println("解密:"+headText);
|
|
|
|
+ //将listName添加到result中
|
|
|
|
+ result.add(headEncrypt);
|
|
|
|
+ Long delIds[] = new Long[sysLogininforList.size()];
|
|
|
|
+ for (int j=0;j<sysLogininforList.size();j++) {
|
|
|
|
+ SysLogininfor sysLogininfor = sysLogininforList.get(j);
|
|
|
|
+ String[] listValue = new String[15];
|
|
|
|
+ listValue[0] = String.valueOf(sysLogininfor.getInfoId());
|
|
|
|
+ delIds[j] = sysLogininfor.getInfoId();
|
|
|
|
+ listValue[1] = String.valueOf(sysLogininfor.getUserName());
|
|
|
|
+ listValue[2] = String.valueOf(sysLogininfor.getStatus());
|
|
|
|
+ listValue[3] = String.valueOf(sysLogininfor.getIpaddr());
|
|
|
|
+ listValue[4] = String.valueOf(sysLogininfor.getLoginLocation());
|
|
|
|
+ listValue[5] = String.valueOf(sysLogininfor.getBrowser());
|
|
|
|
+ listValue[6] = String.valueOf(sysLogininfor.getOs());
|
|
|
|
+ listValue[7] = String.valueOf(sysLogininfor.getMsg());
|
|
|
|
+ listValue[8] = String.valueOf(DateUtil.format(sysLogininfor.getLoginTime(),"yyyy-MM-dd HH:mm:ss"));
|
|
|
|
+ listValue[9] = String.valueOf(sysLogininfor.getDelFlag());
|
|
|
|
+ listValue[10] = String.valueOf(sysLogininfor.getCreateBy());
|
|
|
|
+ listValue[11] = String.valueOf(DateUtil.format(sysLogininfor.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
|
|
|
|
+ listValue[12] = String.valueOf(sysLogininfor.getUpdateBy());
|
|
|
|
+ listValue[13] = String.valueOf(DateUtil.format(sysLogininfor.getUpdateTime(),"yyyy-MM-dd HH:mm:ss"));
|
|
|
|
+ listValue[14] = String.valueOf(sysLogininfor.getRemark());
|
|
|
|
+
|
|
|
|
+ String listValueStr = "";
|
|
|
|
+ for (int k=0;k<listValue.length;k++){
|
|
|
|
+ listValueStr = listValueStr + listValue[k]+"|";
|
|
|
|
+ }
|
|
|
|
+ String encrypt = SM2CryptUtils.encrypt(listValueStr.substring(0,listValueStr.length()-1), SecretKeyConstants.CLIENT_PUBLIC_KEY);
|
|
|
|
+// System.out.println("加密:"+encrypt);
|
|
|
|
+// String text = SM2CryptUtils.decrypt(encrypt,SecretKeyConstants.CLIENT_PRIVATE_KEY);
|
|
|
|
+// System.out.println("解密:"+text);
|
|
|
|
+ result.add(encrypt);
|
|
|
|
+ }
|
|
|
|
+ //导入HuTool中CSV工具包的CsvWriter类
|
|
|
|
+ //设置导出字符类型, CHARSET_UTF_8
|
|
|
|
+ File csvFile = new File(fileName);
|
|
|
|
+ File destDir = new File(FileUtil.getAuditBackUpPath()+ File.separator +DateUtils.dateTime());
|
|
|
|
+ if (!destDir.exists()) {// 如果目录不存在则创建目录
|
|
|
|
+ boolean b = destDir.mkdirs();
|
|
|
|
+ if (!b) {
|
|
|
|
+ throw new RuntimeException(destDir.getPath() + " 目录创建失败");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ CsvWriter writer = CsvUtil.getWriter(destDir + File.separator + csvFile.getName(), CharsetUtil.CHARSET_UTF_8);
|
|
|
|
+ writer.write(result); //通过CsvWriter中的write方法写入数据
|
|
|
|
+ writer.close(); //关闭CsvWriter
|
|
|
|
+ // 删除表中数据
|
|
|
|
+ sysLogininforService.deleteLogininforByIds(delIds);
|
|
|
|
+ }
|
|
|
|
+ log.info("结束登录日志备份");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 生成操作日志csv文件
|
|
|
|
+ *
|
|
|
|
+ * @param calendar 备份截止时间
|
|
|
|
+ */
|
|
|
|
+ public void createSysOperCsvFile(Calendar calendar) throws Exception{
|
|
|
|
+ log.info("执行登录日志备份");
|
|
|
|
+ QueryWrapper<SysOperLog> sysOperLogQueryWrapper = new QueryWrapper<>();
|
|
|
|
+ sysOperLogQueryWrapper.le("create_time", DateUtils.getDayLastTime(calendar.getTime()));
|
|
|
|
+ List<SysOperLog> sysOperLogList = sysOperLogService.list(sysOperLogQueryWrapper);
|
|
|
|
+ // 生成csv备份
|
|
|
|
+ if (sysOperLogList.size()>0){
|
|
|
|
+ // 备份文件名
|
|
|
|
+ String fileName = DateUtil.format(DateUtils.getDayLastTime(calendar.getTime()),"yyyy-MM-dd") + "-" + "SysOperLog" + ".csv";
|
|
|
|
+ //第一行
|
|
|
|
+ String[] listName = new String[25];
|
|
|
|
+ listName[0] = "operId";
|
|
|
|
+ listName[1] = "title";
|
|
|
|
+ listName[2] = "businessType";
|
|
|
|
+ listName[3] = "auditType";
|
|
|
|
+ listName[4] = "businessTypes";
|
|
|
|
+ listName[5] = "method";
|
|
|
|
+ listName[6] = "requestMethod";
|
|
|
|
+ listName[7] = "operatorType";
|
|
|
|
+ listName[8] = "operName";
|
|
|
|
+ listName[9] = "deptName";
|
|
|
|
+ listName[10] = "operUrl";
|
|
|
|
+ listName[11] = "operIp";
|
|
|
|
+ listName[12] = "operLocation";
|
|
|
|
+ listName[13] = "operParam";
|
|
|
|
+ listName[14] = "jsonResult";
|
|
|
|
+ listName[15] = "status";
|
|
|
|
+ listName[16] = "errorMsg";
|
|
|
|
+ listName[17] = "operTime";
|
|
|
|
+ listName[18] = "costTime";
|
|
|
|
+ listName[19] = "delFlag";
|
|
|
|
+ listName[20] = "createBy";
|
|
|
|
+ listName[21] = "createTime";
|
|
|
|
+ listName[22] = "updateBy";
|
|
|
|
+ listName[23] = "updateTime";
|
|
|
|
+ listName[24] = "remark";
|
|
|
|
+ //获取String[]类型的数据至result中
|
|
|
|
+ List<String> result = new ArrayList<>();
|
|
|
|
+ String listNameStr = "";
|
|
|
|
+ for (int i=0;i<listName.length;i++){
|
|
|
|
+ listNameStr = listNameStr + listName[i]+"|";
|
|
|
|
+ }
|
|
|
|
+ String headEncrypt = SM2CryptUtils.encrypt(listNameStr.substring(0,listNameStr.length()-1), SecretKeyConstants.CLIENT_PUBLIC_KEY);
|
|
|
|
+// System.out.println("加密:"+headEncrypt);
|
|
|
|
+// String headText = SM2CryptUtils.decrypt(headEncrypt,SecretKeyConstants.CLIENT_PRIVATE_KEY);
|
|
|
|
+// System.out.println("解密:"+headText);
|
|
|
|
+ //将listName添加到result中
|
|
|
|
+ result.add(headEncrypt);
|
|
|
|
+ Long delIds[] = new Long[sysOperLogList.size()];
|
|
|
|
+ for (int j=0;j<sysOperLogList.size();j++) {
|
|
|
|
+ SysOperLog sysOperLog = sysOperLogList.get(j);
|
|
|
|
+ String[] listValue = new String[25];
|
|
|
|
+ listValue[0] = String.valueOf(sysOperLog.getOperId());
|
|
|
|
+ delIds[j] = sysOperLog.getOperId();
|
|
|
|
+ listValue[1] = String.valueOf(sysOperLog.getTitle());
|
|
|
|
+ listValue[2] = String.valueOf(sysOperLog.getBusinessType());
|
|
|
|
+ listValue[3] = String.valueOf(sysOperLog.getAuditType());
|
|
|
|
+ listValue[4] = String.valueOf(sysOperLog.getBusinessTypes());
|
|
|
|
+ listValue[5] = String.valueOf(sysOperLog.getMethod());
|
|
|
|
+ listValue[6] = String.valueOf(sysOperLog.getRequestMethod());
|
|
|
|
+ listValue[7] = String.valueOf(sysOperLog.getOperatorType());
|
|
|
|
+ listValue[8] = String.valueOf(sysOperLog.getOperName());
|
|
|
|
+ listValue[9] = String.valueOf(sysOperLog.getDeptName());
|
|
|
|
+ listValue[10] = String.valueOf(sysOperLog.getOperUrl());
|
|
|
|
+ listValue[11] = String.valueOf(sysOperLog.getOperIp());
|
|
|
|
+ listValue[12] = String.valueOf(sysOperLog.getOperLocation());
|
|
|
|
+ listValue[13] = String.valueOf(sysOperLog.getOperParam());
|
|
|
|
+ listValue[14] = String.valueOf(sysOperLog.getJsonResult());
|
|
|
|
+ listValue[15] = String.valueOf(sysOperLog.getStatus());
|
|
|
|
+ listValue[16] = String.valueOf(sysOperLog.getErrorMsg());
|
|
|
|
+ listValue[17] = String.valueOf(DateUtil.format(sysOperLog.getOperTime(),"yyyy-MM-dd HH:mm:ss"));
|
|
|
|
+ listValue[18] = String.valueOf(sysOperLog.getCostTime());
|
|
|
|
+ listValue[19] = String.valueOf(sysOperLog.getDelFlag());
|
|
|
|
+ listValue[20] = String.valueOf(sysOperLog.getCreateBy());
|
|
|
|
+ listValue[21] = String.valueOf(DateUtil.format(sysOperLog.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
|
|
|
|
+ listValue[22] = String.valueOf(sysOperLog.getUpdateBy());
|
|
|
|
+ listValue[23] = String.valueOf(DateUtil.format(sysOperLog.getUpdateTime(),"yyyy-MM-dd HH:mm:ss"));
|
|
|
|
+ listValue[24] = String.valueOf(sysOperLog.getRemark());
|
|
|
|
+
|
|
|
|
+ String listValueStr = "";
|
|
|
|
+ for (int k=0;k<listValue.length;k++){
|
|
|
|
+ listValueStr = listValueStr + listValue[k]+"|";
|
|
|
|
+ }
|
|
|
|
+ String encrypt = SM2CryptUtils.encrypt(listValueStr.substring(0,listValueStr.length()-1), SecretKeyConstants.CLIENT_PUBLIC_KEY);
|
|
|
|
+// System.out.println("加密:"+encrypt);
|
|
|
|
+// String text = SM2CryptUtils.decrypt(encrypt,SecretKeyConstants.CLIENT_PRIVATE_KEY);
|
|
|
|
+// System.out.println("解密:"+text);
|
|
|
|
+ result.add(encrypt);
|
|
|
|
+ }
|
|
|
|
+ //导入HuTool中CSV工具包的CsvWriter类
|
|
|
|
+ //设置导出字符类型, CHARSET_UTF_8
|
|
|
|
+ File csvFile = new File(fileName);
|
|
|
|
+ File destDir = new File(FileUtil.getAuditBackUpPath()+ File.separator +DateUtils.dateTime());
|
|
|
|
+ if (!destDir.exists()) {// 如果目录不存在则创建目录
|
|
|
|
+ boolean b = destDir.mkdirs();
|
|
|
|
+ if (!b) {
|
|
|
|
+ throw new RuntimeException(destDir.getPath() + " 目录创建失败");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ CsvWriter writer = CsvUtil.getWriter(destDir + File.separator + csvFile.getName(), CharsetUtil.CHARSET_UTF_8);
|
|
|
|
+ writer.write(result); //通过CsvWriter中的write方法写入数据
|
|
|
|
+ writer.close(); //关闭CsvWriter
|
|
|
|
+ // 删除表中数据
|
|
|
|
+ sysOperLogService.deleteOperLogByIds(delIds);
|
|
|
|
+ }
|
|
|
|
+ log.info("结束操作日志备份");
|
|
|
|
+ }
|
|
|
|
+}
|