1.审计日志问题排查
This commit is contained in:
@@ -6,6 +6,7 @@ import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.github.tocrhz.mqtt.annotation.MqttSubscribe;
|
||||
import com.github.tocrhz.mqtt.annotation.Payload;
|
||||
import com.njcn.common.pojo.constant.LogInfo;
|
||||
import com.njcn.common.pojo.constant.PatternRegex;
|
||||
import com.njcn.common.pojo.dto.LogInfoDTO;
|
||||
import com.njcn.common.utils.PubUtils;
|
||||
@@ -63,22 +64,23 @@ public class MqttMessageHandler {
|
||||
}
|
||||
|
||||
LogInfoDTO logInfoDTO = PubUtils.json2obj(new String(message.getPayload(), StandardCharsets.UTF_8),LogInfoDTO.class);
|
||||
|
||||
if("注销".equals(logInfoDTO.getOperateType()) || "认证".equals(logInfoDTO.getOperateType())){
|
||||
logInfoDTO.setType(1);
|
||||
}else {
|
||||
String loginName = logInfoDTO.getLoginName();
|
||||
if(StrUtil.isNotBlank(loginName) && jsonObject.containsKey(loginName)){
|
||||
if((Integer) jsonObject.get(loginName)==2){
|
||||
logInfoDTO.setType(0);
|
||||
if(!logInfoDTO.getLoginName().equals(LogInfo.UNKNOWN_USER) && !logInfoDTO.getOperate().equals(LogInfo.UNKNOWN_OPERATE) && !logInfoDTO.getIp().equals(LogInfo.UNKNOWN_IP)){
|
||||
if("注销".equals(logInfoDTO.getOperateType()) || "认证".equals(logInfoDTO.getOperateType())){
|
||||
logInfoDTO.setType(1);
|
||||
}else {
|
||||
String loginName = logInfoDTO.getLoginName();
|
||||
if(StrUtil.isNotBlank(loginName) && jsonObject.containsKey(loginName)){
|
||||
if((Integer) jsonObject.get(loginName)==2){
|
||||
logInfoDTO.setType(0);
|
||||
}else {
|
||||
logInfoDTO.setType(1);
|
||||
};
|
||||
}else {
|
||||
logInfoDTO.setType(1);
|
||||
};
|
||||
}else {
|
||||
logInfoDTO.setType(1);
|
||||
}
|
||||
}
|
||||
userLogService.addUserLog(logInfoDTO);
|
||||
}
|
||||
userLogService.addUserLog(logInfoDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -34,7 +34,9 @@ import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||
import com.njcn.common.pojo.exception.BusinessException;
|
||||
import com.njcn.common.utils.PubUtils;
|
||||
import com.njcn.db.constant.DbConstant;
|
||||
import com.njcn.oss.constant.GeneralConstant;
|
||||
import com.njcn.oss.constant.OssPath;
|
||||
import com.njcn.oss.utils.FileStorageUtil;
|
||||
import com.njcn.redis.utils.RedisUtil;
|
||||
import com.njcn.system.enums.SystemResponseEnum;
|
||||
import com.njcn.system.excel.UserLogExcel;
|
||||
@@ -57,8 +59,7 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.*;
|
||||
import java.text.ParseException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
@@ -94,6 +95,8 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
||||
|
||||
private final MqttPublisher publisher;
|
||||
|
||||
private final FileStorageUtil fileStorageUtil;
|
||||
|
||||
@Override
|
||||
public Page<AuditLogVO> getAuditLog(AuditParam auditParam) {
|
||||
List<AuditLogVO> auditLogVOS = new ArrayList<>();
|
||||
@@ -206,11 +209,14 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
||||
public void logFileWriter() {
|
||||
TimeInterval timeInterval = new TimeInterval();
|
||||
//限制多次点击
|
||||
redisUtil.delete("logFileWriter");
|
||||
String logFileWriter = redisUtil.getStringByKey("logFileWriter");
|
||||
if (StrUtil.isNotBlank(logFileWriter) || ObjectUtil.equals(logFileWriter, "1")) {
|
||||
throw new BusinessException(SystemResponseEnum.MULTIPLE_CLICKS_LOG_FILE_WRITER);
|
||||
}
|
||||
redisUtil.saveByKeyWithExpire("logFileWriter", "1", 600L);
|
||||
|
||||
|
||||
Config config = iConfigService.getOne(new LambdaQueryWrapper<Config>().eq(Config::getState, DataStateEnum.ENABLE.getCode()));
|
||||
LocalDateTime nowDate = LocalDateTime.now();
|
||||
LocalDateTime agoDate = nowDate.minusMonths(config.getLogTime()).with(TemporalAdjusters.firstDayOfMonth());
|
||||
@@ -223,12 +229,29 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
||||
aa.ge("sys_user_log.update_time", date);
|
||||
aa.le("sys_user_log.update_time", endTime);
|
||||
String nowTime = date + "至" + endTime;
|
||||
File file = new File(generalInfo.getBusinessTempPath() + File.separator + OssPath.LOGBAK);
|
||||
if (!file.exists() && !file.isDirectory()) {
|
||||
file.mkdir();
|
||||
}
|
||||
|
||||
//必须放到循环外,否则会刷新流
|
||||
ExcelWriter excelWriter = EasyExcel.write(generalInfo.getBusinessTempPath() + File.separator + OssPath.LOGBAK + nowTime + ExcelTypeEnum.XLSX.getValue(), UserLogExcel.class).build();
|
||||
ExcelWriter excelWriter =null;
|
||||
FileInputStream fileInputStream;
|
||||
File file = null;
|
||||
try {
|
||||
if (generalInfo.getBusinessFileStorage() == GeneralConstant.LOCAL_DISK) {
|
||||
file = new File(generalInfo.getBusinessTempPath() + File.separator + OssPath.LOGBAK);
|
||||
if (!file.exists() && !file.isDirectory()) {
|
||||
file.mkdir();
|
||||
}
|
||||
excelWriter = EasyExcel.write(generalInfo.getBusinessTempPath() + File.separator + OssPath.LOGBAK + nowTime + ExcelTypeEnum.XLSX.getValue(), UserLogExcel.class).build();
|
||||
} else if (generalInfo.getBusinessFileStorage() == GeneralConstant.HUAWEI_OBS || generalInfo.getBusinessFileStorage() == GeneralConstant.MINIO_OSS) {
|
||||
file = File.createTempFile(date+"_"+endTime,".xlsx");
|
||||
excelWriter = EasyExcel.write(file, UserLogExcel.class).build();
|
||||
}
|
||||
}catch (Exception e){
|
||||
log.error("获取文件服务器异常");
|
||||
redisUtil.delete("logFileWriter");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
//记录总数:实际中需要根据查询条件进行统计即可:一共多少条
|
||||
int totalCount = this.count(aa);
|
||||
@@ -257,13 +280,24 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
||||
excelWriter.write(list, writeSheet);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
redisUtil.delete("logFileWriter");
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
excelWriter.finish();
|
||||
}
|
||||
|
||||
if (generalInfo.getBusinessFileStorage() == GeneralConstant.HUAWEI_OBS || generalInfo.getBusinessFileStorage() == GeneralConstant.MINIO_OSS) {
|
||||
try {
|
||||
fileInputStream = new FileInputStream(file);
|
||||
fileStorageUtil.uploadStream(fileInputStream,OssPath.LOGBAK,file.getName());
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}finally {
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
|
||||
redisUtil.delete("logFileWriter");
|
||||
System.out.println("日志备份结束,共花费时间" + timeInterval.intervalSecond() + "S");
|
||||
}
|
||||
@@ -276,12 +310,13 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
||||
throw new BusinessException(SystemResponseEnum.MULTIPLE_CLICKS_LOG_FILE_WRITER);
|
||||
}
|
||||
redisUtil.saveByKeyWithExpire("recoverLogFile", "1", 600L);
|
||||
File newFile = getLastFile();
|
||||
if (FileUtil.isEmpty(newFile)) {
|
||||
throw new BusinessException(SystemResponseEnum.NOT_FIND_FILE);
|
||||
}
|
||||
FileInputStream in = null;
|
||||
File newFile = null;
|
||||
try {
|
||||
newFile = getLastFile();
|
||||
if (FileUtil.isEmpty(newFile)) {
|
||||
throw new BusinessException(SystemResponseEnum.NOT_FIND_FILE);
|
||||
}
|
||||
in = new FileInputStream(newFile);
|
||||
Set<String> logIndex = new HashSet<>(this.baseMapper.getAllIndex());
|
||||
List<UserLog> userLogs = new ArrayList<>();
|
||||
@@ -307,6 +342,10 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
||||
throw new BusinessException(SystemResponseEnum.LOG_EXCEPTION);
|
||||
} finally {
|
||||
IOUtils.closeQuietly(in);
|
||||
|
||||
if(generalInfo.getBusinessFileStorage() == GeneralConstant.HUAWEI_OBS || generalInfo.getBusinessFileStorage() == GeneralConstant.MINIO_OSS) {
|
||||
newFile.deleteOnExit();
|
||||
}
|
||||
}
|
||||
redisUtil.delete("recoverLogFile");
|
||||
}
|
||||
@@ -430,7 +469,16 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
||||
* 返回一个最新修改日期的文件
|
||||
*/
|
||||
public File getLastFile() {
|
||||
File parentFile = new File(generalInfo.getBusinessTempPath() + File.separator + OssPath.LOGBAK);
|
||||
File parentFile = null;
|
||||
if(generalInfo.getBusinessFileStorage() == GeneralConstant.LOCAL_DISK){
|
||||
parentFile = new File(generalInfo.getBusinessTempPath() + File.separator + OssPath.LOGBAK);
|
||||
}else if(generalInfo.getBusinessFileStorage() == GeneralConstant.HUAWEI_OBS || generalInfo.getBusinessFileStorage() == GeneralConstant.MINIO_OSS){
|
||||
try {
|
||||
parentFile = fileStorageUtil.getLastFile("excelreport",OssPath.LOGBAK);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
//文件夹下的所有子文件数组
|
||||
File[] files = parentFile.listFiles();
|
||||
List<Date> list;
|
||||
@@ -439,7 +487,7 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
||||
list.sort(Comparator.reverseOrder());
|
||||
return Arrays.stream(files).filter(tempFile -> FileUtil.lastModifiedTime(tempFile).equals(list.get(0))).collect(Collectors.toList()).get(0);
|
||||
}
|
||||
return null;
|
||||
return parentFile;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,6 +4,8 @@ import cn.hutool.core.date.DatePattern;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.njcn.common.pojo.exception.BusinessException;
|
||||
import com.njcn.harmonic.api.PqTypicalSourceFeignClient;
|
||||
import com.njcn.harmonic.api.UploadGwDataFeignClient;
|
||||
import com.njcn.harmonic.pojo.param.UploadDataParam;
|
||||
import com.njcn.harmonic.pojo.param.UploadParam;
|
||||
import com.njcn.system.timer.TimerTaskRunner;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -20,15 +22,19 @@ public class PqTypicalSourceTaskRunner implements TimerTaskRunner {
|
||||
|
||||
private final PqTypicalSourceFeignClient pqTypicalSourceFeignClient;
|
||||
|
||||
private final UploadGwDataFeignClient uploadGwDataFeignClient;
|
||||
|
||||
|
||||
@Override
|
||||
public void action(String date) {
|
||||
UploadParam param=new UploadParam();
|
||||
param.setTime(DateUtil.yesterday().toString(DatePattern.NORM_DATE_PATTERN));
|
||||
String code = pqTypicalSourceFeignClient.send(param).getCode();
|
||||
if(code.equals("A0002")){
|
||||
throw new BusinessException("失败");
|
||||
}
|
||||
String dateDeal =DateUtil.yesterday().toString(DatePattern.NORM_DATE_PATTERN);
|
||||
param.setTime(dateDeal);
|
||||
pqTypicalSourceFeignClient.send(param).getCode();
|
||||
|
||||
UploadDataParam uploadDataParam = new UploadDataParam();
|
||||
uploadDataParam.setSearchBeginTime(dateDeal);
|
||||
uploadGwDataFeignClient.upGwCommPoint(uploadDataParam);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user