审计日志相关代码提交

This commit is contained in:
2022-07-13 15:44:05 +08:00
parent 1129172b3d
commit 783e75e341
21 changed files with 195 additions and 54 deletions

View File

@@ -2,11 +2,15 @@ package com.njcn.web.service.impl;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.tocrhz.mqtt.publisher.MqttPublisher;
import com.njcn.common.config.GeneralInfo;
import com.njcn.common.pojo.constant.LogInfo;
import com.njcn.common.pojo.dto.LogInfoDTO;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.PubUtils;
import com.njcn.common.utils.ReflectCommonUtil;
import com.njcn.web.service.ILogService;
import com.njcn.web.utils.RequestUtil;
@@ -46,6 +50,8 @@ public class LogServiceImpl implements ILogService {
private final GeneralInfo generalInfo;
private final MqttPublisher publisher;
/**
* 异步记录controller中返回的信息内容
*
@@ -59,19 +65,21 @@ public class LogServiceImpl implements ILogService {
public void recodeAdviceLog(ServerHttpRequest request, MethodParameter returnType, HttpResult httpResult, String methodDescribe) {
//处理审计日志
String userName;
int isLogin = 0;
HttpServletRequest httpServletRequest = RequestUtil.getRequest(request);
if (UN_LOGIN_METHOD.contains(methodDescribe)) {
userName = RequestUtil.getLoginName(httpServletRequest);
} else {
userName = RequestUtil.getUserNickname(request);
isLogin = 1;
}
String result = httpResult.getCode().equalsIgnoreCase(CommonResponseEnum.FAIL.getCode()) ? CommonResponseEnum.FAIL.getMessage() : CommonResponseEnum.SUCCESS.getMessage();
String ip = RequestUtil.getRealIp(request);
String type = ReflectCommonUtil.getOperateInfoByMethod(returnType.getMethod()).getOperateType();
String level = ReflectCommonUtil.getOperateInfoByMethod(returnType.getMethod()).getOperateLevel();
String operateType = ReflectCommonUtil.getOperateTypeByMethod(returnType.getMethod());
LogInfoDTO logInfoDTO = new LogInfoDTO(userName, methodDescribe, result, ip, type, level, operateType, generalInfo.getMicroServiceName());
System.out.println(logInfoDTO);
LogInfoDTO logInfoDTO = new LogInfoDTO(userName, ip, methodDescribe, operateType, result.equalsIgnoreCase("失败") ? 0 : 1, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), isLogin);
publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 2, false);
}
@@ -87,8 +95,10 @@ public class LogServiceImpl implements ILogService {
public void recodeBusinessExceptionLog(Exception exception, HttpServletRequest request, String message) {
LogInfoDTO tempLogInfo = RequestUtil.initLogInfo(request);
//认证前,获取用户信息
int isLogin = 1;
if (Objects.equals(tempLogInfo.getUserName(), LogInfo.UNKNOWN_USER)) {
tempLogInfo.setUserName(RequestUtil.getLoginName(request));
isLogin = 0;
}
//根据异常获取method方法
Method method = ReflectCommonUtil.getMethod(exception);
@@ -101,9 +111,8 @@ public class LogServiceImpl implements ILogService {
String type = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateType();
String level = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateLevel();
String operateType = ReflectCommonUtil.getOperateTypeByMethod(method);
LogInfoDTO logInfoDTO = new LogInfoDTO(tempLogInfo.getUserName(), methodDescribe, result, tempLogInfo.getIp(), type, level, operateType, generalInfo.getMicroServiceName());
System.out.println(logInfoDTO);
LogInfoDTO logInfoDTO = new LogInfoDTO(tempLogInfo.getUserName(), tempLogInfo.getIp(), methodDescribe, operateType, result.equalsIgnoreCase("失败") ? 0 : 1, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), isLogin);
publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 1, false);
}
/**
@@ -129,10 +138,37 @@ public class LogServiceImpl implements ILogService {
String type = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateType();
String level = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateLevel();
String operateType = ReflectCommonUtil.getOperateTypeByMethod(method);
LogInfoDTO logInfoDTO = new LogInfoDTO(userName, methodDescribe, result, ip, type, level, operateType, generalInfo.getMicroServiceName());
System.out.println(logInfoDTO);
LogInfoDTO logInfoDTO = new LogInfoDTO(userName, ip, methodDescribe, operateType, result.equalsIgnoreCase("失败") ? 0 : 1, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), 0);
publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 1, false);
}
/**
* 严重度 文字 转 数字
*/
private Integer levelStringToNumber(String level) {
switch (level) {
case "中等":
return 1;
case "严重":
return 2;
default:
return 0;
}
}
/**
* 严重度 数字 转 文字
*/
private String levelNumberToString(Integer level) {
switch (level) {
case 1:
return "中等";
case 2:
return "严重";
default:
return "普通";
}
}
}