java 自定义注解+AOP实现日志记录

java  自定义注解+AOP实现日志记录

1、自定义注解

package com.xxx.controller.framework.auth;

import java.lang.annotation.*;

@Target({ ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ControllerAop {
    String url() default "";

    /** 动作的名称 */
    String action() default "";
}

2、通过AOP+反射

package com.xxx.controller.framework.auth;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xxx.controller.base.controller.BaseController;
import com.xxx.model.common.system.user.MipUser;
import com.xxx.model.common.system.userlog.SysUserlog;
import com.xxx.service.SysUserlogService;
import com.xxx.service.UserService;
import com.xxx.utils.OrgUtil;
import com.xxx.utils.Tools;
import com.xxx.utils.common.IdUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 操作日志记录处理
 *
 * @author xiaoao
 */

@Aspect
@Component
public class ControllerAopOperator extends BaseController{


    @Autowired
    private SysUserlogService sysUserlogService;


	HttpServletRequest request = null;
	
	//声明AOP切入点,凡是使用了XXXOperateLog的方法均被拦截
    @Pointcut("@annotation(com.xxx.controller.framework.auth.ControllerAop)")
    public void log() {
//        System.out.println("我是一个切入点");
    }


    /**
     * 前置通知 用于拦截Controller层记录用户的操作
     *
     * @param joinPoint 切点
     */
    @AfterReturning(pointcut = "log()")
    public void doBefore(JoinPoint joinPoint)
        {
            handleLog(joinPoint, null);
    }

    @AfterThrowing(value = "log()", throwing = "e")
    public void doAfter(JoinPoint joinPoint, Exception e)
    {
        handleLog(joinPoint, e);
    }


    private void handleLog(JoinPoint joinPoint, Exception e)
    {
        try
        {
            // 获得注解
            ControllerAop controllerAop = giveController(joinPoint);
            if (controllerAop== null)
            {
                return;
            }
            System.out.println(controllerAop.action()+">>>>>>>>>>>>>>>>>>>>>>>");
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                    .getRequest();

            // *========数据库日志=========*//
            SysUserlog sysUserlog = new SysUserlog();
            // 请求的IP
            sysUserlog.setUserName(Tools.getUserName());
//            sysUserlog.setUserName("sysadmin");
            sysUserlog.setLoginTime(new Date());
            String clientIp = getIpAddr(request);
            sysUserlog.setLoginIp(clientIp);
            sysUserlog.setUrl(request.getRequestURI());
            sysUserlog.setRequestParam(JSON.toJSONString(request.getParameterMap()));



            // 处理设置注解上的参数
            getControllerMethodDescription(controllerAop,sysUserlog,request);
            // 保存数据库
            sysUserlogService.insertUserlog(sysUserlog);
        }
        catch (Exception ex)
        {
            // 记录本地异常日志
            ex.printStackTrace();
        }
    }


    /**
     * 是否存在注解,如果存在就记录日志
     *
     * @param joinPoint
     * @param  //controllerAop
     * @return
     * @throws Exception
     */
    private static ControllerAop giveController(JoinPoint joinPoint) throws Exception
    {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();

        if (method != null)
        {
            return method.getAnnotation(ControllerAop.class);
        }
        return null;
    }




    /**
     * 获取注解中对方法的描述信息 用于Controller层注解
     *
     * @param  /joinPoint 切点
     * @return 方法描述
     * @throws Exception
     */
    public static void getControllerMethodDescription(ControllerAop  controllerAop, SysUserlog sysUserlog,
                                                      HttpServletRequest request) throws Exception
    {
        // 设置action动作
        sysUserlog.setAction(controllerAop.action());

    }



    /**
     * 获取请求的参数,放到log中
     *
     * @param sysUserlog
     * @param request
     */
    @SuppressWarnings("all")
    private static void setRequestValue(SysUserlog sysUserlog, HttpServletRequest request)
    {
        if (sysUserlog == null){
            sysUserlog = new SysUserlog();
            Map map = request.getParameterMap();
            String params = JSONObject.toJSONString(map);
            sysUserlog.setRequestParam(params);
        }

    }

}

 

上一篇:如何使两台机器不通过密码连接起来(linux)


下一篇:springboot整合MongoDB