zoukankan      html  css  js  c++  java
  • 切面保存web访问记录

    package com.hn.xf.device.api.rest.aspect;
    import com.hn.xf.device.api.rest.authorization.manager.TokenManager;
    import com.hn.xf.device.api.rest.authorization.model.TokenModel;
    import com.hn.xf.device.api.rest.config.Constants;
    import com.hn.xf.device.service.log.service.AppLogService;
    import lombok.extern.slf4j.Slf4j;
    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 javax.servlet.http.HttpServletRequest;
    import java.util.Arrays;
    
    /**
     * @Created with IntelliJ IDEA.
     * @author: fengshufang
     * @Date: 2018/1/22
     * @Description APP日志
     */
    @Aspect
    @Component
    @Slf4j
    public class WebLogAspect {
        @Autowired
        AppLogService appLogService;
        @Autowired
        private TokenManager manager;
    
        @Pointcut("execution(public * com.hn.xf.device.api.rest.controller..*(..))")
        public void webLog() {
        }
    
        @Before("webLog()")
        public void doBefore(JoinPoint joinPoint) throws Throwable {
            // 接收到请求,记录请求内容
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            String authorization = request.getHeader(Constants.AUTHORIZATION);
            String user="";
            String cpid = request.getHeader(Constants.CPID);
            if (cpid == null ) {
                //验证token
                TokenModel model = manager.getToken(authorization);
                if(model!=null)
                {
                    user=model.getUserId().toString();
                }
            }
            else {
                user=cpid;
            }
            if (Arrays.toString(joinPoint.getArgs()).toString().length() < 500) {
                appLogService.add(user, joinPoint.getSignature().getName(), request.getRequestURL().toString() + request.getRemoteAddr() + Arrays.toString(joinPoint.getArgs()).toString());
            } else {
                String tempData = Arrays.toString(joinPoint.getArgs()).toString().substring(0,499);
                appLogService.add(user, joinPoint.getSignature().getName(), request.getRequestURL().toString() + request.getRemoteAddr() + tempData);
    
            }
    
        }
    
        @AfterReturning(returning = "ret", pointcut = "webLog()")
        public void doAfterReturning(Object ret) throws Throwable {
            // 处理完请求,返回内容
            //log.info("RESPONSE : " + ret);
        }
    
    }
    

      

  • 相关阅读:
    关于WEB页面的强制分页打印问题
    iOS地图多个自定义大头针绘制核心代码
    PHP生成随机码
    安卓动画小结
    WKWebView遇到的问题汇总
    实现图片预加载的几种方式
    苹果所有常用证书,appID,Provisioning Profiles配置说明及制作图文教程
    markdown语法博客园测试
    Agreement has been updated--Edit Phone Number最便捷解决办法(不需要安全提示问题和双重认证)
    Python黑帽子:自动化内存取证
  • 原文地址:https://www.cnblogs.com/anakin/p/8907324.html
Copyright © 2011-2022 走看看