码迷,mamicode.com
首页 > Web开发 > 详细

切面保存web访问记录

时间:2018-04-22 15:59:48      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:org   lang   private   oaf   text   thold   lan   man   name   

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访问记录

标签:org   lang   private   oaf   text   thold   lan   man   name   

原文地址:https://www.cnblogs.com/anakin/p/8907324.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!