|
@@ -0,0 +1,105 @@
|
|
|
+package cn.minbb.job.aspect;
|
|
|
+
|
|
|
+import cn.minbb.job.controller.UniqueNameGenerator;
|
|
|
+import cn.minbb.job.data.Const;
|
|
|
+import cn.minbb.job.model.User;
|
|
|
+import cn.minbb.job.model.WebLog;
|
|
|
+import cn.minbb.job.service.WebLogService;
|
|
|
+import cn.minbb.job.system.UserSession;
|
|
|
+import cn.minbb.job.util.IPUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import lombok.extern.log4j.Log4j2;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.aspectj.lang.JoinPoint;
|
|
|
+import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
+import org.aspectj.lang.annotation.*;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+
|
|
|
+@Aspect
|
|
|
+@Component
|
|
|
+@Log4j2
|
|
|
+public class WebLogAspect {
|
|
|
+
|
|
|
+ private final IPUtil ipUtil;
|
|
|
+ private final WebLogService webLogService;
|
|
|
+
|
|
|
+ public WebLogAspect(IPUtil ipUtil, WebLogService webLogService) {
|
|
|
+ this.ipUtil = ipUtil;
|
|
|
+ this.webLogService = webLogService;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Pointcut("execution(public * cn.minbb.job.controller..*.*(..))")
|
|
|
+ public void webLogPointCut() {
|
|
|
+ }
|
|
|
+
|
|
|
+ @Before("webLogPointCut()")
|
|
|
+ public void doBefore(JoinPoint joinPoint) throws Throwable {
|
|
|
+ // 接收到请求
|
|
|
+ }
|
|
|
+
|
|
|
+ @AfterReturning(pointcut = "webLogPointCut()", returning = "object")
|
|
|
+ public void doAfterReturning(Object object) throws Throwable {
|
|
|
+ // 处理完请求
|
|
|
+ }
|
|
|
+
|
|
|
+ @Around("webLogPointCut()")
|
|
|
+ public Object doAround(ProceedingJoinPoint point) throws Throwable {
|
|
|
+ // 记录方法开始执行时间
|
|
|
+ long startTime = System.currentTimeMillis();
|
|
|
+ // 获取目标类名称 // point.getSignature().getDeclaringTypeName()
|
|
|
+ String className = point.getTarget().getClass().getName();
|
|
|
+ // 获取目标类方法名称
|
|
|
+ String classMethodName = className.replaceAll(UniqueNameGenerator.class.getPackage().getName(), "*") + "#" + point.getSignature().getName();
|
|
|
+ // 入参
|
|
|
+ Object[] args = point.getArgs();
|
|
|
+ // 记录请求日志
|
|
|
+ WebLog webLog = null;
|
|
|
+ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
+ if (null != attributes) {
|
|
|
+ HttpServletRequest request = attributes.getRequest();
|
|
|
+ String queryString = request.getQueryString();
|
|
|
+ String requestURI = request.getRequestURI() + (StringUtils.isEmpty(queryString) ? "" : ("?" + request.getQueryString()));
|
|
|
+ String requestMethod = request.getMethod();
|
|
|
+ String requestProtocol = request.getProtocol();
|
|
|
+ String requestHeader = request.getHeader("User-Agent");
|
|
|
+ String ipAddress = ipUtil.getIpAddress(request);
|
|
|
+ // 请求用户
|
|
|
+ User user = UserSession.getUserAuthentication();
|
|
|
+ String userRemark = null;
|
|
|
+ if (null != user) {
|
|
|
+ User userItem = new User();
|
|
|
+ userItem.setId(user.getId());
|
|
|
+ userItem.setUsername(user.getUsername());
|
|
|
+ userItem.setName(user.getName());
|
|
|
+ userRemark = JSON.toJSONString(userItem);
|
|
|
+ }
|
|
|
+ webLog = webLogService.saveOne(new WebLog(requestURI, requestMethod, requestProtocol, requestHeader, classMethodName, null, userRemark, ipAddress));
|
|
|
+ if (request.getAttribute("intercept") != null) {
|
|
|
+ log.info("{} -> {} -> 拒绝访问", requestMethod, requestURI);
|
|
|
+ } else {
|
|
|
+ log.info("{} -> {} {} data -> {}", requestMethod, requestURI, classMethodName, String.valueOf(args));
|
|
|
+ }
|
|
|
+ // 关联请求信息
|
|
|
+ request.setAttribute(Const.Key.KEY_REMARK, webLog.getId());
|
|
|
+ }
|
|
|
+ // 方法执行
|
|
|
+ Object object = point.proceed();
|
|
|
+ // 更新请求日志 - 记录请求时长
|
|
|
+ if (null != webLog) {
|
|
|
+ // 处理时长
|
|
|
+ int processTime = (int) (System.currentTimeMillis() - startTime);
|
|
|
+ webLog.setProcessTime(processTime);
|
|
|
+ webLogService.saveOne(webLog);
|
|
|
+ }
|
|
|
+ return object;
|
|
|
+ }
|
|
|
+
|
|
|
+ @AfterThrowing(value = "webLogPointCut()", throwing = "e")
|
|
|
+ public void afterReturningMethod(JoinPoint joinPoint, Exception e) {
|
|
|
+ log.error("记录访问日志异常 -> ", e);
|
|
|
+ }
|
|
|
+}
|