|
@@ -0,0 +1,73 @@
|
|
|
+package com.backendsys.modules.common.aspect;
|
|
|
+
|
|
|
+import com.backendsys.modules.common.config.security.utils.HttpRequestUtil;
|
|
|
+import com.backendsys.modules.system.dao.SysLogDao;
|
|
|
+import com.backendsys.modules.system.entity.SysLogEntity;
|
|
|
+import com.google.gson.Gson;
|
|
|
+import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
+import org.aspectj.lang.annotation.Around;
|
|
|
+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 java.lang.reflect.Method;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 系统日志,切面处理类
|
|
|
+ */
|
|
|
+@Aspect
|
|
|
+@Component
|
|
|
+public class SysLogAspect {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private HttpRequestUtil httpRequestUtil;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SysLogDao sysLogDao;
|
|
|
+
|
|
|
+ @Pointcut("@annotation(com.backendsys.modules.common.aspect.SysLog)")
|
|
|
+ public void logPointCut() {}
|
|
|
+
|
|
|
+ @Around("logPointCut()")
|
|
|
+ public Object around(ProceedingJoinPoint point) throws Throwable {
|
|
|
+ long beginTime = System.currentTimeMillis();
|
|
|
+ Object result = point.proceed();
|
|
|
+ // 执行时长(毫秒)
|
|
|
+ long time = System.currentTimeMillis() - beginTime;
|
|
|
+ // 保存日志
|
|
|
+ saveSysLog(point, time);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {
|
|
|
+ MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
|
|
+ Method method = signature.getMethod();
|
|
|
+
|
|
|
+ SysLogEntity sysLogEntity = new SysLogEntity();
|
|
|
+
|
|
|
+ // 注解上的描述
|
|
|
+ SysLog syslog = method.getAnnotation(SysLog.class);
|
|
|
+ if(syslog != null){
|
|
|
+ sysLogEntity.setOperation(syslog.value());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 请求方法名
|
|
|
+ String className = joinPoint.getTarget().getClass().getName();
|
|
|
+ String methodName = signature.getName();
|
|
|
+ sysLogEntity.setClassname(className);
|
|
|
+ sysLogEntity.setMethod(methodName);
|
|
|
+
|
|
|
+ // 请求参数
|
|
|
+ Object[] args = joinPoint.getArgs();
|
|
|
+ String params = new Gson().toJson(args);
|
|
|
+ sysLogEntity.setParams(params);
|
|
|
+
|
|
|
+ sysLogEntity.setIp(httpRequestUtil.getIpAddr());
|
|
|
+ sysLogEntity.setUsername(httpRequestUtil.getUserName());
|
|
|
+ sysLogEntity.setTime(time);
|
|
|
+ // 保存系统日志
|
|
|
+ sysLogDao.insert(sysLogEntity);
|
|
|
+ }
|
|
|
+}
|