java aspectj + 自定义注解实现操作日志


@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface OperatorLog {
    /**
     * 日志模块:登录/退出/基础维护/系统管理/订单管理/结算管理
     */
    String logModule() default "";
    /**
     * 页面名称
     */
     String pageName() default "";
    /**
     * 日志类型:insert/update/delete/login/logout
     */
     String logType() default "";
    /**
     * 日志内容
     */
     String logContent() default "";
    /**
     * 通知类型 Before(方法前)/After(方法后)
     */
    String adviseType() default "After";
@Component
@Aspect
public class OperatorLogAop {

    @Resource
    HerOperatorLogService herOperatorLogService;

    @Resource
    CurrentUserMethodArgumentResolver currentUserMethodArgumentResolver;

    //公共切入点
    @Pointcut("@annotation(com.br.web.anno.OperatorLog)")
    public void pointcut() {
    }

    /**
     * 后置通知
     * @param joinPoint
     * @param operatorLog
     */
    @After("pointcut() && @annotation(operatorLog)")
    public void doAfterInService(JoinPoint joinPoint,  OperatorLog operatorLog) {
        //如果通知类型为Before不执行以下语句
        if(operatorLog.adviseType().equals("Before") ){
            return;
        }
        UserVo userVo = null;
        try {
            userVo = currentUserMethodArgumentResolver.getUserVo();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //调用本类添加日志方法
        this.addOperatorLog(operatorLog,userVo);

    }
    /**
     * 前置通知(专为登出设计,因为用后置通知先退出,就没有用户信息了)
     * @param joinPoint
     * @param operatorLog
     */
    @Before("pointcut() && @annotation(operatorLog)")
    public void doBeforeInService(JoinPoint joinPoint,  OperatorLog operatorLog) {
        //如果通知类型为After不执行以下语句
        if(operatorLog.adviseType().equals("After") ){
            return;
        }
        UserVo userVo = null;
        try {
            userVo = currentUserMethodArgumentResolver.getUserVo();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //调用本类添加日志方法
        this.addOperatorLog(operatorLog,userVo);
    }

    /**
     * @param operatorLog 操作信息
     * @param userVo 用户信息
     */
    public void addOperatorLog(OperatorLog operatorLog,UserVo userVo){
            //从获取RequestAttributes中获取HttpServletRequest的信息
            HttpServletRequest httpServletRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            //登录日志
            HerOperatorLog log = new HerOperatorLog();
            log.setLogModule(operatorLog.logModule());
            log.setPageName(operatorLog.pageName());
            log.setIdOperatorUser(userVo.getIdUser());
            log.setOperatorUser(userVo.getName());
            log.setLogType(operatorLog.logType());
            //登录、登出时这两个字段不会用到,占用字段较长,不能存入日志
            userVo.setHerRoles(null);
            userVo.setRoles(null);
            //如果没有指定操作内容,则把整个用户的实体,转为string作为内容
            if (StringUtils.isNotBlank(operatorLog.logContent())){
                log.setLogContent(operatorLog.logContent());
            }else{
                log.setLogContent(userVo.toString());
            }
            log.setLogIp(IpUtils.getRealRequestIp(httpServletRequest));
            herOperatorLogService.insert(log);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北凉军

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值