日志增强类——前置(后置)通知、获取方法路径、用户名、访问时间、ip

日志增强类

package com.service;

import com.dao.LogMapper;
import com.model.SysLog;
import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.UUID;

@Component
@Aspect
public class LogAop {

    //访问时间
    private Date visitedTime;
    //类
    private Class visitedClass;
    //方法
    private Method visitedMethod;
    //完整方法路径
    private String method;
    //执行时间
    private long executionTime;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private LogMapper mapper;

    //前置通知
    //获取访问的类、方法
    @Before("execution(* com.controller.*Controller.*(..))")
    public void before(JoinPoint joinPoint) throws NoSuchMethodException {
        //获取访问时间
        visitedTime = new Date();
        //获取访问的类
        visitedClass=joinPoint.getTarget().getClass();
        //获取方法名
        String methodName = joinPoint.getSignature().getName();
        //获取方法的参数
        Object[] args = joinPoint.getArgs();

        //获取方法
        if (args==null ||args.length == 0){
            //没有参数,填入方法名,获取方法
            visitedMethod=visitedClass.getMethod(methodName);
        }else {
            //有参数,获取参数的类型
            Class[] argsClass = new Class[args.length];
            for(int i = 0;i<args.length;i++){
                //获取每个参数的类型
                argsClass[i] = args[i].getClass();
            }
            //填入方法名、参数类型 获取方法
            visitedMethod = visitedClass.getMethod(methodName, argsClass);
        }

        //拼接类名和方法名,获取完整方法路径
        method = visitedClass.getName()+"."+visitedMethod.getName();
    }

    //前置通知
    //获取 url username ip
    @After("execution(* com.controller.*Controller.*(..))")
    public void after(JoinPoint joinPoint){
        //获取执行时间
        executionTime = new Date().getTime() - visitedTime.getTime();
        //获取客户端ip地址
        String ip = request.getRemoteAddr();
        //获取username
        SecurityContext securityContext = SecurityContextHolder.getContext();
        String username = ((User) (securityContext.getAuthentication().getPrincipal())).getUsername();
        //获取url
        //类的Url
         RequestMapping classAnnotation = (RequestMapping)visitedClass.getAnnotation(RequestMapping.class);
        String[] classUrl = classAnnotation.value();
        //方法的Url
        String[] methodUrl = visitedMethod.getAnnotation(RequestMapping.class).value();
        //拼接Url
        String url = classUrl[0]+methodUrl[0];

        SysLog sysLog = new SysLog(visitedTime,username,ip,url,executionTime,method);
        sysLog.setId(UUID.randomUUID().toString());
        mapper.insertLog(sysLog);
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值