diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/pom.xml b/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/pom.xml
index 869fe7ab..4db62a30 100644
--- a/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/pom.xml
+++ b/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/pom.xml
@@ -26,6 +26,13 @@
org.springframework.boot
spring-boot-starter
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+ provided
+
\ No newline at end of file
diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/annotation/IgnoreParameter.java b/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/annotation/IgnoreParameter.java
new file mode 100644
index 00000000..3d472499
--- /dev/null
+++ b/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/annotation/IgnoreParameter.java
@@ -0,0 +1,17 @@
+package com.orion.ops.framework.biz.operator.log.core.annotation;
+
+import java.lang.annotation.*;
+
+/**
+ * 用于记录操作日志时候 忽略参数
+ *
+ * @author Jiahang Li
+ * @version 1.0.0
+ * @since 2023/10/11 17:45
+ */
+@Target({ElementType.PARAMETER})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface IgnoreParameter {
+
+}
diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/annotation/OperatorLog.java b/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/annotation/OperatorLog.java
index c69443dd..feea2bbe 100644
--- a/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/annotation/OperatorLog.java
+++ b/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/annotation/OperatorLog.java
@@ -21,6 +21,19 @@ public @interface OperatorLog {
*/
String value();
+ /**
+ * 是否记录参数
+ *
+ * - {@link org.springframework.web.bind.annotation.RequestBody}
+ * - {@link org.springframework.web.bind.annotation.RequestParam}
+ * - {@link org.springframework.web.bind.annotation.RequestHeader}
+ * - {@link org.springframework.web.bind.annotation.PathVariable}
+ *
+ * 使用 @IgnoreParameter 可以忽略参数记录 {@link IgnoreParameter}
+ * 如果只需要忽略某个字段可以使用 @Desensitize(toEmpty = true) 标注
+ */
+ boolean parameter() default true;
+
/**
* 返回值处理
*/
diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/aspect/OperatorLogAspect.java b/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/aspect/OperatorLogAspect.java
index 549f0d0d..531c61fb 100644
--- a/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/aspect/OperatorLogAspect.java
+++ b/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/aspect/OperatorLogAspect.java
@@ -4,8 +4,10 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.orion.lang.define.thread.ExecutorBuilder;
import com.orion.lang.define.wrapper.Ref;
+import com.orion.lang.utils.Arrays1;
import com.orion.lang.utils.Strings;
import com.orion.lang.utils.json.matcher.ReplacementFormatters;
+import com.orion.ops.framework.biz.operator.log.core.annotation.IgnoreParameter;
import com.orion.ops.framework.biz.operator.log.core.annotation.OperatorLog;
import com.orion.ops.framework.biz.operator.log.core.config.OperatorLogConfig;
import com.orion.ops.framework.biz.operator.log.core.enums.ReturnType;
@@ -16,6 +18,7 @@ import com.orion.ops.framework.biz.operator.log.core.service.OperatorLogFramewor
import com.orion.ops.framework.biz.operator.log.core.uitls.OperatorLogs;
import com.orion.ops.framework.common.enums.BooleanBit;
import com.orion.ops.framework.common.meta.TraceIdHolder;
+import com.orion.ops.framework.common.security.LoginUser;
import com.orion.ops.framework.common.security.SecurityHolder;
import com.orion.ops.framework.common.utils.IpUtils;
import com.orion.web.servlet.web.Servlets;
@@ -23,14 +26,19 @@ 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.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestHeader;
+import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
+import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
-import java.util.Date;
-import java.util.Map;
-import java.util.Objects;
-import java.util.Optional;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+import java.util.*;
import java.util.concurrent.ExecutorService;
/**
@@ -71,9 +79,9 @@ public class OperatorLogAspect {
@Around("@annotation(o)")
public Object around(ProceedingJoinPoint joinPoint, OperatorLog o) throws Throwable {
long start = System.currentTimeMillis();
- // 先清空上下文
- OperatorLogs.clear();
try {
+ // 初始化参数
+ this.initExtra(joinPoint, o);
// 执行
Object result = joinPoint.proceed();
// 记录日志
@@ -89,6 +97,71 @@ public class OperatorLogAspect {
}
}
+ /**
+ * 初始化参数
+ *
+ * @param joinPoint joinPoint
+ */
+ private void initExtra(ProceedingJoinPoint joinPoint, OperatorLog o) {
+ // 清空上下文
+ OperatorLogs.clear();
+ if (!o.parameter()) {
+ return;
+ }
+ // 获取方法注解
+ Annotation[][] methodAnnotations = Optional.ofNullable(joinPoint.getSignature())
+ .filter(s -> (s instanceof MethodSignature))
+ .map(s -> ((MethodSignature) s).getMethod())
+ .map(Method::getParameterAnnotations)
+ .orElse(null);
+ if (Arrays1.isEmpty(methodAnnotations)) {
+ return;
+ }
+ // 获取参数
+ Object[] args = joinPoint.getArgs();
+ for (int i = 0; i < methodAnnotations.length; i++) {
+ Annotation[] annotations = methodAnnotations[i];
+ if (Arrays1.isEmpty(annotations)) {
+ continue;
+ }
+ // 检查是否有 IgnoreParameter 注解
+ boolean ignore = Arrays.stream(annotations)
+ .anyMatch(s -> s instanceof IgnoreParameter);
+ if (ignore) {
+ continue;
+ }
+ // 获取需要记录的参数
+ for (Annotation annotation : annotations) {
+ if (annotation instanceof RequestBody) {
+ OperatorLogs.add(args[i]);
+ break;
+ } else if (annotation instanceof RequestParam) {
+ Object arg = args[i];
+ if (arg instanceof MultipartFile) {
+ break;
+ }
+ OperatorLogs.add(
+ orElse(((RequestParam) annotation).value(),
+ ((RequestParam) annotation).name()),
+ args[i]);
+ break;
+ } else if (annotation instanceof RequestHeader) {
+ OperatorLogs.add(
+ orElse(((RequestHeader) annotation).value(),
+ ((RequestHeader) annotation).name()),
+ args[i]);
+ break;
+ } else if (annotation instanceof PathVariable) {
+ OperatorLogs.add(
+ orElse(((PathVariable) annotation).value(),
+ ((PathVariable) annotation).name()),
+ args[i]);
+ break;
+ }
+ }
+ }
+ }
+
/**
* 保存日志
*
@@ -97,7 +170,17 @@ public class OperatorLogAspect {
*/
private void saveLog(long start, OperatorLog o, Object ret, Throwable exception) {
try {
- // 请求信息
+ // 获取日志类型
+ OperatorType type = OperatorTypeHolder.get(o.value());
+ if (type == null) {
+ return;
+ }
+ // 获取当前用户
+ LoginUser user = this.getUser();
+ if (user == null) {
+ return;
+ }
+ // 获取请求信息
Map extra = OperatorLogs.get();
if (!OperatorLogs.isSave(extra)) {
return;
@@ -106,7 +189,7 @@ public class OperatorLogAspect {
// 填充使用时间
this.fillUseTime(model, start);
// 填充用户信息
- this.fillUserInfo(model);
+ this.fillUserInfo(model, user);
// 填充请求信息
this.fillRequest(model);
// 填充结果信息
@@ -114,7 +197,7 @@ public class OperatorLogAspect {
// 填充拓展信息
this.fillExtra(model, extra);
// 填充日志
- this.fillLogInfo(model, extra, o);
+ this.fillLogInfo(model, extra, type);
// 插入日志
this.asyncSaveLog(model);
} catch (Exception e) {
@@ -122,6 +205,20 @@ public class OperatorLogAspect {
}
}
+ /**
+ * 获取当前用户
+ *
+ * @return user
+ */
+ private LoginUser getUser() {
+ LoginUser user = OperatorLogs.getUser();
+ if (user != null) {
+ return user;
+ }
+ // 登录上下文获取
+ return securityHolder.getLoginUser();
+ }
+
/**
* 填充使用时间
*
@@ -139,9 +236,11 @@ public class OperatorLogAspect {
* 填充用户信息
*
* @param model model
+ * @param user user
*/
- private void fillUserInfo(OperatorLogModel model) {
- model.setUserId(securityHolder.getLoginUserId());
+ private void fillUserInfo(OperatorLogModel model, LoginUser user) {
+ model.setUserId(user.getId());
+ model.setUsername(user.getUsername());
}
/**
@@ -205,10 +304,9 @@ public class OperatorLogAspect {
*
* @param model model
* @param extra extra
- * @param o o
+ * @param type type
*/
- private void fillLogInfo(OperatorLogModel model, Map extra, OperatorLog o) {
- OperatorType type = OperatorTypeHolder.get(o.value());
+ private void fillLogInfo(OperatorLogModel model, Map extra, OperatorType type) {
model.setModule(type.getModule());
model.setType(type.getType());
model.setLogInfo(ReplacementFormatters.format(type.getTemplate(), extra));
@@ -220,9 +318,18 @@ public class OperatorLogAspect {
* @param model model
*/
private void asyncSaveLog(OperatorLogModel model) {
- LOG_SAVER.submit(() -> {
- operatorLogFrameworkService.insert(model);
- });
+ LOG_SAVER.submit(() -> operatorLogFrameworkService.insert(model));
+ }
+
+ /**
+ * 获取可用值
+ *
+ * @param value value
+ * @param name name
+ * @return available
+ */
+ private String orElse(String value, String name) {
+ return Strings.isBlank(value) ? name : value;
}
}
diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/constant/OperatorLogKeys.java b/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/constant/OperatorLogKeys.java
index 3ef35011..3d8999de 100644
--- a/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/constant/OperatorLogKeys.java
+++ b/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/constant/OperatorLogKeys.java
@@ -17,6 +17,10 @@ public interface OperatorLogKeys {
String NAME = "name";
+ String STATUS = "status";
+
+ String STATUS_NAME = "statusName";
+
String USERNAME = "username";
String TITLE = "title";
diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/model/OperatorLogModel.java b/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/model/OperatorLogModel.java
index 55d1b8c6..76cbe3e0 100644
--- a/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/model/OperatorLogModel.java
+++ b/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/model/OperatorLogModel.java
@@ -19,6 +19,11 @@ public class OperatorLogModel {
*/
private Long userId;
+ /**
+ * 用户名
+ */
+ private String username;
+
/**
* traceId
*/
diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/uitls/OperatorLogs.java b/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/uitls/OperatorLogs.java
index b0edf33d..bd3e615e 100644
--- a/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/uitls/OperatorLogs.java
+++ b/orion-ops-framework/orion-ops-spring-boot-starter-biz-operator-log/src/main/java/com/orion/ops/framework/biz/operator/log/core/uitls/OperatorLogs.java
@@ -3,6 +3,7 @@ package com.orion.ops.framework.biz.operator.log.core.uitls;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.orion.ops.framework.biz.operator.log.core.constant.OperatorLogKeys;
+import com.orion.ops.framework.common.security.LoginUser;
import java.util.HashMap;
import java.util.Map;
@@ -20,8 +21,16 @@ public class OperatorLogs implements OperatorLogKeys {
private static ValueFilter desensitizeValueFilter;
+ /**
+ * 拓展信息
+ */
private static final ThreadLocal