`

springboot使用aop切面做用户操作日志记录

    博客分类:
  • JAVA
阅读更多
参考
https://blog.csdn.net/u2133048/article/details/98178861

https://blog.csdn.net/puhaiyang/article/details/78146620


spring cloud微服务架构想实现操作日志的切面
比如:rest层
    @ApiOperation("保存用户")
    @SysLogIntercept(action="save", description="保存用户")
    @RequestMapping(value = "saveUserInfo",method = RequestMethod.POST)
    public ObjectRestResponse<UserVo> saveUserInfo(@RequestBody UserVo userVo){
        if (userVo==null || userVo.getDeptId()==null || userVo.getJobId()==null ) {
            log.info("userVo为null 或 deptId为null 或 jobId为null=============");
            return new ObjectRestResponse().code(CodeConstants.STATUS_CODE_DATA_ID_NULL);
        }

        Msg msg = userManager.saveUserInfo(userVo);
        return new ObjectRestResponse().code(msg.getCode()).data(msg.getData()).bool(msg.getSucFlag()).msg(msg.getDesc()) ;
    }



    @ApiOperation("删除用户")
    @SysLogIntercept(action="delete", description="删除用户")
    @RequestMapping(value = "deleteUserInfo",method = RequestMethod.POST)
    public ObjectRestResponse deleteUserInfo(@RequestParam String objectIdStr) {
        if (StringUtils.isBlank(objectIdStr)) {
            log.info("objectIdStr为空=============");
            return new ObjectRestResponse().code(CodeConstants.STATUS_CODE_DATA_ID_NULL) ;
        }

        Msg msg = userManager.deleteUserInfo(objectIdStr.trim()) ;
        return new ObjectRestResponse().msg(msg.getDesc()).code(msg.getSucFlag() ? 0 : msg.getCode());
    }


SysLogIntercept是我定义的一个切面
因为新增保存和编辑保存是一个接口,需要区分操作日志是新增还是编辑,所以需要获取请求的参数,判断请求的参数中id的值是否为空
//获取目标方法的参数信息
        Object[] obj = joinPoint.getArgs();
        for (Object argItem : obj) {
            Object idObj = getValueByKey(argItem, "id");
            Long idLong = null ;
            idObj = (idObj!=null) ? (Long)idObj : null ;
            idValue = (idObj!=null) ? String.valueOf(idObj) : null ;
            System.out.println("---->saveParamInfo-->idValue:" + idValue);
        }


保存的时候,也不是调用了保存接口就一定会成功,有可能会保存提示重复,此时要区分保存后的返回数据是否成功,才插入日志
    /**
     * 环绕通知 @Around , 当然也可以使用 @Before (前置通知) @After (后置通知)
     *
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @AfterReturning(pointcut="@annotation(com.ronglian.kangrui.saasprivate.research.admin.components.SysLogIntercept)", returning="rvt")
    public void AfterExec(JoinPoint joinPoint,Object rvt){
        String sysAction = this.getSysAction(joinPoint) ;
        ObjectRestResponse restResponse = (ObjectRestResponse)rvt ;

        if(sysAction.equals("save")) {
            // 新增或编辑
            if (restResponse.getBoolResult()) {
                long beginTime = System.currentTimeMillis();
                long time = System.currentTimeMillis() - beginTime;

                Object object = ((ObjectRestResponse) rvt).getData() ;
                Long resultId = (object!=null) ? (Long)getValueByKey(object, "id")  :  null ;
                String resultIdStr = (object!=null) ? String.valueOf(resultId) :  null ;
                saveLog(joinPoint, resultIdStr);
            }
        } else {
            // 删除
            if (restResponse.getCode()==0){
                saveLog(joinPoint, idValue);
            }
        }

        log.info("rvt=={}", JSON.toJSONString(rvt) );
    }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics