Przeglądaj źródła

Merge remote-tracking branch 'origin/matser' into matser

wy 6 miesięcy temu
rodzic
commit
14118d7a05

+ 87 - 8
ibps-basic-root/modules/basic-response/src/main/resources/config/application-common.yml

@@ -38,34 +38,110 @@ app:
       weight: 10
     - host: 192.168.3.240
       weight: 10
+## 端点暴露问题,会导致用户的安全扫描报警,注释掉,勿删,仅供参考
+#management:
+#  endpoints:
+#    shutdown:
+#      enabled: false
+#      sensitive: false
+#    web:
+#      base-path: /
+#      exposure:
+#        include: '*'
+#        exclude: beans
+#  metrics:
+#    tags:
+#      application: ${spring.application.name}
+#    export:
+#      prometheus:
+#        enabled: true
+#        step: 1ms
+#        descriptions: true
+#  endpoint:
+#    health:
+#      enabled: true
+#      show-details: always
+#    env:
+#      enabled: true
+#    prometheus:
+#      enabled: true
+#    mappings:
+#      enabled: false
+#  health:
+#    mail:
+#      enabled: false
+#    redis:
+#      enabled: false
+#    rabbit:
+#      enabled: false
+#    mongo:
+#      enabled: false
+
+
+## 新增内容,关闭端点暴露
 management:
   endpoints:
+    enabled-by-default: false
     shutdown:
       enabled: false
       sensitive: false
     web:
       base-path: /
       exposure:
-        include: '*'
-        exclude: beans
+        include: ""
+        exclude: "*"
   metrics:
     tags:
       application: ${spring.application.name}
     export:
       prometheus:
-        enabled: true
+        enabled: false
         step: 1ms
         descriptions: true
   endpoint:
+    # 显式禁用所有端点
     health:
-      enabled: true
-      show-details: always
+      enabled: false
+    info:
+      enabled: false
     env:
-      enabled: true
-    prometheus:
-      enabled: true
+      enabled: false
+    heapdump:
+      enabled: false
+    configprops:
+      enabled: false
+    beans:
+      enabled: false
     mappings:
       enabled: false
+    trace:
+      enabled: false
+    dump:
+      enabled: false
+    autoconfig:
+      enabled: false
+    metrics:
+      enabled: false
+    loggers:
+      enabled: false
+    auditevents:
+      enabled: false
+    httptrace:
+      enabled: false
+    threaddump:
+      enabled: false
+    conditions:
+      enabled: false
+    flyway:
+      enabled: false
+    liquibase:
+      enabled: false
+    scheduledtasks:
+      enabled: false
+    prometheus:
+      enabled: false
+    shutdown:
+      enabled: false
   health:
     mail:
       enabled: false
@@ -75,6 +151,9 @@ management:
       enabled: false
     mongo:
       enabled: false
+
+
+
 logging:
   config: classpath:config/log4j2.yml
 ##---------邮箱配置---------

+ 216 - 0
ibps-provider-root/modules/provider-business/src/main/java/com/lc/ibps/aop/EquipmentDBLogAspect.java

@@ -0,0 +1,216 @@
+package com.lc.ibps.aop;
+
+import cn.hutool.core.date.StopWatch;
+import cn.hutool.json.JSONArray;
+import cn.hutool.json.JSONUtil;
+import com.lc.ibps.base.core.util.BeanUtils;
+import com.lc.ibps.base.framework.table.ICommonDao;
+import com.lc.ibps.base.web.context.ContextUtil;
+import com.lc.ibps.cloud.utils.RequestUtil;
+import com.lc.ibps.sysdata.dao.UpdateDataTableDao;
+import com.lc.ibps.untils.LogAopUtil;
+import org.apache.commons.lang3.StringUtils;
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.springframework.beans.factory.annotation.Autowired;
+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.util.*;
+
+@Aspect
+@Component
+public class EquipmentDBLogAspect {
+    private static final String TABLE_NAME = "t_business_log";
+    private static final String REMOVE_METHOD = "remove";
+    private static final String SAVE_METHOD = "save";
+    // 创建路径到表名的映射
+    private static final Map<String, String> PATH_TO_TABLE_MAP = new HashMap<>();
+
+    static {
+        // 初始化映射关系
+        PATH_TO_TABLE_MAP.put("equipment/accessoriesDevice", "T_FSSBJPJB");
+        PATH_TO_TABLE_MAP.put("equipment/calibrateResultRecord", "T_JYXTXZJGYZHQRJLB");
+        PATH_TO_TABLE_MAP.put("equipment/calibrationCheckRecord", "T_MJSBJDXZJHZB");
+        PATH_TO_TABLE_MAP.put("equipment/equipmentCard", "T_SBDJ");
+        PATH_TO_TABLE_MAP.put("equipment/maintenanceItem", "T_WHZQJXM");
+        PATH_TO_TABLE_MAP.put("equipment/maintenanceRecord", "T_MJSBWHBYJLBY");
+        PATH_TO_TABLE_MAP.put("equipment/repairRecord", "T_SBWXJLB");
+        PATH_TO_TABLE_MAP.put("equipment/scrappedRecord", "T_SBTYBFJLB");
+    }
+
+    @Resource
+    private ICommonDao commonDao;
+    @Autowired
+    private UpdateDataTableDao updateDataTableDao;
+
+    // 声明环绕通知 定义切点 流程涉及到的接口
+    @Around("execution(* com.lc.ibps.components.equipment.api.*.*(..))")
+    public Object around(ProceedingJoinPoint point) throws Throwable {
+        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
+        String tableName = getTableNameByURL(request.getRequestURI());
+        StopWatch stopWatch = new StopWatch();
+        stopWatch.start();
+
+        Object result = null;
+        String methodName = point.getSignature().getName();
+        Boolean exceptionFlag = false ;
+        String oldData = "";
+        try {
+            // 在方法执行前保存原始数据
+            if (ifAddLog(methodName) && StringUtils.isNotEmpty(tableName)) {//匹配到了
+                oldData = saveOriginalData(point, methodName,tableName);
+            }
+
+            result = point.proceed();
+
+        } catch (Throwable throwable) {
+            exceptionFlag = true;
+            // 发生异常时记录错误日志
+            Map<String, Object> logData = LogAopUtil.logError(request, point, throwable);
+            updateDataTableDao.insertLog(logData);
+            throw throwable;
+
+        } finally {
+            stopWatch.stop();
+            // 在清理前记录正常日志
+            if (!exceptionFlag && ifAddLog(methodName) && StringUtils.isNotEmpty(tableName)) {
+                Map<String, Object> logData = log(request, point, result, stopWatch,oldData,tableName);
+                updateDataTableDao.insertLog(logData);
+            }
+
+        }
+        return result;
+    }
+    private String saveOriginalData(ProceedingJoinPoint point, String methodName,String tableName) {
+        String oldData ="";
+        String columnName ="id_";
+        StringBuilder whereBuilder = new StringBuilder();
+        if (REMOVE_METHOD.equals(methodName)) {//设备删除传入的参数是id的数组
+            String params = LogAopUtil.filterParam(point.getArgs());
+            JSONArray jsonArray = JSONUtil.parseArray(params);
+            //String tableName ="t_sbdj";
+
+            // 处理空数组的情况
+            if (jsonArray == null || jsonArray.size() == 0) {
+                columnName = columnName + " IN ('')"; // 或者返回空字符串或其他默认值
+                whereBuilder.append(columnName);
+            }
+            // 构建 IN 条件
+            whereBuilder.append(columnName).append(" IN (");
+            for (int i = 0; i < jsonArray.size(); i++) {
+                if (i > 0) {
+                    whereBuilder.append(",");
+                }
+                // 添加单引号包裹的值
+                whereBuilder.append("'").append(jsonArray.getStr(i)).append("'");
+            }
+            whereBuilder.append(")");
+            String WhereStr = whereBuilder.toString();
+            String zbWhereStr = WhereStr.replace("id_", "parent_id_");
+            String query = String.format("SELECT * FROM %s WHERE %s", tableName,WhereStr);
+            List<Map<String,Object>> masterList = commonDao.query(query);
+            //获取子表的数据
+            getChildData(tableName, zbWhereStr,masterList);
+            //拼接主子表的数据
+            if(BeanUtils.isNotEmpty(masterList)){
+                // 使用 JSONArray 并设置日期格式防止时间被转换为毫秒
+                JSONArray jsonArrayResult = new JSONArray(masterList);
+                jsonArrayResult.setDateFormat("yyyy-MM-dd HH:mm:ss");
+                oldData = jsonArrayResult.toString();
+            }
+        }
+        if (SAVE_METHOD.equals(methodName)) {
+            String params = LogAopUtil.filterParam(point.getArgs());
+            Map<String,Object> map = JSONUtil.parseObj(params);
+            Object id = map.get("id");
+
+            if(BeanUtils.isNotEmpty(id)){
+                // 构建条件
+                String zbWhereStr = " parent_id_ = '"+id+"'";
+                String query = String.format("SELECT * FROM %s WHERE id_ = '"+id+"' ", tableName);
+                List<Map<String,Object>> masterList = commonDao.query(query);
+                //获取子表的数据
+                getChildData(tableName, zbWhereStr, masterList);
+                if(BeanUtils.isNotEmpty(masterList)){
+                    // 使用 JSONArray 并设置日期格式防止时间被转换为毫秒
+                    JSONArray jsonArrayResult = new JSONArray(masterList);
+                    jsonArrayResult.setDateFormat("yyyy-MM-dd HH:mm:ss");
+                    oldData = jsonArrayResult.toString();
+                }
+            }
+        }
+        return oldData;
+    }
+
+    private void getChildData(String tableName, String whereStr, List<Map<String, Object>> masterList) {
+        if("T_SBDJ".equals(tableName)){
+            //ids.replace("id_","parent_id_");
+            if(BeanUtils.isNotEmpty(masterList)){
+                for (Map<String, Object> map : masterList) {
+                    map.put("table_name", "T_SBDJ");
+                }
+            }
+            String query = String.format("SELECT * FROM T_FSSBJPJB WHERE %s",whereStr);
+            List<Map<String,Object>> list = commonDao.query(query);
+            if(BeanUtils.isNotEmpty(list)){
+                for (Map<String, Object> map : list) {
+                    map.put("table_name", "T_FSSBJPJB");
+                }
+                masterList.addAll(list);
+            }
+            String query2 = String.format("SELECT * FROM T_WHZQJXM WHERE %s",whereStr);
+            List<Map<String,Object>> list2 = commonDao.query(query2);
+            if(BeanUtils.isNotEmpty(list2)){
+                for (Map<String, Object> map : list2) {
+                    map.put("table_name", "T_WHZQJXM");
+                }
+                masterList.addAll(list2);
+            }
+
+        }
+    }
+
+    /**
+     * 判断是否需要添加日志
+     */
+    public boolean ifAddLog(String methodName) {
+        String[] names = {REMOVE_METHOD, SAVE_METHOD};
+        return Arrays.asList(names).contains(methodName);
+    }
+
+
+    public static String getTableNameByURL(String request) {
+        if (request == null || request.trim().isEmpty()) {
+            return "";
+        }
+
+        // 遍历映射表,检查请求地址是否包含关键路径
+        for (Map.Entry<String, String> entry : PATH_TO_TABLE_MAP.entrySet()) {
+            if (request.contains(entry.getKey())) {
+                return entry.getValue();
+            }
+        }
+
+        return ""; // 未找到匹配项
+    }
+    public static Map<String,Object> log(HttpServletRequest request, ProceedingJoinPoint point,Object result, StopWatch stopWatch,String oldData,String tableName) {
+        Map<String,Object> map = new HashMap<>();
+        map.put("method_", point.getSignature().getName());
+        map.put("params_", LogAopUtil.filterParam(point.getArgs()));
+        map.put("user_id_", ContextUtil.getCurrentUser().getUserId());
+        map.put("user_name_", ContextUtil.getCurrentUser().getFullname());
+        map.put("time_", stopWatch.getTotalTimeMillis());
+        map.put("ip_", RequestUtil.getIpAddr(request));
+        map.put("response_", JSONUtil.toJsonStr(result));
+        map.put("uri_", request.getRequestURI());
+        map.put("type_", "正常日志");
+        map.put("data_", oldData);
+        map.put("table_name", tableName);
+        return map;
+    }
+}

+ 2 - 2
ibps-provider-root/modules/provider-business/src/main/resources/com/lc/ibps/klimsibps/mapping/UpdateDataTableMapper.xml

@@ -247,10 +247,10 @@
 
     <!--  添加操作日志   -->
     <insert  id="insertLog"  parameterType="java.util.Map">
-        INSERT INTO t_business_log (method_, params_, user_id_, user_name_, create_time_, time_, ip_, response_, uri_ , type_ , error_)
+        INSERT INTO t_business_log (method_, params_, user_id_, user_name_, create_time_, time_, ip_, response_, uri_ , type_ , error_, data_ , table_name)
          values
         (#{map.method_},#{map.params_},#{map.user_id_},#{map.user_name_},now(),
-         #{map.time_},#{map.ip_},#{map.response_},#{map.uri_},#{map.type_},#{map.error_})
+         #{map.time_},#{map.ip_},#{map.response_},#{map.uri_},#{map.type_},#{map.error_},#{map.data_},#{map.table_name})
     </insert >
     <select id="selectByLocation" resultType="java.util.Map" parameterType="java.util.Map">
         select a.*,