|
@@ -19,19 +19,39 @@
|
|
|
|
|
|
package com.dragon.tj.portal.component.log.aspect;
|
|
|
|
|
|
-import com.dragon.tj.portal.component.log.dto.SysLogDTO;
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.dragon.tj.portal.auth.model.LoginUser;
|
|
|
+import com.dragon.tj.portal.auth.service.TokenService;
|
|
|
import com.dragon.tj.portal.component.log.annotation.SysLog;
|
|
|
+import com.dragon.tj.portal.component.log.dto.SysLogDTO;
|
|
|
import com.dragon.tj.portal.component.log.event.SysLogEvent;
|
|
|
import com.dragon.tj.portal.component.log.uitl.LogTypeEnum;
|
|
|
import com.dragon.tj.portal.component.log.uitl.SysLogUtils;
|
|
|
-import lombok.RequiredArgsConstructor;
|
|
|
import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
import org.aspectj.lang.annotation.Around;
|
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
|
+import org.aspectj.lang.reflect.MethodSignature;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.ApplicationEventPublisher;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.StreamUtils;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.lang.annotation.Annotation;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.SortedMap;
|
|
|
+import java.util.TreeMap;
|
|
|
|
|
|
/**
|
|
|
* @author huey China.
|
|
@@ -41,10 +61,18 @@ import org.springframework.stereotype.Component;
|
|
|
@Component
|
|
|
@Slf4j
|
|
|
@Aspect
|
|
|
-@RequiredArgsConstructor
|
|
|
public class SysLogAspect {
|
|
|
|
|
|
- private final ApplicationEventPublisher publisher;
|
|
|
+ private static final String GET_METHOD = "GET";
|
|
|
+ private static final String POST_METHOD = "POST";
|
|
|
+
|
|
|
+ private static final Class<RequestBody> REQUEST_BODY_CLASS = RequestBody.class;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ApplicationEventPublisher publisher;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TokenService tokenService;
|
|
|
|
|
|
@SneakyThrows
|
|
|
@Around("@annotation(sysLog)")
|
|
@@ -53,8 +81,20 @@ public class SysLogAspect {
|
|
|
String strMethodName = point.getSignature().getName();
|
|
|
log.debug("[类名]:{},[方法]:{}", strClassName, strMethodName);
|
|
|
|
|
|
+ HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
|
|
|
+ LoginUser loginUser = tokenService.getLoginUser(request);
|
|
|
+ String createBy = "admin";
|
|
|
+ String createUser = "admin";
|
|
|
+ if (Objects.nonNull(loginUser)) {
|
|
|
+ log.info("class-method-current-login {}-{}-{}", strClassName, strMethodName, JSON.toJSONString(loginUser));
|
|
|
+ createBy = loginUser.getIdCard();
|
|
|
+ createUser = loginUser.getUsername();
|
|
|
+ }
|
|
|
SysLogDTO logDTO = SysLogUtils.getSysLog();
|
|
|
logDTO.setTitle(sysLog.value());
|
|
|
+ logDTO.setCreateBy(createBy);
|
|
|
+ logDTO.setCreateUser(createUser);
|
|
|
+ logDTO.setParams(this.params(request, point));
|
|
|
// 发送异步日志事件
|
|
|
Long startTime = System.currentTimeMillis();
|
|
|
Object obj;
|
|
@@ -72,4 +112,55 @@ public class SysLogAspect {
|
|
|
return obj;
|
|
|
}
|
|
|
|
|
|
-}
|
|
|
+
|
|
|
+ private String params(HttpServletRequest request1, ProceedingJoinPoint point) throws IOException {
|
|
|
+ // 获取GET请求的参数列表及对应的值
|
|
|
+
|
|
|
+ StringBuilder params = new StringBuilder();
|
|
|
+ Map<String, String[]> getParameters = request1.getParameterMap();
|
|
|
+ for (Map.Entry<String, String[]> entry : getParameters.entrySet()) {
|
|
|
+ String parameterName = entry.getKey();
|
|
|
+ String[] parameterValues = entry.getValue();
|
|
|
+ // 处理GET请求的参数及对应的值
|
|
|
+ params.append("GET-PARAM:" + parameterName + " - " + String.join(", ", parameterValues));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取POST请求的参数列表及对应的值
|
|
|
+ // 获取POST请求中的JSON值
|
|
|
+ if (POST_METHOD.equalsIgnoreCase(request1.getMethod())) {
|
|
|
+ // 获取JSON参数
|
|
|
+ MethodSignature methodSignature = (MethodSignature) point.getSignature();
|
|
|
+
|
|
|
+ int index = getIndexOfRequestBodyParameter(methodSignature);
|
|
|
+ if (index > -1) {
|
|
|
+ Object[] args = point.getArgs();
|
|
|
+ // 若有参数,则参数
|
|
|
+ Object arg = args[index];
|
|
|
+ String s = JSON.toJSONString(arg);
|
|
|
+ if (Objects.nonNull(s)) {
|
|
|
+ params.append("POST-PARAM:"+s);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return params.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取@RequestBody注解参数的索引
|
|
|
+ */
|
|
|
+ private int getIndexOfRequestBodyParameter(final MethodSignature methodSignature) {
|
|
|
+ int index = -1;
|
|
|
+ // 获取参数上的所有注解
|
|
|
+ final Annotation[][] parameterAnnotations = methodSignature.getMethod().getParameterAnnotations();
|
|
|
+ for (int i = 0; i < parameterAnnotations.length; i++) {
|
|
|
+ Annotation[] annotations = parameterAnnotations[i];
|
|
|
+ for (int j = 0; j < annotations.length; j++) {
|
|
|
+ Annotation annotation = annotations[j];
|
|
|
+ if (REQUEST_BODY_CLASS.equals(annotation.annotationType())) {
|
|
|
+ return i;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return index;
|
|
|
+ }
|
|
|
+}
|