自定义注解 使用AOP实现用户访问日志

[java]  view plain  copy
  1. 首先编写一个自定义注解,值为操作的内容  
[java]  view plain  copy
  1. /** 
  2.  * 系统日志注解 
  3.  */  
  4. @Target(ElementType.METHOD)  
  5. @Retention(RetentionPolicy.RUNTIME)  
  6. @Documented  
  7. public @interface SysLog {  
  8.   
  9.     String value() default "";  
  10. }  
[java]  view plain  copy
  1. /** 
  2.  * 系统日志 
  3.  */  
  4. @Aspect  
  5. @Component  
  6. public class SysLogAspect {  
  7. <span class="space" style="white-space:pre; display:inline-block; text-indent:2em; line-height:inherit"> 定义切面类,切注解</span>  
  8.     @Pointcut("@annotation(com.webworld.marlboro.ecp.base.annotation.SysLog)")  
  9.     public void logPointCut() {  
  10.   
  11.     }  
  12. <span class="space" style="white-space:pre; display:inline-block; text-indent:2em; line-height:inherit"> 环绕切</span>  
  13.     @Around("logPointCut()")  
  14.     public Object around(ProceedingJoinPoint point) throws Throwable {  
  15.         long beginTime = System.currentTimeMillis();  
  16.         //执行方法  
  17.         Object result = point.proceed();  
  18.         //执行时长(毫秒)  
  19.         long time = System.currentTimeMillis() - beginTime;  
  20.   
  21.         //保存日志  
  22.         saveUserLog(point, time);  
  23.   
  24.         return result;  
  25.     }  
  26.   
  27.     private void saveUserLog(ProceedingJoinPoint joinPoint, long time) {  
  28.         MethodSignature signature = (MethodSignature) joinPoint.getSignature();  
  29.         Method method = signature.getMethod();  
  30.   
  31.         UserLog userLog = new UserLog();  
  32.         SysLog syslog = method.getAnnotation(SysLog.class);  
  33.         if (syslog != null) {  
  34.             //注解上的描述  
  35.             userLog.setOperation(syslog.value());  
  36.         }  
  37.   
  38.         //请求的方法名  
  39.         String className = joinPoint.getTarget().getClass().getName();  
  40.         String methodName = signature.getName();  
  41.         // 方法返回值  
  42.         AnnotatedType annotatedReturnType = method.getAnnotatedReturnType();  
  43.         Type returnType = annotatedReturnType.getType();  
  44.         String returnTypeName = returnType.getTypeName();  
  45.   
  46.         //请求的参数  
  47.         Object[] args = joinPoint.getArgs();  
  48.   
  49.         userLog.setMethod(returnTypeName + " " + className + "." + methodName + "(" + Arrays.toString(args) + ")");  
  50.         try {  
  51.             String params = new Gson().toJson(args[0]);  
  52.             userLog.setParameters(params);  
  53.         } catch (Exception e) {  
  54.   
  55.         }  
  56.   
  57.         //获取request  
  58.         HttpServletRequest request = HttpContextUtils.getHttpServletRequest();  
  59.   
  60.         //获取请求类型  
  61.         String requestMethod = request.getMethod();  
  62.         userLog.setRequestType(requestMethod);  
  63.   
  64.         //获取userAgent  
  65.         String userAgent = request.getHeader("User-Agent");  
  66.         userLog.setUserAgent(userAgent);  
  67.   
  68.         String requestURI = request.getRequestURI();  
  69.         userLog.setUrl(requestURI);  
  70.   
  71.         //设置IP地址  
  72.         userLog.setIp(IPUtils.getIpAddr(request));  
  73.   
  74.         Long userId = NumberUtils.createLong(StringUtils.valueOf(HttpContextUtils.getAttribute(SessionConstant.USER_ID)));  
  75.         Long shopId = NumberUtils.createLong(StringUtils.valueOf(HttpContextUtils.getAttribute(SessionConstant.SHOP_ID)));  
  76.   
  77.         if (userId == null) {  
  78.             String weChatOpenId = StringUtils.valueOf(HttpContextUtils.getAttribute(SessionConstant.WE_CHAT_OPEN_ID));  
  79.             if (StringUtils.isNotBlank(weChatOpenId)) {  
  80.                 User user = userService.findByWeChatOpenId(weChatOpenId);  
  81.                 if (user != null) {  
  82.                     userId = user.getId();  
  83.                     Shop shop = shopService.findByUserId(userId);  
  84.                     shopId = shop == null ? null : shop.getId();  
  85.                 }  
  86.             }  
  87.         }  
  88.         userLog.setUserId(userId);  
  89.         userLog.setShopId(shopId);  
  90.   
  91.         userLog.setTime(time);  
  92.         userLog.setRequestDate(new Date());  
  93.   
  94.         //保存系统日志  
  95.         userLogService.save(userLog);  
  96.     }  
  97. }    
[java]  view plain  copy
  1. @SysLog("修改当前用户信息")  
  2.     @PutMapping(value = "/current/info")  
  3.     public Result updateUserInfo(@RequestBody UserVo userVo) throws ParseException {  
  4. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值