审计日志相关代码提交

This commit is contained in:
2022-07-13 20:16:32 +08:00
parent 783e75e341
commit 128518989f
19 changed files with 100 additions and 46 deletions

View File

@@ -1,9 +1,5 @@
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;
@@ -19,14 +15,11 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.MethodArgumentNotValidException;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
@@ -65,20 +58,21 @@ public class LogServiceImpl implements ILogService {
public void recodeAdviceLog(ServerHttpRequest request, MethodParameter returnType, HttpResult httpResult, String methodDescribe) {
//处理审计日志
String userName;
int isLogin = 0;
String userIndex ;
HttpServletRequest httpServletRequest = RequestUtil.getRequest(request);
if (UN_LOGIN_METHOD.contains(methodDescribe)) {
userName = RequestUtil.getLoginName(httpServletRequest);
userIndex = userName;
} else {
userName = RequestUtil.getUserNickname(request);
isLogin = 1;
userIndex = RequestUtil.getUserIndex(request);
}
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, ip, methodDescribe, operateType, result.equalsIgnoreCase("失败") ? 0 : 1, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), isLogin);
LogInfoDTO logInfoDTO = new LogInfoDTO(userName, ip, methodDescribe, operateType, result.equalsIgnoreCase("失败") ? 0 : 1, "",levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), userIndex);
publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 2, false);
}
@@ -95,10 +89,12 @@ public class LogServiceImpl implements ILogService {
public void recodeBusinessExceptionLog(Exception exception, HttpServletRequest request, String message) {
LogInfoDTO tempLogInfo = RequestUtil.initLogInfo(request);
//认证前,获取用户信息
int isLogin = 1;
String userIndex;
if (Objects.equals(tempLogInfo.getUserName(), LogInfo.UNKNOWN_USER)) {
tempLogInfo.setUserName(RequestUtil.getLoginName(request));
isLogin = 0;
userIndex = tempLogInfo.getUserName();
}else{
userIndex = RequestUtil.getUserIndex(request);
}
//根据异常获取method方法
Method method = ReflectCommonUtil.getMethod(exception);
@@ -106,12 +102,11 @@ public class LogServiceImpl implements ILogService {
MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException) exception;
method = methodArgumentNotValidException.getParameter().getMethod();
}
String methodDescribe = StrFormatter.format("{}{}{}", ReflectCommonUtil.getMethodDescribeByMethod(method), StrUtil.C_COMMA, message);
String result = CommonResponseEnum.FAIL.getMessage();
String type = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateType();
String level = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateLevel();
String operateType = ReflectCommonUtil.getOperateTypeByMethod(method);
LogInfoDTO logInfoDTO = new LogInfoDTO(tempLogInfo.getUserName(), tempLogInfo.getIp(), methodDescribe, operateType, result.equalsIgnoreCase("失败") ? 0 : 1, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), isLogin);
LogInfoDTO logInfoDTO = new LogInfoDTO(tempLogInfo.getUserName(), tempLogInfo.getIp(), ReflectCommonUtil.getMethodDescribeByMethod(method), operateType, result.equalsIgnoreCase("失败") ? 0 : 1,message, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), userIndex);
publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 1, false);
}
@@ -132,13 +127,12 @@ public class LogServiceImpl implements ILogService {
MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException) exception;
method = methodArgumentNotValidException.getParameter().getMethod();
}
String methodDescribe = StrFormatter.format("{}{}{}", ReflectCommonUtil.getMethodDescribeByMethod(method), StrUtil.C_COMMA, message);
String result = CommonResponseEnum.FAIL.getMessage();
String ip = RequestUtil.getRealIp(request);
String type = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateType();
String level = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateLevel();
String operateType = ReflectCommonUtil.getOperateTypeByMethod(method);
LogInfoDTO logInfoDTO = new LogInfoDTO(userName, ip, methodDescribe, operateType, result.equalsIgnoreCase("失败") ? 0 : 1, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), 0);
LogInfoDTO logInfoDTO = new LogInfoDTO(userName, ip, ReflectCommonUtil.getMethodDescribeByMethod(method), operateType, result.equalsIgnoreCase("失败") ? 0 : 1,message, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), userName);
publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 1, false);
}

View File

@@ -26,6 +26,7 @@ import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
import java.util.Objects;
/**
@@ -70,7 +71,6 @@ public class RequestUtil {
String refreshToken = request.getParameter(SecurityConstants.REFRESH_TOKEN_KEY);
String payload = StrUtil.toString(JWSObject.parse(refreshToken).getPayload());
cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(payload);
String authenticationMethod = jsonObject.getStr(SecurityConstants.AUTHENTICATION_METHOD);
if (StrUtil.isBlank(authenticationMethod)) {
authenticationMethod = AuthenticationMethodEnum.USERNAME.getValue();
@@ -86,8 +86,8 @@ public class RequestUtil {
*/
public static HttpServletRequest getRequest() {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if(Objects.nonNull(requestAttributes)){
return ((ServletRequestAttributes)requestAttributes).getRequest();
if (Objects.nonNull(requestAttributes)) {
return ((ServletRequestAttributes) requestAttributes).getRequest();
}
return null;
}
@@ -132,8 +132,8 @@ public class RequestUtil {
*/
public static JSONObject getJwtPayload() {
JSONObject jsonObject = null;
String jwtPayload = getRequest().getHeader(SecurityConstants.JWT_PAYLOAD_KEY);
try {
String jwtPayload = getRequest().getHeader(SecurityConstants.JWT_PAYLOAD_KEY);
if (StrUtil.isNotBlank(jwtPayload)) {
jwtPayload = URLDecoder.decode(jwtPayload, StandardCharsets.UTF_8.toString());
jsonObject = JSONObject.fromObject(jwtPayload);
@@ -150,8 +150,8 @@ public class RequestUtil {
*/
public static JSONObject getJwtPayload(HttpServletRequest request) {
JSONObject jsonObject = null;
String jwtPayload = request.getHeader(SecurityConstants.JWT_PAYLOAD_KEY);
try {
String jwtPayload = request.getHeader(SecurityConstants.JWT_PAYLOAD_KEY);
if (StrUtil.isNotBlank(jwtPayload)) {
jwtPayload = URLDecoder.decode(jwtPayload, StandardCharsets.UTF_8.toString());
jsonObject = JSONObject.fromObject(jwtPayload);
@@ -170,8 +170,8 @@ public class RequestUtil {
JSONObject jsonObject = null;
HttpHeaders headers = request.getHeaders();
try {
String jwtPayload = headers.get(SecurityConstants.JWT_PAYLOAD_KEY).get(0);
if (StrUtil.isNotBlank(jwtPayload)) {
if (CollectionUtils.isNotEmpty(headers.get(SecurityConstants.JWT_PAYLOAD_KEY))) {
String jwtPayload = headers.get(SecurityConstants.JWT_PAYLOAD_KEY).get(0);
jwtPayload = URLDecoder.decode(jwtPayload, StandardCharsets.UTF_8.toString());
jsonObject = JSONObject.fromObject(jwtPayload);
}
@@ -182,6 +182,29 @@ public class RequestUtil {
return jsonObject;
}
/**
* HttpServletRequest获取在网关中存储的用户索引
*/
public static String getUserIndex(ServerHttpRequest request) {
String userIndex = LogInfo.UNKNOWN_USER;
JSONObject jwtPayload = getJwtPayload(request);
if (Objects.nonNull(jwtPayload)) {
userIndex = jwtPayload.getString(SecurityConstants.USER_INDEX_KEY);
}
return userIndex;
}
/**
* HttpServletRequest获取在网关中存储的用户索引
*/
public static String getUserIndex(HttpServletRequest request) {
String userIndex = LogInfo.UNKNOWN_USER;
JSONObject jwtPayload = getJwtPayload(request);
if (Objects.nonNull(jwtPayload)) {
userIndex = jwtPayload.getString(SecurityConstants.USER_INDEX_KEY);
}
return userIndex;
}
/**
* HttpServletRequest获取在网关中存储的用户索引