审计日志相关代码提交
This commit is contained in:
@@ -64,10 +64,10 @@
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
</dependency>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.alibaba</groupId>-->
|
||||
<!-- <artifactId>easyexcel</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.alibaba</groupId>-->
|
||||
<!-- <artifactId>easyexcel</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
<!--API接口文档-->
|
||||
<dependency>
|
||||
<groupId>com.github.xiaoymin</groupId>
|
||||
@@ -91,6 +91,11 @@
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-annotations</artifactId>
|
||||
</dependency>
|
||||
<!--mqtt相关依赖-->
|
||||
<dependency>
|
||||
<groupId>com.github.tocrhz</groupId>
|
||||
<artifactId>mqtt-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -29,12 +29,26 @@ public class LogInfoDTO implements Serializable {
|
||||
|
||||
private String operateType;
|
||||
|
||||
private String result;
|
||||
/**
|
||||
* 操作结果 0.失败 1.成功
|
||||
*/
|
||||
private Integer result;
|
||||
|
||||
private String level;
|
||||
/**
|
||||
* 严重度 0.普通 1.中等 2.严重
|
||||
*/
|
||||
private Integer level;
|
||||
|
||||
private String type;
|
||||
/**
|
||||
* 事件类型 0.业务事件 1.系统事件
|
||||
*/
|
||||
private Integer type;
|
||||
|
||||
private String serviceName;
|
||||
|
||||
/**
|
||||
* 0 未登录; 1 已登录
|
||||
*/
|
||||
private Integer isLogin;
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.njcn.common.utils;
|
||||
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
@@ -120,6 +121,21 @@ public class PubUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将实体对象转为JSON
|
||||
*
|
||||
* @param object 实体对象
|
||||
*/
|
||||
public static String obj2json(Object object) {
|
||||
try {
|
||||
MAPPER.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
|
||||
return MAPPER.writeValueAsString(object);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("将将实体对象转为JSON时发生错误:" + object, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 判断一个数字是否在区间内
|
||||
*
|
||||
|
||||
@@ -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 "普通";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
<module>common-autocode</module>
|
||||
<module>common-influxdb</module>
|
||||
<module>common-poi</module>
|
||||
<!-- <module>common-echarts</module>-->
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
|
||||
Reference in New Issue
Block a user