161 lines
6.2 KiB
Java
Raw Normal View History

2023-02-16 15:28:15 +08:00
package com.zhgd.xmgl.config;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.zhgd.annotation.OperLog;
import com.zhgd.xmgl.modules.basicdata.entity.OperationLog;
import com.zhgd.xmgl.modules.basicdata.service.IOperationLogService;
2024-06-28 20:51:36 +08:00
import com.zhgd.xmgl.security.util.SecurityUtils;
2023-02-16 15:28:15 +08:00
import com.zhgd.xmgl.util.IpUtil;
2024-04-14 21:05:01 +08:00
import lombok.extern.slf4j.Slf4j;
2024-06-28 20:51:36 +08:00
import org.apache.commons.lang3.ArrayUtils;
2023-02-16 15:28:15 +08:00
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
2024-07-09 11:07:16 +08:00
import org.springframework.web.multipart.MultipartFile;
2023-02-16 15:28:15 +08:00
import javax.servlet.http.HttpServletRequest;
2024-06-28 20:51:36 +08:00
import javax.servlet.http.HttpServletResponse;
2023-02-16 15:28:15 +08:00
import java.lang.reflect.Method;
2024-06-28 20:51:36 +08:00
import java.util.*;
import java.util.stream.Collectors;
2023-02-16 15:28:15 +08:00
/**
* @program: wisdomSite
* @description: 切面处理类操作日志记录处理
* @author: Mr.Peng
* @create: 2021-05-06 18:04
**/
@Aspect
@Component
2024-04-14 21:05:01 +08:00
@Slf4j
2023-02-16 15:28:15 +08:00
public class OperLogAspect {
@Autowired
private IOperationLogService operationLogService;
/**
* 设置操作日志切入点 记录操作日志 在注解的位置切入代码
2024-06-28 20:51:36 +08:00
*/
2023-02-16 15:28:15 +08:00
@Pointcut("@annotation(com.zhgd.annotation.OperLog)")
2024-06-28 20:51:36 +08:00
public void operLogPoinCut() {
2023-02-16 15:28:15 +08:00
}
/**
2024-06-28 20:51:36 +08:00
* 设置操作异常切入点记录异常日志 扫描所有controller包下操作
*/
2023-02-16 15:28:15 +08:00
@Pointcut("execution(* com.*.*.*.*.controller..*.*(..))")
2024-06-28 20:51:36 +08:00
public void operExceptionLogPoinCut() {
2023-02-16 15:28:15 +08:00
}
/**
2024-06-28 20:51:36 +08:00
* 正常返回通知拦截用户操作日志连接点正常执行完成后执行 如果连接点抛出异常则不会执行
*
* @param joinPoint 切入点
* @param keys 返回结果
*/
2023-02-16 15:28:15 +08:00
@AfterReturning(value = "operLogPoinCut()", returning = "keys")
public void saveOperLog(JoinPoint joinPoint, Object keys) {
2024-06-28 20:51:36 +08:00
// 获取RequestAttributes
2023-02-16 15:28:15 +08:00
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
// 从获取RequestAttributes中获取HttpServletRequest的信息
2024-07-09 11:07:16 +08:00
HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
2023-02-16 15:28:15 +08:00
OperationLog operlog = new OperationLog();
try {
2024-07-09 11:07:16 +08:00
// 主键ID
//operlog.setOperId(IdGenerator.getUUID());
2023-02-16 15:28:15 +08:00
// 从切面织入点处通过反射机制获取织入点处的方法
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
// 获取切入点所在的方法
Method method = signature.getMethod();
// 获取操作
OperLog opLog = method.getAnnotation(OperLog.class);
if (opLog != null) {
String operModul = opLog.operModul();
String operType = opLog.operType();
String operDesc = opLog.operDesc();
2024-07-09 11:07:16 +08:00
// 操作模块
operlog.setOperModul(operModul);
// 操作类型
operlog.setOperType(operType);
// 操作描述
operlog.setOperDesc(operDesc);
2023-02-16 15:28:15 +08:00
}
// 获取请求的类名
String className = joinPoint.getTarget().getClass().getName();
// 获取请求的方法名
String methodName = method.getName();
methodName = className + "." + methodName;
operlog.setOperMethod(methodName);
// 请求的参数
Map<String, String> rtnMap = converMap(request.getParameterMap());
// 将参数所在的数组转换成json
String params = JSON.toJSONString(rtnMap);
2024-07-09 11:07:16 +08:00
// 请求参数
operlog.setOperRequParam(params);
2024-06-28 20:51:36 +08:00
Object[] args = joinPoint.getArgs();
if (ArrayUtils.isNotEmpty(args)) {
2024-07-09 11:07:16 +08:00
List<Object> logArgs = Arrays.stream(args).filter(arg -> (!(arg instanceof HttpServletRequest) && !(arg instanceof HttpServletResponse) && !(arg instanceof MultipartFile))).collect(Collectors.toList());
2024-06-28 20:51:36 +08:00
operlog.setBody(JSON.toJSONString(logArgs));
} else {
log.info("Request Args : null");
}
2023-02-16 15:28:15 +08:00
// 请求用户ID
String operUserId = request.getHeader("operateId");
2024-06-28 20:51:36 +08:00
operlog.setOperUserId(operUserId);
if (StringUtils.isBlank(operUserId) && SecurityUtils.getUser() != null) {
operlog.setOperUserId(String.valueOf(SecurityUtils.getUser().getUserId()));
2023-02-16 15:28:15 +08:00
}
//operlog.setOperUserName(); // 请求用户名称
2024-07-09 11:07:16 +08:00
// 请求IP
operlog.setOperIp(IpUtil.getRemortIP(request));
// 请求URI
operlog.setOperUri(request.getRequestURI());
// 请求方法
operlog.setOperMethod(request.getMethod());
// 创建时间
operlog.setOperCreateTime(new Date());
2024-06-28 20:51:36 +08:00
if ("com.zhgd.xmgl.modules.basicdata.controller.LoginController.login".equals(operlog.getOperMethod())) {
2023-02-16 15:28:15 +08:00
if (keys != null) {
2024-06-28 20:51:36 +08:00
JSONObject json = JSONUtil.parseObj(keys);
if ("200".equals(json.getStr("code"))) {
JSONObject jsonObject = json.getJSONObject("result");
String userId = jsonObject.getStr("userId");
2024-07-19 10:31:31 +08:00
operlog.setOperUserId(userId);
2023-02-16 15:28:15 +08:00
}
}
}
2024-07-19 10:31:31 +08:00
operationLogService.save(operlog);
2023-02-16 15:28:15 +08:00
} catch (Exception e) {
2024-04-14 21:05:01 +08:00
log.error("error", e);
2023-02-16 15:28:15 +08:00
}
}
2024-06-28 20:51:36 +08:00
/**
2023-02-16 15:28:15 +08:00
* 转换request 请求参数
*
* @param paramMap request获取的参数数组
*/
2024-06-28 20:51:36 +08:00
public Map<String, String> converMap(Map<String, String[]> paramMap) {
Map<String, String> rtnMap = new HashMap<String, String>();
for (String key : paramMap.keySet()) {
rtnMap.put(key, paramMap.get(key)[0]);
}
return rtnMap;
}
2023-02-16 15:28:15 +08:00
}