注解接口
package com.bw.admin.common.annotation;
import com.bw.admin.common.enums.LogsEnums;
import java.lang.annotation.*;
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysLog {
LogsEnums type() default LogsEnums.QUERY;
boolean paramHide() default false;
String description() default "";
}
aop
package com.bw.admin.common.aop;
import com.bw.admin.common.annotation.SysLog;
import com.bw.admin.common.mongo.Dao.SysOperationLogDao;
import com.bw.admin.common.mongo.Entity.SysOperationLog;
import com.bw.admin.common.utils.ServletUtils;
import com.bw.admin.system.entity.Admin;
import com.coin.common.utils.HttpUtil;
import com.coin.common.utils.JsonHelper;
import com.jiu.common.mongo.dao.Page;
import lombok.extern.slf4j.Slf4j;
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.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Map;
@Aspect
@Component
@Order
@Slf4j
public class SysLogAspect {
@Autowired
private SysOperationLogDao sysOperationLogDao;
// defined aop pointcut
@Pointcut("@annotation(com.bw.admin.common.annotation.SysLog)")
public void controllerLog() {
}
// result of return
@AfterReturning(pointcut = "controllerLog()", returning = "result")
@Async
public void after(JoinPoint joinPoint, Object result) {
try{
HttpServletRequest request = ServletUtils.getRequest();
String requestIp = HttpUtil.clientIP(request);
//获取用户
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Admin admin = (Admin)auth.getPrincipal();
Class targetClass = joinPoint.getTarget().getClass();
String methodName = joinPoint.getSignature().getName();
Map<String, String[]> map = request.getParameterMap();//请求参数
Class[] parameterTypes = ((MethodSignature)joinPoint.getSignature()).getMethod().getParameterTypes();
Method method = targetClass.getMethod(methodName, parameterTypes);
Annotation annotation = method.getAnnotation(SysLog.class);
SysLog anLog = (SysLog)annotation;
String requestCode = anLog.type().getCode();
String requestName = anLog.type().getName();
//请求参数是否隐藏,隐藏请求参数,则不保留请求参数 比如修改密码
boolean paramHide = anLog.paramHide();
String res = result == null?"":JsonHelper.obj2JsonStr(result);
/* log.info("操作人:{},请求ip:{},请求地址:{},请求方法:{},请求参数:{},返回结果:{}",
admin.getEmail(),requestIp,request.getRequestURI(),requestName,map.toString(),res);*/
SysOperationLog sysOperationLog = null ;
if(paramHide){
sysOperationLog = new SysOperationLog(admin.getEmail(),requestIp,requestName,request.getRequestURI(),res,requestCode);
}else{
sysOperationLog = new SysOperationLog(admin.getEmail(),requestIp,requestName,request.getRequestURI(),map==null?"":map.toString(),res,requestCode);
}
sysOperationLogDao.save(sysOperationLog);
}catch (Exception e){
log.info("保存操作日志失败");
e.printStackTrace();
}
}
}