|
|
|
@ -0,0 +1,210 @@
|
|
|
|
|
package com.supervision.ai.service.hub.aop;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
|
import com.supervision.ai.service.hub.service.AuditLogService;
|
|
|
|
|
import jakarta.servlet.ServletRequest;
|
|
|
|
|
import jakarta.servlet.ServletResponse;
|
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
|
|
import lombok.Data;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.aspectj.lang.JoinPoint;
|
|
|
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
|
|
import org.aspectj.lang.annotation.*;
|
|
|
|
|
import org.aspectj.lang.reflect.MethodSignature;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
|
import java.io.StringWriter;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 日志切面
|
|
|
|
|
*
|
|
|
|
|
* @author :liu
|
|
|
|
|
* create :2020-11-16 10:50
|
|
|
|
|
**/
|
|
|
|
|
@Component
|
|
|
|
|
@Aspect
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class RequestLogAop {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private AuditLogService auditLogService;
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("all")
|
|
|
|
|
@Around("within(com..*..controller..*) && @within(org.springframework.web.bind.annotation.RestController)")
|
|
|
|
|
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
|
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
|
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
|
|
HttpServletRequest request = attributes.getRequest();
|
|
|
|
|
|
|
|
|
|
AuditLogDTO auditLogDTO = new AuditLogDTO(request);
|
|
|
|
|
Map<String, Object> requestParams = getRequestParamsByProceedingJoinPoint(proceedingJoinPoint);
|
|
|
|
|
auditLogDTO.setRequestParams(JSONUtil.toJsonStr(requestParams));
|
|
|
|
|
auditLogDTO.setUserId((String) requestParams.get("userId"));
|
|
|
|
|
AuditLogThreadHolder.AUDIT_LOG.set(auditLogDTO);// 把数据缓存到treadLocal中
|
|
|
|
|
// 执行目标方法
|
|
|
|
|
Object result = proceedingJoinPoint.proceed();
|
|
|
|
|
|
|
|
|
|
// 目标方法执行后
|
|
|
|
|
RequestInfo requestInfo = new RequestInfo();
|
|
|
|
|
requestInfo.setUrl(request.getRequestURL().toString());
|
|
|
|
|
requestInfo.setHttpMethod(request.getMethod());
|
|
|
|
|
requestInfo.setClassMethod(String.format("%s.%s", proceedingJoinPoint.getSignature().getDeclaringTypeName(),
|
|
|
|
|
proceedingJoinPoint.getSignature().getName()));
|
|
|
|
|
requestInfo.setRequestParams(requestParams);
|
|
|
|
|
Object response = proceedResult(result);
|
|
|
|
|
requestInfo.setResult(response);
|
|
|
|
|
requestInfo.setTimeCost(System.currentTimeMillis() - start);
|
|
|
|
|
log.info("Request Info: {}", JSONUtil.toJsonStr(requestInfo));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
auditLogDTO.setRecordType("0");
|
|
|
|
|
if (response instanceof String){
|
|
|
|
|
auditLogDTO.setResponse((String) response);
|
|
|
|
|
}
|
|
|
|
|
auditLogDTO.setEndTime(LocalDateTime.now());
|
|
|
|
|
auditLogService.saveAuditLog(auditLogDTO);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("保存审计信息出错,保存内容:{}",JSONUtil.toJsonStr(auditLogDTO), e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Object proceedResult(Object result) {
|
|
|
|
|
String value = null;
|
|
|
|
|
try {
|
|
|
|
|
value = JSONUtil.toJsonStr(result);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
value = result.toString();
|
|
|
|
|
}
|
|
|
|
|
if (StrUtil.isNotBlank(value) && value.length() > 10240){
|
|
|
|
|
value = value.substring(0, 1024) + "......" + value.substring(value.length() - 1024);
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
if (StrUtil.isNotEmpty(value)){
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("all")
|
|
|
|
|
@AfterThrowing(pointcut = "@within(org.springframework.web.bind.annotation.RestController)", throwing = "e")
|
|
|
|
|
public void doAfterThrow(JoinPoint joinPoint, RuntimeException e) {
|
|
|
|
|
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
|
|
HttpServletRequest request = attributes.getRequest();
|
|
|
|
|
RequestErrorInfo requestErrorInfo = new RequestErrorInfo();
|
|
|
|
|
requestErrorInfo.setUrl(request.getRequestURL().toString());
|
|
|
|
|
requestErrorInfo.setHttpMethod(request.getMethod());
|
|
|
|
|
requestErrorInfo.setClassMethod(String.format("%s.%s", joinPoint.getSignature().getDeclaringTypeName(),
|
|
|
|
|
joinPoint.getSignature().getName()));
|
|
|
|
|
requestErrorInfo.setRequestParams(getRequestParamsByJoinPoint(joinPoint));
|
|
|
|
|
requestErrorInfo.setException(e);
|
|
|
|
|
log.info("Error Request Info : {}", JSONUtil.toJsonStr(requestErrorInfo));
|
|
|
|
|
|
|
|
|
|
AuditLogDTO auditLogDTO = AuditLogThreadHolder.AUDIT_LOG.get();
|
|
|
|
|
if (null != auditLogDTO){
|
|
|
|
|
try {
|
|
|
|
|
auditLogDTO.setEndTime(LocalDateTime.now());
|
|
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
|
e.printStackTrace(new PrintWriter(sw));
|
|
|
|
|
auditLogDTO.setExceptionDesc(sw.toString());
|
|
|
|
|
auditLogDTO.setRecordType("1");
|
|
|
|
|
auditLogService.saveAuditLog(auditLogDTO);
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
log.warn("保存审计信息出错,保存内容:{}",JSONUtil.toJsonStr(auditLogDTO), ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@After(value = "@within(org.springframework.web.bind.annotation.RestController)")
|
|
|
|
|
public void doAfter(JoinPoint joinPoint) {
|
|
|
|
|
|
|
|
|
|
// 清理线程变量
|
|
|
|
|
AuditLogThreadHolder.AUDIT_LOG.remove();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取入参
|
|
|
|
|
*
|
|
|
|
|
* @param proceedingJoinPoint
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private Map<String, Object> getRequestParamsByProceedingJoinPoint(ProceedingJoinPoint proceedingJoinPoint) {
|
|
|
|
|
//参数名
|
|
|
|
|
String[] paramNames = ((MethodSignature) proceedingJoinPoint.getSignature()).getParameterNames();
|
|
|
|
|
//参数值
|
|
|
|
|
Object[] paramValues = proceedingJoinPoint.getArgs();
|
|
|
|
|
|
|
|
|
|
return buildRequestParam(paramNames, paramValues);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Map<String, Object> getRequestParamsByJoinPoint(JoinPoint joinPoint) {
|
|
|
|
|
//参数名
|
|
|
|
|
String[] paramNames = ((MethodSignature) joinPoint.getSignature()).getParameterNames();
|
|
|
|
|
//参数值
|
|
|
|
|
Object[] paramValues = joinPoint.getArgs();
|
|
|
|
|
|
|
|
|
|
return buildRequestParam(paramNames, paramValues);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Map<String, Object> buildRequestParam(String[] paramNames, Object[] paramValues) {
|
|
|
|
|
Map<String, Object> requestParams = new HashMap<>();
|
|
|
|
|
if (paramNames == null){
|
|
|
|
|
return requestParams;
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < paramNames.length; i++) {
|
|
|
|
|
Object value = paramValues[i];
|
|
|
|
|
if (value instanceof ServletRequest || value instanceof ServletResponse) {
|
|
|
|
|
//ServletRequest不能序列化,从入参里排除,否则报异常:java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode (i.e. isAsyncStarted() returns false)
|
|
|
|
|
//ServletResponse不能序列化 从入参里排除,否则报异常:java.lang.IllegalStateException: getOutputStream() has already been called for this response
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//如果是文件对象
|
|
|
|
|
if (value instanceof MultipartFile) {
|
|
|
|
|
MultipartFile file = (MultipartFile) value;
|
|
|
|
|
//获取文件名
|
|
|
|
|
value = file.getOriginalFilename();
|
|
|
|
|
}
|
|
|
|
|
requestParams.put(paramNames[i], value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return requestParams;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Data
|
|
|
|
|
public static class RequestInfo {
|
|
|
|
|
private String url;
|
|
|
|
|
private String httpMethod;
|
|
|
|
|
private String classMethod;
|
|
|
|
|
private Object requestParams;
|
|
|
|
|
private Object result;
|
|
|
|
|
private Long timeCost;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Data
|
|
|
|
|
public static class RequestErrorInfo {
|
|
|
|
|
private String url;
|
|
|
|
|
private String httpMethod;
|
|
|
|
|
private String classMethod;
|
|
|
|
|
private Object requestParams;
|
|
|
|
|
private RuntimeException exception;
|
|
|
|
|
}
|
|
|
|
|
}
|