SpringAop拦截controller进行日志管理

一.使用自定义注解的方式进行操作日志管理
import java.lang.annotation.*;

/**
 * Created by Administrator on 2017/8/30.
 * 自定义拦截service
 */
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SystemLog {
    int operationType()  default 0;
    String opObject() default "";
}
二.操作日志拦截类
import com.findingroof.appserver.annotation.SystemLog;
import com.findingroof.appserver.contant.CacheConstant;
import com.findingroof.appserver.interceptor.LoginInterceptor;
import com.findingroof.appserver.operationlogservice.model.Operationlog;
import com.findingroof.appserver.operationlogservice.provider.OperationlogProvider;
import com.findingroof.appserver.userinfoservice.model.Userinfo;
import com.findingroof.appserver.util.DateUtil;
import com.findingroof.appserver.util.RedisUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;

/**
 */

@Component
@Aspect
public class SystemLogAction {
    @Autowired
    private static final Logger logger = LoggerFactory.getLogger(SystemLogAction.class);

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private OperationlogProvider operationlogProvider;

    //Controller层切点
    @Pointcut("@annotation(com.findingroof.appserver.annotation.SystemLog)")
    public void controllerAspect() {
    }

    /**
     * 前置通知 用于拦截Controller层记录用户的操作
     *
     * @param joinPoint 切点
     */
    @After(value = "controllerAspect() && @annotation(systemLog) && args(log,..)")
    public void after(JoinPoint joinPoint, SystemLog systemLog, Operationlog log) throws Throwable {
        long start = System.currentTimeMillis();
        logger.debug(log.toString());

        //常见日志实体对象
        log.setOptime(DateUtil.format(new Date(), DateUtil.FORMAT_DATETIME));
        log.setOpobject(systemLog.opObject());
        log.setOperationtype(systemLog.operationType());
        //获取登录用户账户
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        String name = (String) request.getAttribute("username");
        String token = request.getParameter("token");
        Userinfo userinfo = (Userinfo) redisUtils.get(CacheConstant.USERINFO + token);
//        String aopUsername = userinfo.getUsername();
       // log.setOpmen(userinfo.getUsername());
        

        //获取系统ip,这里用的是我自己的工具类,可自行网上查询获取ip方法
        String ip = request.getRemoteAddr();
        logger.debug(ip);

        // 拦截的实体类,就是当前正在执行的controller
        Object target = joinPoint.getTarget();
        logger.debug(target.toString());
        // 拦截的方法名称。当前正在执行的方法
        String methodName = joinPoint.getSignature().getName();
        logger.debug(methodName.toString());
        // 拦截的方法参数
        Object[] args = joinPoint.getArgs();
        Signature signature = joinPoint.getSignature();

        MethodSignature methodSignature = (MethodSignature) signature;
        String[] strings = methodSignature.getParameterNames();
        System.out.println(Arrays.toString(strings));

        for (int i = 0; i < args.length; i++) {
            logger.debug(String.valueOf(strings[i] + ":" + args[i].toString()));
        }

        //拦截的放参数类型
        Signature sig = joinPoint.getSignature();
        logger.debug(sig.toString());

        logger.debug("耗时:{}", System.currentTimeMillis() - start);
        operationlogProvider.insertOperationlog(log);
    }

}
三.使用注解测试

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值