From 3fd15bc3818a16c20ca48f1a98a41e2bb456bdb7 Mon Sep 17 00:00:00 2001
From: liuxm <liuxm@fescotech.com>
Date: 星期三, 24 四月 2024 15:41:00 +0800
Subject: [PATCH] 日志管理--添加字段;登录验证码验证后删除

---
 /dev/null                                                                |   35 -----------
 pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/pojoBa/BaLog.java   |   16 +++++
 pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/aop/LogAspect.java  |   63 +++++++++++++-------
 pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/aop/LogService.java |    4 +
 pms-parent/pms-web-sso/src/main/java/com/dy/sso/busi/SsoCtrl.java        |    2 
 pms-parent/pms-global/src/main/resources/mapper/BaLogMapper.xml          |   24 +++++--
 6 files changed, 78 insertions(+), 66 deletions(-)

diff --git a/pms-parent/pms-common/src/main/java/com/dy/common/util/ObjectToMapUtil.java b/pms-parent/pms-common/src/main/java/com/dy/common/util/ObjectToMapUtil.java
deleted file mode 100644
index 8165e98..0000000
--- a/pms-parent/pms-common/src/main/java/com/dy/common/util/ObjectToMapUtil.java
+++ /dev/null
@@ -1,35 +0,0 @@
-package com.dy.common.util;
-
-import java.lang.reflect.Field;
-import java.util.HashMap;
-import java.util.Map;
-
-public class ObjectToMapUtil {
-
-    /**
-     * 灏嗗璞¤浆鎹负Map銆�
-     *
-     * @param obj 瑕佽浆鎹㈢殑瀵硅薄銆�
-     * @return 鍖呭惈瀵硅薄瀛楁鍜屽�肩殑Map銆�
-     */
-    public static Map<String, Object> objectToMap(Object obj) {
-        if (obj == null) {
-            return null;
-        }
-
-        Map<String, Object> map = new HashMap<>();
-        Class<?> clazz = obj.getClass();
-        Field[] fields = clazz.getDeclaredFields();
-
-        for (Field field : fields) {
-            try {
-                field.setAccessible(true); // 璁剧疆瀛楁鍙闂�
-                map.put(field.getName(), field.get(obj));
-            } catch (IllegalAccessException e) {
-                throw new RuntimeException("Error accessing field: " + field.getName(), e);
-            }
-        }
-
-        return map;
-    }
-}
diff --git a/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/aop/LogAspect.java b/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/aop/LogAspect.java
index db1e48e..1086ddc 100644
--- a/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/aop/LogAspect.java
+++ b/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/aop/LogAspect.java
@@ -1,12 +1,14 @@
 package com.dy.pmsGlobal.aop;
 
-import com.dy.common.util.ObjectToMapUtil;
+import com.alibaba.fastjson2.JSONObject;
 import com.dy.common.webFilter.UserTokenContext;
 import com.dy.common.webUtil.BaseResponse;
 import com.dy.common.webUtil.BaseResponseUtils;
 import com.dy.pmsGlobal.pojoBa.BaUser;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.mysql.cj.util.StringUtils;
 import jakarta.servlet.http.HttpServletRequest;
+import lombok.extern.slf4j.Slf4j;
 import org.aspectj.lang.JoinPoint;
 import org.aspectj.lang.annotation.AfterReturning;
 import org.aspectj.lang.annotation.Aspect;
@@ -23,8 +25,7 @@
 import org.springframework.web.context.request.ServletRequestAttributes;
 import org.springframework.web.util.UriComponentsBuilder;
 
-import java.util.Map;
-
+@Slf4j
 @Aspect
 @Component
 public class LogAspect {
@@ -44,24 +45,24 @@
     }
 
     @AfterReturning(pointcut = "@annotation(com.dy.pmsGlobal.aop.Log)", returning = "result")
-    public void logAfterReturning(JoinPoint joinPoint, Object result) {
-        //缁撴灉
-        BaseResponse res = (BaseResponse)result;
-        res.isSuccess();
-        // 鑾峰彇鐢ㄦ埛淇℃伅
-        BaUser user = (BaUser)getCurUser(res);
-        if(user!=null){
-            Long operator = user.id;
-            // 鑾峰彇鏂规硶鐨勪腑鏂囨弿杩�
-            MethodSignature sign = (MethodSignature) joinPoint.getSignature();
-            Log logDesc = sign.getMethod().getAnnotation(Log.class);
-            String operationName = logDesc.value();
-            // 鑾峰彇IP鍦板潃
-            String ip = getRemoteHost();
-            // 璁板綍鏃ュ織
-            logSv.save(operator, operationName,ip);
+    public void logAfterReturning(JoinPoint joinPoint, BaseResponse result) {
+        try{
+            // 鑾峰彇鐢ㄦ埛淇℃伅
+            BaUser user = (BaUser)getCurUser(result);
+            if(user!=null && user.id !=null && !StringUtils.isNullOrEmpty(user.name)){
+                Long operator = user.id;
+                // 鑾峰彇鏂规硶鐨勪腑鏂囨弿杩�
+                MethodSignature sign = (MethodSignature) joinPoint.getSignature();
+                Log logDesc = sign.getMethod().getAnnotation(Log.class);
+                String operationName = logDesc.value();
+                // 鑾峰彇IP鍦板潃
+                String ip = getRemoteHost();
+                // 璁板綍鏃ュ織
+                logSv.save(operator, operationName,ip,result.getCode(),result.getMsg());
+            }
+        }catch (Exception e){
+            log.error("璁板綍鏃ュ織寮傚父:"+e.getMessage());
         }
-
     }
 
     /**
@@ -73,8 +74,10 @@
         if(!StringUtils.isNullOrEmpty(SsoCurUserUrl)){
             String token = UserTokenContext.get();
             if(StringUtils.isNullOrEmpty(token)){
-                Map<String,Object> resMap = ObjectToMapUtil.objectToMap(response.getContent());
-                token =resMap.containsKey("token")?resMap.get("token").toString():"";
+                JSONObject res = objectToJson(response.getContent());
+                if(res!=null && res.containsKey("token")){
+                    token = res.get("token").toString();
+                }
             }
 
             String url = UriComponentsBuilder.fromUriString(SsoCurUserUrl)
@@ -116,4 +119,20 @@
         }
         return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
     }
+
+    /**
+     * 灏嗗璞¤浆鎹负JSONObject
+     * @param obj
+     * @return
+     */
+    public static JSONObject objectToJson(Object obj) {
+        ObjectMapper mapper = new ObjectMapper();
+        try {
+            JSONObject o = JSONObject.parseObject(mapper.writeValueAsString(obj));
+            return o;
+        } catch (Exception e) {
+            log.error("瀵硅薄杞崲涓篔SONObject澶辫触:"+e.getMessage());
+            return null;
+        }
+    }
 }
diff --git a/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/aop/LogService.java b/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/aop/LogService.java
index 966ae15..5ca8a39 100644
--- a/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/aop/LogService.java
+++ b/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/aop/LogService.java
@@ -13,12 +13,14 @@
     @Autowired
     private BaLogMapper dao;
 
-    public void save(long operator, String operation,String ip) {
+    public void save(long operator, String operation,String ip,String code,String msg) {
         BaLog log = new BaLog();
         log.userId=operator;
         log.content = operation;
         log.dt = new Date();
         log.ip = ip;
+        log.code = code;
+        log.msg = msg;
         dao.insert(log);
     }
 
diff --git a/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/pojoBa/BaLog.java b/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/pojoBa/BaLog.java
index 213d9cb..4ab5877 100644
--- a/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/pojoBa/BaLog.java
+++ b/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/pojoBa/BaLog.java
@@ -1,5 +1,9 @@
 package com.dy.pmsGlobal.pojoBa;
 
+import com.alibaba.fastjson2.annotation.JSONField;
+import com.alibaba.fastjson2.writer.ObjectWriterImplToString;
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
 import lombok.*;
 
@@ -12,6 +16,9 @@
 @NoArgsConstructor
 @AllArgsConstructor
 public class BaLog {
+
+    @JSONField(serializeUsing= ObjectWriterImplToString.class)
+    @TableId(value = "id", type = IdType.INPUT)
     public Long id;
 
     /**
@@ -30,6 +37,15 @@
     public String ip;
 
     /**
+     * 鍝嶅簲鐘舵�佺爜
+     */
+    public String code;
+    /**
+     * 鍝嶅簲淇℃伅
+     */
+    public String msg;
+
+    /**
     * 鍒涘缓鏃堕棿
     */
     public Date dt;
diff --git a/pms-parent/pms-global/src/main/resources/mapper/BaLogMapper.xml b/pms-parent/pms-global/src/main/resources/mapper/BaLogMapper.xml
index f02960f..7a2bcf7 100644
--- a/pms-parent/pms-global/src/main/resources/mapper/BaLogMapper.xml
+++ b/pms-parent/pms-global/src/main/resources/mapper/BaLogMapper.xml
@@ -8,11 +8,13 @@
     <result column="user_id" jdbcType="BIGINT" property="userId" />
     <result column="content" jdbcType="VARCHAR" property="content" />
     <result column="ip" jdbcType="VARCHAR" property="ip" />
+    <result column="code" jdbcType="VARCHAR" property="code" />
+    <result column="msg" jdbcType="VARCHAR" property="msg" />
     <result column="dt" jdbcType="TIMESTAMP" property="dt" />
   </resultMap>
   <sql id="Base_Column_List">
     <!--@mbg.generated-->
-    id, user_id, content, dt,ip
+    id, user_id, content, dt,ip,code,msg
   </sql>
   <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
     <!--@mbg.generated-->
@@ -23,10 +25,10 @@
   </select>
   <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.dy.pmsGlobal.pojoBa.BaLog" useGeneratedKeys="true">
     <!--@mbg.generated-->
-    insert into ba_log (user_id, content, dt,ip
+    insert into ba_log (user_id, content, ip,code,msg
       )
-    values (#{userId,jdbcType=BIGINT}, #{content,jdbcType=VARCHAR}, #{dt,jdbcType=TIMESTAMP}
-    , #{ip,jdbcType=VARCHAR}
+    values (#{userId,jdbcType=BIGINT}, #{content,jdbcType=VARCHAR}, #{ip,jdbcType=VARCHAR},
+    #{code,jdbcType=VARCHAR},#{msg,jdbcType=VARCHAR}
       )
   </insert>
   <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.dy.pmsGlobal.pojoBa.BaLog" useGeneratedKeys="true">
@@ -39,8 +41,11 @@
       <if test="content != null">
         content,
       </if>
-      <if test="dt != null">
-        dt,
+      <if test="code != null">
+        code,
+      </if>
+      <if test="msg != null">
+        msg,
       </if>
       <if test="ip != null">
         ip,
@@ -53,8 +58,11 @@
       <if test="content != null">
         #{content,jdbcType=VARCHAR},
       </if>
-      <if test="dt != null">
-        #{dt,jdbcType=TIMESTAMP},
+      <if test="code != null">
+        #{code,jdbcType=VARCHAR},
+      </if>
+      <if test="msg != null">
+        #{msg,jdbcType=VARCHAR},
       </if>
       <if test="ip != null">
         #{ip,jdbcType=VARCHAR},
diff --git a/pms-parent/pms-web-sso/src/main/java/com/dy/sso/busi/SsoCtrl.java b/pms-parent/pms-web-sso/src/main/java/com/dy/sso/busi/SsoCtrl.java
index c3ee57c..35026d1 100644
--- a/pms-parent/pms-web-sso/src/main/java/com/dy/sso/busi/SsoCtrl.java
+++ b/pms-parent/pms-web-sso/src/main/java/com/dy/sso/busi/SsoCtrl.java
@@ -66,6 +66,7 @@
             String sessionCaptcha = (String) session.getAttribute("captcha");
             // 棣栧厛楠岃瘉楠岃瘉鐮�
             if (vo.captcha != null && vo.captcha.equalsIgnoreCase(sessionCaptcha)) {
+                session.removeAttribute("captcha");
                 return this.doLogin(vo) ;
             } else {
                 // 楠岃瘉鐮侀敊璇紝杩斿洖鐧诲綍椤甸潰骞舵樉绀洪敊璇俊鎭�
@@ -93,6 +94,7 @@
             String sessionCaptcha = (String) session.getAttribute("captcha");
             // 棣栧厛楠岃瘉楠岃瘉鐮�
             if (vo.captcha != null && vo.captcha.equalsIgnoreCase(sessionCaptcha)) {
+                session.removeAttribute("captcha");
                 return this.doLogin(vo) ;
             } else {
                 // 楠岃瘉鐮侀敊璇紝杩斿洖鐧诲綍椤甸潰骞舵樉绀洪敊璇俊鎭�

--
Gitblit v1.8.0