首页>代码>基于SpringBoot+Vue实现的物流快递仓库管理系统>/服务端代码/api/src/main/java/com/example/api/aspect/LogAspect.java
package com.example.api.aspect;

import com.example.api.annotation.Log;
import com.example.api.model.entity.SystemLog;
import com.example.api.service.SystemLogService;
import com.example.api.utils.IpUtil;
import com.example.api.utils.JwtTokenUtil;
import org.aspectj.lang.ProceedingJoinPoint;
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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.time.LocalDateTime;

@Aspect
@Component
public class LogAspect {
    @Autowired
    private SystemLogService logService;

    /*
        表明切点
     */
    @Pointcut("@annotation(com.example.api.annotation.Log)")
    public void pt(){}

    /*
       环绕通知
     */
    @Around("pt()")
    public Object Around(ProceedingJoinPoint point) throws Throwable {
        //记录开始时间
        long beginTime = System.currentTimeMillis();
        Object res = null;
        try {
            //执行方法
            res = point.proceed();
        }finally {
            //计算执行时长
            long time = System.currentTimeMillis() - beginTime;
            recordLog(point);
        }
        return res;
    }

    private void recordLog(ProceedingJoinPoint point){
        //获取当前请求对象
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        //获取目标方法签名
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        Log annotation = method.getAnnotation(Log.class);
        //封装日志对象
        SystemLog systemLog = new SystemLog();
        systemLog.setModule(annotation.moudle());
        systemLog.setBusincessType(annotation.type().getName());
        systemLog.setIp(IpUtil.getIpAddr(request));
        systemLog.setTime(LocalDateTime.now());
        //获取方法的全路径
        systemLog.setMethod(signature.getDeclaringTypeName()+"."+signature.getName());
        //获取token,并解析token来获取当前账号
        String token = request.getHeader(JwtTokenUtil.TOKEN_HEADER);
        systemLog.setAccount(JwtTokenUtil.getUsername(token));
        //持久化到数据库
        logService.record(systemLog);
    }
}
最近下载更多
5418888  LV3 5月13日
luhong  LV3 5月6日
xp95323  LV15 3月6日
hubugai1  LV11 2月20日
微信网友_6961718086799360  LV2 2024年12月22日
sshiqi  LV2 2024年12月11日
3993zby  LV2 2024年11月11日
初心不负丶方得始终  LV11 2024年8月22日
mlml123  LV6 2024年8月20日
最近浏览更多
zhaopb  LV8 5月17日
5418888  LV3 5月13日
ljy050925 5月13日
暂无贡献等级
luhong  LV3 5月6日
黄志琴  LV1 4月13日
youwuzuichen  LV11 4月3日
aaappp  LV1 3月25日
billy-zuidaima  LV1 3月24日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友