1. 注解类
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface PlatformLoginLog {
}
2. 切面类
package com.siwei.os.log.aspect;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.servlet.ServletUtil;
import com.github.pagehelper.util.StringUtil;
import com.siwei.common.pojo.Result;
import com.siwei.os.authorization.LoginUser;
import com.siwei.os.authorization.TokenCacheManager;
import com.siwei.os.log.aspect.logAnnotation.PlatformOperateLog;
import com.siwei.os.log.listener.event.LogPlatformLoginEvent;
import com.siwei.os.log.listener.event.LogPlatformOperEvent;
import com.siwei.os.log.pojo.po.LogPlatformLogin;
import com.siwei.os.log.pojo.po.LogPlatformOper;
import com.siwei.os.platform.pojo.vo.GetPlatformUser;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
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.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
@Aspect
@Component
@Slf4j
public class PlatformLogAspect {
public static final String TOKEN = "token";
@Resource
private TokenCacheManager tokenCacheManager;
@Resource
private HttpServletRequest request;
@Resource
private ApplicationEventPublisher publisher;
@Pointcut("@annotation(com.siwei.os.log.aspect.logAnnotation.PlatformOperateLog)")
public void pointCut() {
}
@Around("pointCut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
//从切面织入点处通过反射机制获取织入点处的方法
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//获取切入点所在的方法
Method method = signature.getMethod();
//获取token并解析出用户id
String nowToken = request.getHeader(TOKEN);
String token = tokenCacheManager.getCacheToken(nowToken);
Long userId = null;
if (!StringUtil.isEmpty(token)) {
userId = tokenCacheManager.getUserId(token);
}
PlatformOperateLog selfLog = method.getAnnotation(PlatformOperateLog.class);
//操作日志实体类对象
LogPlatformOper operateLog = new LogPlatformOper();
operateLog.setUserId(userId);
//切入该方法 设置操作路径
operateLog.setOperPath(request.getRequestURI()).setOperDesc(selfLog.operateDescDesc());
// 通过事件监听操作数据库
publisher.publishEvent(new LogPlatformOperEvent(operateLog));
// 执行目标方法
Object obj = joinPoint.proceed();
return obj;
}
@Pointcut("@annotation(com.siwei.os.log.aspect.logAnnotation.PlatformLoginLog)")
public void LoginPointCut() {
}
//returning 拿到aop的后置通知即方法的返回值
@AfterReturning(value = "LoginPointCut()",returning = "result")
public void login(JoinPoint joinPoint, Result result) {
String token = tokenCacheManager.getCacheToken((String) result.getData());
LoginUser user = null;
if (!StringUtil.isEmpty(token)) {
user = tokenCacheManager.getLoginUser(token);
}
//这是操作日志实体类对象
LogPlatformLogin logPlatformLogin = new LogPlatformLogin();
GetPlatformUser platformUser = new GetPlatformUser();
BeanUtil.copyProperties(joinPoint.getArgs()[0], platformUser);
// 设置ip地址
String ip = ServletUtil.getClientIP(request);
logPlatformLogin.setIpAddr(ip);
//设置userId
logPlatformLogin.setUserId(user.getUserId());
// 设置mac地址 终端类型
logPlatformLogin.setMacAddr(platformUser.getMac());
logPlatformLogin.setTerminalType(platformUser.getPlatform());
// 通过事件监听 添加平台操作日志表
publisher.publishEvent(new LogPlatformLoginEvent(logPlatformLogin));
}
}