使用spring aop 进行用户行为跟踪错误

领导要求进行用户操作行为跟踪,我是用aop来做,在使用切点时匹配了相应的方法,可是无法排除指定类的方法,经过个总努力,错误在
@Pointcut("execution(* cn.jesong.webcall.console.service..add*(..)) && !execution(* cn.jesong.webcall.console.service.LogService.*(..))")
之前看是写成了

@Pointcut("execution(* cn.jesong.webcall.console.service..add*(..)) and !execution(* cn.jesong.webcall.console.service.LogService.*(..))")

and和&&的错误,铭记!!!



 package cn.jesong.webcall.console.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
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 cn.jesong.webcall.console.dto.CustomerDto;
import cn.jesong.webcall.console.dto.LogDto;
import cn.jesong.webcall.console.service.LogService;
@Component
@Aspect
public class LogForUserAction {
    
    @Autowired
    private LogService logService;
    
    @Pointcut("execution(* cn.jesong.webcall.console.service..add*(..)) && !execution(* cn.jesong.webcall.console.service.LogService.*(..))")
    public void  pointUserAdd(){}
    
    @Pointcut("execution(* cn.jesong.webcall.console.service..update*(..)) ! execution(* cn.jesong.webcall.console.service.LogService.*(..))")
    public void  pointUserUpdate(){}
    
    @Pointcut("execution(* cn.jesong.webcall.console.service..delete*(..))  ! execution(* cn.jesong.webcall.console.service.LogService.*(..))")
    public void  pointUserDelete(){}
    
    @Pointcut("execution(* cn.jesong.webcall.console.service.*.get*(..))  ! execution(* cn.jesong.webcall.console.service.LogService.*(..))")
    public void  pointUserGet(){}
    @AfterReturning("pointUserAdd()")
    public void noteUserAdd(JoinPoint joinPoint){
        this.addAction(joinPoint,"insert");
    }
    @AfterReturning("pointUserUpdate()")
    public void noteUserUpdate(JoinPoint joinPoint){
        this.addAction(joinPoint,"update");
    }
    @AfterReturning(value="pointUserDelete()")
    public void noteUserDelete(JoinPoint joinPoint){
        this.addAction(joinPoint,"delete");
    }
    @AfterReturning(value="pointUserGet()")
    public void noteUserGet(JoinPoint joinPoint){
        this.addAction(joinPoint,"select");
    }
    
    public void addAction(JoinPoint joinPoint,String behavior){
        System.out.println("this is "+behavior);
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        CustomerDto customer = (CustomerDto)request.getSession().getAttribute("customer");
        System.out.println(customer.getCustomerId()+" aaaa "+customer.getUserName()+" aaaa  "+ customer.getPassword());
        if(customer==null){
            return;
        }
        if(joinPoint.getArgs() == null){
            return ;
        }
        //获得方法名
        String methodName = joinPoint.getSignature().getName();
        //获取操作内容
        String acContext = getUserContext(joinPoint.getArgs(),methodName);
        //创建日志对象
        LogDto logDto = new LogDto();
        logDto.setUserid(Integer.parseInt(customer.getCustomerId()));
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format.format(new Date());
        try {
            logDto.setTime(format.parse(time));
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        logDto.setInfo(acContext);
        logDto.setBehavior(behavior);
        
        try {
            logService.addLog(logDto);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public String getUserContext(Object[] args,String methodName){
        String context =null;
        //参数
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < args.length; i++) {
            sb.append(args[i]);
        }
        context = sb+methodName;
        return context;
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值