当前位置 博文首页 > 俺叫啥好嘞的博客:java 设置操作日志切入点
package com.jrtc.backend.interceptor;
import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.jrtc.base.annotations.OperationLogAnnotation;
import com.jrtc.base.entity.bo.AdminBO;
import com.jrtc.base.entity.vo.SysOperLogVO;
import com.jrtc.base.util.IpUtil;
import com.jrtc.dao.AdminDao;
import org.apache.commons.lang.ArrayUtils;
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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Aspect
@Component
public class OperationLogAspect {
@Autowired
private AdminDao adminDao;
/**
* 设置操作日志切入点 在注解的位置切入代码
*/
@Pointcut("@annotation(com.jrtc.base.annotations.OperationLogAnnotation)")
public void operLogPoinCut() {
}
@AfterReturning(returning = "result", value = "operLogPoinCut()")
public void saveOperLog(JoinPoint joinPoint, Object result) throws Throwable {
// 获取RequestAttributes
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
// 从获取RequestAttributes中获取HttpServletRequest的信息
HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
try {
SysOperLogVO sysOperLogVO = new SysOperLogVO();
//获取登录人信息
String token = request.getHeader("token");
String mobile = JWT.decode(token).getAudience().get(0);
AdminBO adminBO = adminDao.queryAdminInfoByMobile(mobile);
sysOperLogVO.setAdminId(adminBO.getId());
// 从切面织入点处通过反射机制获取织入点处的方法
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//获取切入点所在的方法
Method method = signature.getMethod();
//获取操作
OperationLogAnnotation annotation = method.getAnnotation(OperationLogAnnotation.class);
Object[] args = joinPoint.getArgs();
if(args.length>0){
Stream<?> stream = ArrayUtils.isEmpty(args) ? Stream.empty() : Arrays.stream(args);
List<Object> logArgs = stream
.filter(arg -> (!(arg instanceof HttpServletRequest) && !(arg instanceof HttpServletResponse)))
.collect(Collectors.toList());
//过滤后序列化无异常
String jsonStr = JSONObject.toJSONString(logArgs);
sysOperLogVO.setOperData(jsonStr);
}
if (annotation != null) {
sysOperLogVO.setOperModule(annotation.operModule());
sysOperLogVO.setOperType(annotation.operType());
sysOperLogVO.setOperDesc(annotation.operDesc());
}
//操作IP
IpUtil ipUtil = new IpUtil();
sysOperLogVO.setIp(ipUtil.getIpAddr(request));
//保存日志
adminDao.addSysOperLog(sysOperLogVO);
} catch (Exception e) {
e.printStackTrace();
}
}
}
参考
cs