|
@@ -0,0 +1,154 @@
|
|
|
+package com.migao.config.aop;
|
|
|
+
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.migao.config.authority.AuthenticationProperties;
|
|
|
+import com.migao.config.authority.LoginUtils;
|
|
|
+import com.migao.config.constant.LogType;
|
|
|
+import com.migao.config.log.LogAnnotation;
|
|
|
+import com.migao.entity.vo.req.LogHistoryInsertReq;
|
|
|
+import com.migao.service.LogHistoryService;
|
|
|
+import com.migao.util.UrlMatcherUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.aspectj.lang.JoinPoint;
|
|
|
+import org.aspectj.lang.annotation.After;
|
|
|
+import org.aspectj.lang.annotation.Aspect;
|
|
|
+import org.aspectj.lang.annotation.Before;
|
|
|
+import org.aspectj.lang.annotation.Pointcut;
|
|
|
+import org.aspectj.lang.reflect.CodeSignature;
|
|
|
+import org.aspectj.lang.reflect.MethodSignature;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.time.Duration;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Configuration
|
|
|
+@Slf4j
|
|
|
+@Aspect
|
|
|
+@Component
|
|
|
+public class LogAspect {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private LogHistoryService logHistoryService;
|
|
|
+ @Resource
|
|
|
+ private AuthenticationProperties authenticationProperties;
|
|
|
+
|
|
|
+ private final ThreadLocal<LocalDateTime> currentThread = new ThreadLocal<>();
|
|
|
+
|
|
|
+ @Pointcut("execution(public * com.migao.controller..*.*(..)))")
|
|
|
+ public void logAspect() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // @Before("logAspect()"+
|
|
|
+// "&& @annotation(cn.jiheng.ypd.config.log.LogAnnotation)")
|
|
|
+ @Before("logAspect()")
|
|
|
+ public void doBeforeGame() {
|
|
|
+ ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
+ if (servletRequestAttributes != null) {
|
|
|
+ String uri = servletRequestAttributes.getRequest().getRequestURI();
|
|
|
+ log.info("接收请求:{}", uri);
|
|
|
+ currentThread.set(LocalDateTime.now());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @After("logAspect()")
|
|
|
+ public void doAfterReturningGame(JoinPoint joinPoint) {
|
|
|
+ if (currentThread.get() != null) {
|
|
|
+ ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
+ if (servletRequestAttributes != null) {
|
|
|
+ String uri = servletRequestAttributes.getRequest().getRequestURI();
|
|
|
+ log.info("请求地址:{},耗时:{}毫秒", uri, Duration.between(currentThread.get(), LocalDateTime.now()).toMillis());
|
|
|
+ Integer millis = Math.toIntExact(Duration.between(currentThread.get(), LocalDateTime.now()).toMillis());
|
|
|
+ // 获取方法注解的权限
|
|
|
+ MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
|
|
|
+ Method method = methodSignature.getMethod();
|
|
|
+ // 获取日志操作类型
|
|
|
+ LogAnnotation annotation = method.getAnnotation(LogAnnotation.class);
|
|
|
+ String typeName = "";
|
|
|
+ String tableName = "";
|
|
|
+ if (annotation == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ LogType type = annotation.operationType();
|
|
|
+ typeName = type.name();
|
|
|
+ tableName = annotation.describe();
|
|
|
+
|
|
|
+ //获取请求参数
|
|
|
+ Map<String, Object> param = new HashMap<>();
|
|
|
+ Object[] paramValues = joinPoint.getArgs();
|
|
|
+ String[] paramNames = ((CodeSignature) joinPoint.getSignature()).getParameterNames();
|
|
|
+ //忽略参数uri
|
|
|
+ if (!UrlMatcherUtil.matches(authenticationProperties.getLogIgnoreParamList(), uri)) {
|
|
|
+ for (int i = 0; i < paramNames.length; i++) {
|
|
|
+ param.put(paramNames[i], paramValues[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //记录日志
|
|
|
+ ServletRequestAttributes requestAttributes =
|
|
|
+ (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
+ if (requestAttributes == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String string = null;
|
|
|
+ if (annotation != null) {
|
|
|
+ if (annotation.isParameters().equals("是")) {
|
|
|
+ string = JSONObject.toJSONString(param);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ HttpServletRequest request =
|
|
|
+ requestAttributes.getRequest();
|
|
|
+ Long userId = null;
|
|
|
+ if (StringUtils.isNotBlank(request.getHeader(authenticationProperties.getHeaderName()))) {
|
|
|
+ if (!uri.contains("login")) {
|
|
|
+ userId = LoginUtils.getLoginUserId();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ logHistoryService.insert(
|
|
|
+ LogHistoryInsertReq
|
|
|
+ .builder()
|
|
|
+ .deleted(0)
|
|
|
+ .type(typeName)
|
|
|
+ .tableName(tableName)
|
|
|
+ .requestTime(currentThread.get())
|
|
|
+ .uri(uri)
|
|
|
+ .params(string)
|
|
|
+ .duration(millis)
|
|
|
+ .userId(userId)
|
|
|
+ .ip(getRemoteHost(request))
|
|
|
+ .build()
|
|
|
+ );
|
|
|
+ currentThread.remove();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取目标主机的ip
|
|
|
+ *
|
|
|
+ * @param request request
|
|
|
+ * @return ip
|
|
|
+ */
|
|
|
+ private String getRemoteHost(HttpServletRequest request) {
|
|
|
+ String ip = request.getHeader("x-forwarded-for");
|
|
|
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
|
+ ip = request.getHeader("Proxy-Client-IP");
|
|
|
+ }
|
|
|
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
|
+ ip = request.getHeader("WL-Proxy-Client-IP");
|
|
|
+ }
|
|
|
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
|
+ ip = request.getRemoteAddr();
|
|
|
+ }
|
|
|
+ return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
|
|
|
+ }
|
|
|
+}
|