Kaynağa Gözat

操作日志加入审计策略配置过滤

xusl 1 yıl önce
ebeveyn
işleme
0f87692ab9

+ 55 - 27
backend/src/main/java/com/jiayue/ssi/aspectj/OperateLogAspect.java

@@ -2,13 +2,17 @@ package com.jiayue.ssi.aspectj;
 
 import cn.hutool.core.util.DesensitizedUtil;
 import cn.hutool.json.JSONUtil;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.jiayue.ssi.annotation.OperateLog;
+import com.jiayue.ssi.backenum.AuditAblesEventEnum;
 import com.jiayue.ssi.backenum.BusinessStatus;
 import com.jiayue.ssi.backenum.HttpMethod;
 import com.jiayue.ssi.entity.SysOperLog;
+import com.jiayue.ssi.entity.SysPolicy;
 import com.jiayue.ssi.entity.SysUser;
 import com.jiayue.ssi.factory.OperateLogFactory;
 import com.jiayue.ssi.filter.PropertyPreExcludeFilter;
+import com.jiayue.ssi.service.SysPolicyService;
 import com.jiayue.ssi.util.IPUtils;
 import com.jiayue.ssi.util.RyStringUtils;
 import com.jiayue.ssi.util.SecurityContextUtil;
@@ -22,6 +26,7 @@ import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Before;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.NamedThreadLocal;
 import org.springframework.core.annotation.Order;
 import org.springframework.stereotype.Component;
@@ -58,6 +63,9 @@ public class OperateLogAspect {
      */
     private static final ThreadLocal<Long> TIME_THREADLOCAL = new NamedThreadLocal<Long>("Cost Time");
 
+    @Autowired
+    SysPolicyService sysPolicyService;
+
     /**
      * 处理请求前执行
      */
@@ -89,37 +97,57 @@ public class OperateLogAspect {
 
     protected void handleLog(final JoinPoint joinPoint, OperateLog controllerLog, final Exception e, Object jsonResult) {
         try {
-            // 获取当前的用户
-            SysUser sysUser = SecurityContextUtil.getSysUser();
+            String visitUrl = StringUtils.substring(ServletUtils.getRequest().getRequestURI(), 0, 255);
+            // 对审计策略配置过滤
+            SysPolicy sysPolicy = sysPolicyService.getOne(new QueryWrapper<>());
+            String auditableEvent = sysPolicy.getAuditableEvent();
 
-            // *========数据库日志=========*//
-            SysOperLog operLog = new SysOperLog();
-            operLog.setStatus(BusinessStatus.SUCCESS.ordinal());
-            // 请求的地址
-            String ip = IPUtils.getIpAddr();
-            operLog.setOperIp(ip);
-            operLog.setOperUrl(StringUtils.substring(ServletUtils.getRequest().getRequestURI(), 0, 255));
-            if (sysUser != null) {
-                operLog.setOperName(sysUser.getUsername());
+            boolean executeAudit = true;
+            if (auditableEvent!=null){
+                String[] auditables = auditableEvent.split(",");
+                for (int i=0;i<auditables.length;i++){
+                    // 0-NWP;1-DQ
+                    if (visitUrl.equals(AuditAblesEventEnum.getByCode(Integer.parseInt(auditables[i])).getMessage())){
+                        // 遇到短期或者nwp不加入审计
+                        executeAudit = false;
+                        break;
+                    }
+                }
             }
 
-            if (e != null) {
-                operLog.setStatus(BusinessStatus.FAIL.ordinal());
-                operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000));
+            if (executeAudit){
+                // 获取当前的用户
+                SysUser sysUser = SecurityContextUtil.getSysUser();
+
+                // *========数据库日志=========*//
+                SysOperLog operLog = new SysOperLog();
+                operLog.setStatus(BusinessStatus.SUCCESS.ordinal());
+                // 请求的地址
+                String ip = IPUtils.getIpAddr();
+                operLog.setOperIp(ip);
+                operLog.setOperUrl(visitUrl);
+                if (sysUser != null) {
+                    operLog.setOperName(sysUser.getUsername());
+                }
+
+                if (e != null) {
+                    operLog.setStatus(BusinessStatus.FAIL.ordinal());
+                    operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000));
+                }
+                // 设置方法名称
+                String className = joinPoint.getTarget().getClass().getName();
+                String methodName = joinPoint.getSignature().getName();
+                operLog.setMethod(className + "." + methodName + "()");
+                // 设置请求方式
+                operLog.setRequestMethod(ServletUtils.getRequest().getMethod());
+                // 处理设置注解上的参数
+                getControllerMethodDescription(joinPoint, controllerLog, operLog, jsonResult);
+                // 设置消耗时间
+                operLog.setCostTime(System.currentTimeMillis() - TIME_THREADLOCAL.get());
+                operLog.setCreateBy(sysUser.getUsername());
+                // 保存数据库
+                OperateLogFactory.recordOper(operLog);
             }
-            // 设置方法名称
-            String className = joinPoint.getTarget().getClass().getName();
-            String methodName = joinPoint.getSignature().getName();
-            operLog.setMethod(className + "." + methodName + "()");
-            // 设置请求方式
-            operLog.setRequestMethod(ServletUtils.getRequest().getMethod());
-            // 处理设置注解上的参数
-            getControllerMethodDescription(joinPoint, controllerLog, operLog, jsonResult);
-            // 设置消耗时间
-            operLog.setCostTime(System.currentTimeMillis() - TIME_THREADLOCAL.get());
-            operLog.setCreateBy(sysUser.getUsername());
-            // 保存数据库
-            OperateLogFactory.recordOper(operLog);
         } catch (Exception exp) {
             // 记录本地异常日志
             log.error("异常信息:{}", exp.getMessage());

+ 48 - 0
backend/src/main/java/com/jiayue/ssi/backenum/AuditAblesEventEnum.java

@@ -0,0 +1,48 @@
+package com.jiayue.ssi.backenum;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+/**
+* 审计策略配置枚举
+*
+* @author xsl
+* @since 2023/08/18
+*/
+@Getter
+public enum AuditAblesEventEnum {
+    /**
+     * nwp
+     */
+    NWP(0, "/nwpController/getAll"),
+    /**
+     * 短期
+     */
+    DQ(1, "/forecastPowerShortTermController/getAll");
+
+    private Integer code;
+    private String message;
+
+    AuditAblesEventEnum(Integer code, String msg) {
+        this.code = code;
+        this.message = msg;
+    }
+
+    public Integer getCode() {
+        return code;
+    }
+
+
+    public String getMsg() {
+        return message;
+    }
+
+    public static AuditAblesEventEnum getByCode(Integer code) {
+        for (AuditAblesEventEnum type : values()) {
+            if (type.getCode().intValue()==code.intValue()) {
+                return type;
+            }
+        }
+        return null;
+    }
+}