栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springBoot通过aop实现日志记录

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springBoot通过aop实现日志记录

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));
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/826749.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号