Springboot 自定义注解(二) AOP 实现日志拦截

(一)JoinPoint类方法
java.lang.Object[] getArgs():获取连接点方法运行时的入参列表
Signature getSignature() :获取连接点的方法签名对象
java.lang.Object getTarget() :获取连接点所在的目标对象
java.lang.Object getThis() :获取代理对象本身

(二)自定义注解@SysLog

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysLog {
    String value() default "操作日志";
}

(三)使用Aop保存日志信息

package cn.ugwxcloud.ysdsquares.aop;

import java.lang.reflect.Method;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;

@Aspect
@Component
public class SysLogAspect {
	@Resource
	private	SysLogEntityMapper sysLogEntityMapper;
    /**
     * 切点
     */
    @Pointcut("@annotation(cn.ugwxcloud.ysdsquares.aop.SysLog)")
    public void logPointCut() {

    }
    /**
     * 前置通知
     * @param joinPoint 连接点
     * @param ret   方法返回值
     */
    /**
     * java.lang.Object[] getArgs():获取连接点方法运行时的入参列表
     * Signature getSignature() :获取连接点的方法签名对象
     * java.lang.Object getTarget() :获取连接点所在的目标对象
     * java.lang.Object getThis() :获取代理对象本身
     * @param joinPoint
     * @param ret
     */
    @AfterReturning(returning = "ret", pointcut = "logPointCut()")
    public void saveSysLog(JoinPoint joinPoint,Object ret) {
        // 获取连接点的方法签名对象
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

        SysLogEntityWithBLOBs sysLog = new SysLogEntityWithBLOBs();
        SysLog syslog = method.getAnnotation(SysLog.class);
        if (syslog != null) {
            //注解上的描述
            sysLog.setOperation(syslog.value());
        }


        //请求的方法名
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        sysLog.setMethod(className + "." + methodName + "()");

        //请求的参数
        Object[] args = joinPoint.getArgs();
        String params = JSON.toJSONString(args[0]);
        sysLog.setParams(params);

        //获取request
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();


        //设置IP地址
        sysLog.setIp(IPUtils.getIpAddr(request));

        HttpSession session = request.getSession();         
        SystemUser systemUser=   (SystemUser) session.getAttribute("userInfo");
        if(systemUser!=null){
        	sysLog.setUsername(systemUser.getName());
        	sysLog.setUseraccount(systemUser.getUserAccount());
        	
        }else{
        	sysLog.setUsername( "无法获取");
        	sysLog.setUseraccount("匿名未登录");

        }
        JsonResponse  re =  new JsonResponse();
        re =(JsonResponse) ret;

        if(ret!=null){
        	if(re.isSuccess()){
        		sysLog.setResult("success");
        	}else{
        		sysLog.setResult("false");
        	}
        	sysLog.setResponses(JSON.toJSONString(ret));
        }


        sysLog.setCreateDate(new Date());
        //保存系统日志
        sysLogEntityMapper.insert(sysLog);
    }



    @AfterThrowing(pointcut = "logPointCut()", throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint,Exception ex) {
        if (ex instanceof Exception) {
        	 MethodSignature signature = (MethodSignature) joinPoint.getSignature();
             Method method = signature.getMethod();

             SysLogEntityWithBLOBs sysLog = new SysLogEntityWithBLOBs();
             SysLog syslog = method.getAnnotation(SysLog.class);
             if (syslog != null) {
                 //注解上的描述
                 sysLog.setOperation(syslog.value());
             }
             //请求的方法名
             String className = joinPoint.getTarget().getClass().getName();
             String methodName = signature.getName();
             sysLog.setMethod(className + "." + methodName + "()");
             //请求的参数
             Object[] args = joinPoint.getArgs();
             String params = JSON.toJSONString(args[0]);
             sysLog.setParams(params);
             //获取request
             HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
             //设置IP地址
             sysLog.setIp(IPUtils.getIpAddr(request));
             HttpSession session = request.getSession();         
             SystemUser systemUser=   (SystemUser) session.getAttribute("userInfo");
             if(systemUser!=null){
             	sysLog.setUsername(systemUser.getName());
            	sysLog.setUseraccount(systemUser.getUserAccount());

             }else{
            		sysLog.setUsername( "无法获取");
                	sysLog.setUseraccount("匿名未登录");
             }
             	sysLog.setResult("error");
             sysLog.setCreateDate(new Date());
             //保存系统日志
             sysLogEntityMapper.insert(sysLog);
        } 
    }

    
    
}

(四) 使用该注解

猜你喜欢

转载自blog.csdn.net/qq_41446768/article/details/88020275