AOP实现用户操作日志记录

一、自定义注解

在使用aop切面拦截的时候需要统一的日志描述,这个日志描述,我们使用自定义注解来为每个方法添加日志自定义描述内容。

创建一个自定义注解类

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

SpringMvc配置aop
SpringMvc.xm增加以下代码

xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

<aop:aspectj-autoproxy proxy-target-class="true" />
     <!-- aop的执行类 -->
     <bean id="userLogAopAction" class="com.topwalk.bdp.log.web.UserLogAopAction"/>

AOP拦截执行类

@Aspect
public class UserLogAopAction extends WebController {

    @Autowired
    private UserLogService userLogService;

    @Pointcut("execution(* com.topwalk.icgap.*.web..*.*(..))")
    private void controllerAspect() {}// 定义一个切入点

    @Around("controllerAspect()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {

        // 创建日志实体对象
        UserLog log = new UserLog();

        // 获取当前登录用户
        User userInfo = HttpUtil.getSessionUser();
        // 设置用户信息
        log.setAccount(userInfo.getAccount());
        log.setUserId(userInfo.getId());
        log.setUserName(userInfo.getName());
        log.setOrgCode(userInfo.getOrgCode());
        log.setOrgName(userInfo.getOrgName());
        // 当前IP地址
        String theIp = HttpUtil.getIpAddr();
        log.setIp(theIp);

        // 获取当前拦截的实体类
        /*Object target = pjp.getTarget();*/
        // 拦截的方法参数类型
        Signature sig = pjp.getSignature();

        MethodSignature methodSignature = (MethodSignature) sig;

        Object object = null;
        Method method = null;
        try {
            /* method = target.getClass().getMethod(methodName, parameterTypes); */
            method = methodSignature.getMethod();
        } catch (SecurityException e1) {
            e1.printStackTrace();
        }

        if (null != method) {
            // 判断是否包含自定义的注解
            if (method.isAnnotationPresent(UserLogAnnotation.class)) {
                UserLogAnnotation userLogAnnotation =  method
                        .getAnnotation(UserLogAnnotation.class);
                String module = userLogAnnotation.module();
                String methods = userLogAnnotation.methods();

                try {
                    //拼接描述信息
                    String description = "[帐号:"+log.getAccount()+"],"+"操作"+module+"模块,"+"执行"+methods;
                    log.setDescription(description);
                    log.setModule(Module.OTHER);
                    log.setFunction(Function.OTHER);
                    super.info(log);

                    object = pjp.proceed();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            } else {// 没有包含注解
                object = pjp.proceed();
            }
        } else { // 不需要拦截直接执行
            object = pjp.proceed();
        }
        return object;

    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值