| package com.dy.pmsGlobal.aop; | 
|   | 
| 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.aspectj.lang.reflect.MethodSignature; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.beans.factory.annotation.Value; | 
| import org.springframework.http.HttpEntity; | 
| import org.springframework.http.HttpHeaders; | 
| import org.springframework.http.HttpMethod; | 
| import org.springframework.http.ResponseEntity; | 
| import org.springframework.stereotype.Component; | 
| import org.springframework.web.client.RestTemplate; | 
| import org.springframework.web.context.request.RequestContextHolder; | 
| import org.springframework.web.context.request.ServletRequestAttributes; | 
| import org.springframework.web.util.UriComponentsBuilder; | 
|   | 
| @Slf4j | 
| @Aspect | 
| @Component | 
| public class LogAspect { | 
|   | 
|     @Value("${pms.sso.curUserUrl}") | 
|     public String SsoCurUserUrl ; | 
|   | 
|     private LogService logSv; | 
|     @Value("${pms.global.dev}") | 
|     public String isDevStage ;//是否为开发阶段 | 
|     @Autowired | 
|     public void setLogSv(LogService logSv){ | 
|         this.logSv = logSv; | 
|     } | 
|     private RestTemplate restTemplate; | 
|     @Autowired | 
|     public void setRestTemplate(RestTemplate restTemplate){ | 
|         this.restTemplate = restTemplate ; | 
|     } | 
|   | 
|     @AfterReturning(pointcut = "@annotation(com.dy.pmsGlobal.aop.Log)", returning = "result") | 
|     public void logAfterReturning(JoinPoint joinPoint, BaseResponse result) { | 
|         if(isDevStage != null && !isDevStage.trim().equals("") && isDevStage.trim().equalsIgnoreCase("true")){ | 
|             return; | 
|         } | 
|         try{ | 
|             // 获取用户信息 | 
|             BaUser user = (BaUser)getCurUser(result); | 
|             if(user!=null && user.id !=null && !StringUtils.isNullOrEmpty(user.name)){ | 
|                 // 获取方法的中文描述 | 
|                 MethodSignature sign = (MethodSignature) joinPoint.getSignature(); | 
|                 Log logDesc = sign.getMethod().getAnnotation(Log.class); | 
|                 String operationName = logDesc.value(); | 
|                 // 获取IP地址 | 
|                 String ip = getRemoteHost(); | 
|                 // 记录日志 | 
|                 logSv.save(user.id,user.name, operationName,ip,result.getCode(),result.getMsg()); | 
|             } | 
|         }catch (Exception e){ | 
|             log.error("记录日志异常:"+e.getMessage()); | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 调用SSO获取用户信息 | 
|      * @param response (获取token) | 
|      * @return 返回对象 | 
|      */ | 
|     private Object getCurUser(BaseResponse response){ | 
|         if(!StringUtils.isNullOrEmpty(SsoCurUserUrl)){ | 
|             String token = UserTokenContext.get(); | 
|             if(StringUtils.isNullOrEmpty(token)){ | 
|                 JSONObject res = objectToJson(response.getContent()); | 
|                 if(res!=null && res.containsKey("token")){ | 
|                     token = res.get("token").toString(); | 
|                 } | 
|             } | 
|   | 
|             String url = UriComponentsBuilder.fromUriString(SsoCurUserUrl) | 
|                     .queryParam("token", token) | 
|                     .build() | 
|                     .toUriString(); | 
|             HttpHeaders headers = new HttpHeaders(); | 
|             HttpEntity<?> httpEntity = new HttpEntity<>(headers); | 
|             ResponseEntity<BaUser> myResponse = null; | 
|             try { | 
|                 // 通过Get方式调用接口 | 
|                 myResponse = restTemplate.exchange(url, HttpMethod.GET, httpEntity, BaUser.class); | 
|             } catch (Exception e) { | 
|                 e.printStackTrace(); | 
|             } | 
|             assert myResponse != null; | 
|             return myResponse.getBody(); | 
|         }else { | 
|             return BaseResponseUtils.buildError("后端系统出错,未得到SsoCurUserUrl"); | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 获取IP地址 | 
|      * @return | 
|      */ | 
|     private String getRemoteHost() { | 
|         // 获取请求对象 | 
|         HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | 
|         String ip = request.getHeader("x-forwarded-for"); | 
|         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | 
|             ip = request.getHeader("Proxy-Client-IP"); | 
|         } | 
|         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | 
|             ip = request.getHeader("WL-Proxy-Client-IP"); | 
|         } | 
|         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | 
|             ip = request.getRemoteAddr(); | 
|         } | 
|         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; | 
|         } | 
|     } | 
| } |