|
@@ -2,9 +2,11 @@ package com.qmth.teachcloud.common.aspect;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.qmth.teachcloud.common.annotation.OperationLogDetail;
|
|
|
+import com.qmth.teachcloud.common.bean.dto.LogArgsDto;
|
|
|
import com.qmth.teachcloud.common.contant.SystemConstant;
|
|
|
import com.qmth.teachcloud.common.entity.BasicOperationLog;
|
|
|
import com.qmth.teachcloud.common.entity.SysUser;
|
|
|
+import com.qmth.teachcloud.common.service.BasicOperationLogService;
|
|
|
import com.qmth.teachcloud.common.util.ServletUtil;
|
|
|
import org.aspectj.lang.JoinPoint;
|
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
@@ -13,10 +15,14 @@ import org.aspectj.lang.reflect.MethodSignature;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.validation.BeanPropertyBindingResult;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
|
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* @Description: 日志操作记录拦截aop
|
|
@@ -26,6 +32,8 @@ import java.util.Map;
|
|
|
@Aspect
|
|
|
@Component
|
|
|
public class LogAspect {
|
|
|
+ @Resource
|
|
|
+ private BasicOperationLogService basicOperationLogService;
|
|
|
|
|
|
private final static Logger log = LoggerFactory.getLogger(LogAspect.class);
|
|
|
|
|
@@ -33,12 +41,13 @@ public class LogAspect {
|
|
|
* 日志注解切入点
|
|
|
*/
|
|
|
@Pointcut("@annotation(com.qmth.teachcloud.common.annotation.OperationLogDetail)")
|
|
|
- public void operationLog(){
|
|
|
+ public void operationLog() {
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 环绕增强,操作日志环绕切入
|
|
|
+ *
|
|
|
* @param joinPoint joinPoint
|
|
|
* @return return
|
|
|
* @throws Throwable Throwable
|
|
@@ -48,14 +57,14 @@ public class LogAspect {
|
|
|
Object res = null;
|
|
|
long time = System.currentTimeMillis();
|
|
|
try {
|
|
|
- res = joinPoint.proceed();
|
|
|
+ res = joinPoint.proceed();
|
|
|
time = System.currentTimeMillis() - time;
|
|
|
return res;
|
|
|
} finally {
|
|
|
try {
|
|
|
//方法执行完成后增加日志
|
|
|
- addOperationLog(joinPoint,res,time);
|
|
|
- }catch (Exception e){
|
|
|
+ addOperationLog(joinPoint, res, time);
|
|
|
+ } catch (Exception e) {
|
|
|
System.out.println("LogAspect 操作失败:" + e.getMessage());
|
|
|
e.printStackTrace();
|
|
|
}
|
|
@@ -64,21 +73,29 @@ public class LogAspect {
|
|
|
|
|
|
/**
|
|
|
* 记录日志操作
|
|
|
+ *
|
|
|
* @param joinPoint joinPoint
|
|
|
- * @param res 返回值
|
|
|
- * @param time 方法执行时间
|
|
|
+ * @param res 返回值
|
|
|
+ * @param time 方法执行时间
|
|
|
*/
|
|
|
- private void addOperationLog(JoinPoint joinPoint, Object res, long time){
|
|
|
+ private void addOperationLog(JoinPoint joinPoint, Object res, long time) {
|
|
|
SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
|
|
|
String userName = requestUser.getRealName();
|
|
|
+ HttpServletRequest request = ServletUtil.getRequest();
|
|
|
+ MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
|
|
+ String clazzName = signature.getDeclaringTypeName();
|
|
|
+ String methodName = signature.getName();
|
|
|
+ Object[] args = joinPoint.getArgs();
|
|
|
+ String[] paramsName = signature.getParameterNames();
|
|
|
|
|
|
- MethodSignature signature = (MethodSignature)joinPoint.getSignature();
|
|
|
BasicOperationLog operationLog = new BasicOperationLog();
|
|
|
|
|
|
// 方法中的信息
|
|
|
operationLog.setId(SystemConstant.getDbUuid());
|
|
|
- operationLog.setMethod(signature.getDeclaringTypeName() + "." + signature.getName());
|
|
|
- operationLog.setArgs(JSON.toJSONString(joinPoint.getArgs()));
|
|
|
+ operationLog.setUrl(request.getServletPath());
|
|
|
+ operationLog.setClazzName(clazzName);
|
|
|
+ operationLog.setMethod(methodName);
|
|
|
+ operationLog.setArgs(this.getTranslateArgs(paramsName, args));
|
|
|
operationLog.setRunTime(time);
|
|
|
operationLog.setReturnValue(JSON.toJSONString(res));
|
|
|
operationLog.setUserId(requestUser.getId());
|
|
@@ -87,30 +104,45 @@ public class LogAspect {
|
|
|
|
|
|
// 注解中的信息
|
|
|
OperationLogDetail annotation = signature.getMethod().getAnnotation(OperationLogDetail.class);
|
|
|
- if(annotation != null){
|
|
|
- operationLog.setDetail(getDetail(((MethodSignature)joinPoint.getSignature()).getParameterNames(),joinPoint.getArgs(),annotation));
|
|
|
+ if (annotation != null) {
|
|
|
+ operationLog.setDetail(getDetail(((MethodSignature) joinPoint.getSignature()).getParameterNames(), joinPoint.getArgs(), annotation));
|
|
|
operationLog.setLevel(annotation.level());
|
|
|
operationLog.setOperationType(annotation.operationType());
|
|
|
operationLog.setOperationUnit(annotation.operationUnit());
|
|
|
operationLog.setSystemType(annotation.system());
|
|
|
}
|
|
|
- //TODO 这里保存日志
|
|
|
- System.out.println(JSON.toJSONString(operationLog));
|
|
|
+ // 保存操作日志
|
|
|
+ basicOperationLogService.save(operationLog);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getTranslateArgs(String[] paramsName, Object[] args) {
|
|
|
+ List<LogArgsDto> logArgsDtoList = new ArrayList<>();
|
|
|
+ for (int i = 0; i < args.length; i++) {
|
|
|
+ if (Objects.nonNull(args[i]) && (args[i] instanceof HttpServletRequest || args[i] instanceof HttpServletResponse || args[i] instanceof CommonsMultipartFile || args[i] instanceof MultipartFile || args[i] instanceof BeanPropertyBindingResult)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ LogArgsDto logArgsDto = new LogArgsDto();
|
|
|
+ logArgsDto.setArg(String.valueOf(args[i]));
|
|
|
+ logArgsDto.setParamName(paramsName[i]);
|
|
|
+ logArgsDtoList.add(logArgsDto);
|
|
|
+ }
|
|
|
+ return JSON.toJSONString(logArgsDtoList);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 对当前登录用户和占位符处理
|
|
|
- * @param argNames 方法参数名称数组
|
|
|
- * @param args 方法参数数组
|
|
|
+ *
|
|
|
+ * @param argNames 方法参数名称数组
|
|
|
+ * @param args 方法参数数组
|
|
|
* @param annotation 注解信息
|
|
|
* @return 返回处理后的描述
|
|
|
*/
|
|
|
- private String getDetail(String[] argNames, Object[] args, OperationLogDetail annotation){
|
|
|
+ private String getDetail(String[] argNames, Object[] args, OperationLogDetail annotation) {
|
|
|
SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
|
|
|
|
|
|
Map<Object, Object> map = new HashMap<>();
|
|
|
- for(int i = 0;i < argNames.length;i++){
|
|
|
- map.put(argNames[i],args[i]);
|
|
|
+ for (int i = 0; i < argNames.length; i++) {
|
|
|
+ map.put(argNames[i], args[i]);
|
|
|
}
|
|
|
|
|
|
String detail = annotation.detail();
|
|
@@ -121,20 +153,21 @@ public class LogAspect {
|
|
|
Object v = entry.getValue();
|
|
|
detail = detail.replace("{{" + k + "}}", JSON.toJSONString(v));
|
|
|
}
|
|
|
- }catch (Exception e){
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return detail;
|
|
|
}
|
|
|
|
|
|
@Before(value = "operationLog()")
|
|
|
- public void doBeforeAdvice(JoinPoint joinPoint){
|
|
|
+ public void doBeforeAdvice(JoinPoint joinPoint) {
|
|
|
System.out.println("进入方法前执行.....");
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 处理完请求,返回内容
|
|
|
+ *
|
|
|
* @param ret ret
|
|
|
*/
|
|
|
@AfterReturning(returning = "ret", pointcut = "operationLog()")
|
|
@@ -146,7 +179,7 @@ public class LogAspect {
|
|
|
* 后置异常通知
|
|
|
*/
|
|
|
@AfterThrowing(value = "operationLog()")
|
|
|
- public void throwss(JoinPoint jp){
|
|
|
+ public void throwss(JoinPoint jp) {
|
|
|
System.out.println("方法异常时执行.....");
|
|
|
}
|
|
|
|
|
@@ -155,7 +188,7 @@ public class LogAspect {
|
|
|
* 后置最终通知,final增强,不管是抛出异常或者正常退出都会执行
|
|
|
*/
|
|
|
@After(value = "operationLog()")
|
|
|
- public void after(JoinPoint jp){
|
|
|
+ public void after(JoinPoint jp) {
|
|
|
System.out.println("方法最后执行.....");
|
|
|
}
|
|
|
}
|