中国算力平台算力登记系统2.0
YZFG
2024-09-30 43dc2996fd2033991539ed35a0429238829a5417
commit | author | age
43dc29 1 package com.odcc.cpzidc.framework.aspectj;
Y 2
3 import java.util.Collection;
4 import java.util.Map;
5 import javax.servlet.http.HttpServletRequest;
6 import javax.servlet.http.HttpServletResponse;
7 import org.apache.commons.lang3.ArrayUtils;
8 import org.aspectj.lang.JoinPoint;
9 import org.aspectj.lang.annotation.AfterReturning;
10 import org.aspectj.lang.annotation.AfterThrowing;
11 import org.aspectj.lang.annotation.Aspect;
12 import org.aspectj.lang.annotation.Before;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15 import org.springframework.core.NamedThreadLocal;
16 import org.springframework.stereotype.Component;
17 import org.springframework.validation.BindingResult;
18 import org.springframework.web.multipart.MultipartFile;
19 import com.alibaba.fastjson2.JSON;
20 import com.odcc.cpzidc.common.annotation.Log;
21 import com.odcc.cpzidc.common.core.domain.entity.SysUser;
22 import com.odcc.cpzidc.common.core.domain.model.LoginUser;
23 import com.odcc.cpzidc.common.enums.BusinessStatus;
24 import com.odcc.cpzidc.common.enums.HttpMethod;
25 import com.odcc.cpzidc.common.filter.PropertyPreExcludeFilter;
26 import com.odcc.cpzidc.common.utils.SecurityUtils;
27 import com.odcc.cpzidc.common.utils.ServletUtils;
28 import com.odcc.cpzidc.common.utils.StringUtils;
29 import com.odcc.cpzidc.common.utils.ip.IpUtils;
30 import com.odcc.cpzidc.framework.manager.AsyncManager;
31 import com.odcc.cpzidc.framework.manager.factory.AsyncFactory;
32 import com.odcc.cpzidc.system.domain.SysOperLog;
33
34 /**
35  * 操作日志记录处理
36  * 
37  * @author ruoyi
38  */
39 @Aspect
40 @Component
41 public class LogAspect
42 {
43     private static final Logger log = LoggerFactory.getLogger(LogAspect.class);
44
45     /** 排除敏感属性字段 */
46     public static final String[] EXCLUDE_PROPERTIES = { "password", "oldPassword", "newPassword", "confirmPassword" };
47
48     /** 计算操作消耗时间 */
49     private static final ThreadLocal<Long> TIME_THREADLOCAL = new NamedThreadLocal<Long>("Cost Time");
50
51     /**
52      * 处理请求前执行
53      */
54     @Before(value = "@annotation(controllerLog)")
55     public void boBefore(JoinPoint joinPoint, Log controllerLog)
56     {
57         TIME_THREADLOCAL.set(System.currentTimeMillis());
58     }
59
60     /**
61      * 处理完请求后执行
62      *
63      * @param joinPoint 切点
64      */
65     @AfterReturning(pointcut = "@annotation(controllerLog)", returning = "jsonResult")
66     public void doAfterReturning(JoinPoint joinPoint, Log controllerLog, Object jsonResult)
67     {
68         handleLog(joinPoint, controllerLog, null, jsonResult);
69     }
70
71     /**
72      * 拦截异常操作
73      * 
74      * @param joinPoint 切点
75      * @param e 异常
76      */
77     @AfterThrowing(value = "@annotation(controllerLog)", throwing = "e")
78     public void doAfterThrowing(JoinPoint joinPoint, Log controllerLog, Exception e)
79     {
80         handleLog(joinPoint, controllerLog, e, null);
81     }
82
83     protected void handleLog(final JoinPoint joinPoint, Log controllerLog, final Exception e, Object jsonResult)
84     {
85         try
86         {
87             // 获取当前的用户
88             LoginUser loginUser = SecurityUtils.getLoginUser();
89
90             // *========数据库日志=========*//
91             SysOperLog operLog = new SysOperLog();
92             operLog.setStatus(BusinessStatus.SUCCESS.ordinal());
93             // 请求的地址
94             String ip = IpUtils.getIpAddr();
95             operLog.setOperIp(ip);
96             operLog.setOperUrl(StringUtils.substring(ServletUtils.getRequest().getRequestURI(), 0, 255));
97             if (loginUser != null)
98             {
99                 operLog.setOperName(loginUser.getUsername());
100                 SysUser currentUser = loginUser.getUser();
101                 if (StringUtils.isNotNull(currentUser) && StringUtils.isNotNull(currentUser.getDept()))
102                 {
103                     operLog.setDeptName(currentUser.getDept().getDeptName());
104                 }
105             }
106
107             if (e != null)
108             {
109                 operLog.setStatus(BusinessStatus.FAIL.ordinal());
110                 operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000));
111             }
112             // 设置方法名称
113             String className = joinPoint.getTarget().getClass().getName();
114             String methodName = joinPoint.getSignature().getName();
115             operLog.setMethod(className + "." + methodName + "()");
116             // 设置请求方式
117             operLog.setRequestMethod(ServletUtils.getRequest().getMethod());
118             // 处理设置注解上的参数
119             getControllerMethodDescription(joinPoint, controllerLog, operLog, jsonResult);
120             // 设置消耗时间
121             operLog.setCostTime(System.currentTimeMillis() - TIME_THREADLOCAL.get());
122             // 保存数据库
123             AsyncManager.me().execute(AsyncFactory.recordOper(operLog));
124         }
125         catch (Exception exp)
126         {
127             // 记录本地异常日志
128             log.error("异常信息:{}", exp.getMessage());
129             exp.printStackTrace();
130         }
131         finally
132         {
133             TIME_THREADLOCAL.remove();
134         }
135     }
136
137     /**
138      * 获取注解中对方法的描述信息 用于Controller层注解
139      * 
140      * @param log 日志
141      * @param operLog 操作日志
142      * @throws Exception
143      */
144     public void getControllerMethodDescription(JoinPoint joinPoint, Log log, SysOperLog operLog, Object jsonResult) throws Exception
145     {
146         // 设置action动作
147         operLog.setBusinessType(log.businessType().ordinal());
148         // 设置标题
149         operLog.setTitle(log.title());
150         // 设置操作人类别
151         operLog.setOperatorType(log.operatorType().ordinal());
152         // 是否需要保存request,参数和值
153         if (log.isSaveRequestData())
154         {
155             // 获取参数的信息,传入到数据库中。
156             setRequestValue(joinPoint, operLog, log.excludeParamNames());
157         }
158         // 是否需要保存response,参数和值
159         if (log.isSaveResponseData() && StringUtils.isNotNull(jsonResult))
160         {
161             operLog.setJsonResult(StringUtils.substring(JSON.toJSONString(jsonResult), 0, 2000));
162         }
163     }
164
165     /**
166      * 获取请求的参数,放到log中
167      * 
168      * @param operLog 操作日志
169      * @throws Exception 异常
170      */
171     private void setRequestValue(JoinPoint joinPoint, SysOperLog operLog, String[] excludeParamNames) throws Exception
172     {
173         Map<?, ?> paramsMap = ServletUtils.getParamMap(ServletUtils.getRequest());
174         String requestMethod = operLog.getRequestMethod();
175         if (StringUtils.isEmpty(paramsMap)
176                 && (HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod)))
177         {
178             String params = argsArrayToString(joinPoint.getArgs(), excludeParamNames);
179             operLog.setOperParam(StringUtils.substring(params, 0, 2000));
180         }
181         else
182         {
183             operLog.setOperParam(StringUtils.substring(JSON.toJSONString(paramsMap, excludePropertyPreFilter(excludeParamNames)), 0, 2000));
184         }
185     }
186
187     /**
188      * 参数拼装
189      */
190     private String argsArrayToString(Object[] paramsArray, String[] excludeParamNames)
191     {
192         String params = "";
193         if (paramsArray != null && paramsArray.length > 0)
194         {
195             for (Object o : paramsArray)
196             {
197                 if (StringUtils.isNotNull(o) && !isFilterObject(o))
198                 {
199                     try
200                     {
201                         String jsonObj = JSON.toJSONString(o, excludePropertyPreFilter(excludeParamNames));
202                         params += jsonObj.toString() + " ";
203                     }
204                     catch (Exception e)
205                     {
206                     }
207                 }
208             }
209         }
210         return params.trim();
211     }
212
213     /**
214      * 忽略敏感属性
215      */
216     public PropertyPreExcludeFilter excludePropertyPreFilter(String[] excludeParamNames)
217     {
218         return new PropertyPreExcludeFilter().addExcludes(ArrayUtils.addAll(EXCLUDE_PROPERTIES, excludeParamNames));
219     }
220
221     /**
222      * 判断是否需要过滤的对象。
223      * 
224      * @param o 对象信息。
225      * @return 如果是需要过滤的对象,则返回true;否则返回false。
226      */
227     @SuppressWarnings("rawtypes")
228     public boolean isFilterObject(final Object o)
229     {
230         Class<?> clazz = o.getClass();
231         if (clazz.isArray())
232         {
233             return clazz.getComponentType().isAssignableFrom(MultipartFile.class);
234         }
235         else if (Collection.class.isAssignableFrom(clazz))
236         {
237             Collection collection = (Collection) o;
238             for (Object value : collection)
239             {
240                 return value instanceof MultipartFile;
241             }
242         }
243         else if (Map.class.isAssignableFrom(clazz))
244         {
245             Map map = (Map) o;
246             for (Object value : map.entrySet())
247             {
248                 Map.Entry entry = (Map.Entry) value;
249                 return entry.getValue() instanceof MultipartFile;
250             }
251         }
252         return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse
253                 || o instanceof BindingResult;
254     }
255 }