|  |  | 
 |  |  | 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; | 
 |  |  | 
 |  |  | import org.springframework.web.context.request.ServletRequestAttributes; | 
 |  |  | import org.springframework.web.util.UriComponentsBuilder; | 
 |  |  |  | 
 |  |  | import java.util.Map; | 
 |  |  |  | 
 |  |  | @Slf4j | 
 |  |  | @Aspect | 
 |  |  | @Component | 
 |  |  | public class LogAspect { | 
 |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @AfterReturning(pointcut = "@annotation(com.dy.pmsGlobal.aop.Log)", returning = "result") | 
 |  |  |     public void logAfterReturning(JoinPoint joinPoint, Object result) { | 
 |  |  |         //结果 | 
 |  |  |         BaseResponse res = (BaseResponse)result; | 
 |  |  |         res.isSuccess(); | 
 |  |  |     public void logAfterReturning(JoinPoint joinPoint, BaseResponse result) { | 
 |  |  |         try{ | 
 |  |  |         // 获取用户信息 | 
 |  |  |         BaUser user = (BaUser)getCurUser(res); | 
 |  |  |         if(user!=null){ | 
 |  |  |             BaUser user = (BaUser)getCurUser(result); | 
 |  |  |             if(user!=null && user.id !=null && !StringUtils.isNullOrEmpty(user.name)){ | 
 |  |  |             Long operator = user.id; | 
 |  |  |             // 获取方法的中文描述 | 
 |  |  |             MethodSignature sign = (MethodSignature) joinPoint.getSignature(); | 
 |  |  | 
 |  |  |             // 获取IP地址 | 
 |  |  |             String ip = getRemoteHost(); | 
 |  |  |             // 记录日志 | 
 |  |  |             logSv.save(operator, operationName,ip); | 
 |  |  |                 logSv.save(operator, operationName,ip,result.getCode(),result.getMsg()); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         }catch (Exception e){ | 
 |  |  |             log.error("记录日志异常:"+e.getMessage()); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  | 
 |  |  |         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) | 
 |  |  | 
 |  |  |         } | 
 |  |  |         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("对象转换为JSONObject失败:"+e.getMessage()); | 
 |  |  |             return null; | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  | } | 
 
 |  |  | 
 |  |  |     @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); | 
 |  |  |     } | 
 |  |  |  | 
 
 |  |  | 
 |  |  | 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.*; | 
 |  |  |  | 
 |  |  | 
 |  |  | @NoArgsConstructor | 
 |  |  | @AllArgsConstructor | 
 |  |  | public class BaLog { | 
 |  |  |  | 
 |  |  |     @JSONField(serializeUsing= ObjectWriterImplToString.class) | 
 |  |  |     @TableId(value = "id", type = IdType.INPUT) | 
 |  |  |     public Long id; | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  | 
 |  |  |     public String ip; | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 响应状态码 | 
 |  |  |      */ | 
 |  |  |     public String code; | 
 |  |  |     /** | 
 |  |  |      * 响应信息 | 
 |  |  |      */ | 
 |  |  |     public String msg; | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |     * 创建时间 | 
 |  |  |     */ | 
 |  |  |     public Date dt; | 
 
 |  |  | 
 |  |  |     <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--> | 
 |  |  | 
 |  |  |   </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"> | 
 |  |  | 
 |  |  |       <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, | 
 |  |  | 
 |  |  |       <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}, | 
 
 |  |  | 
 |  |  |             String sessionCaptcha = (String) session.getAttribute("captcha"); | 
 |  |  |             // 首先验证验证码 | 
 |  |  |             if (vo.captcha != null && vo.captcha.equalsIgnoreCase(sessionCaptcha)) { | 
 |  |  |                 session.removeAttribute("captcha"); | 
 |  |  |                 return this.doLogin(vo) ; | 
 |  |  |             } else { | 
 |  |  |                 // 验证码错误,返回登录页面并显示错误信息 | 
 |  |  | 
 |  |  |             String sessionCaptcha = (String) session.getAttribute("captcha"); | 
 |  |  |             // 首先验证验证码 | 
 |  |  |             if (vo.captcha != null && vo.captcha.equalsIgnoreCase(sessionCaptcha)) { | 
 |  |  |                 session.removeAttribute("captcha"); | 
 |  |  |                 return this.doLogin(vo) ; | 
 |  |  |             } else { | 
 |  |  |                 // 验证码错误,返回登录页面并显示错误信息 |