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); } }
原文地址:https://www.cnblogs.com/anakin/p/8907324.html
时间: 2024-10-10 19:43:18