|
@@ -1,6 +1,166 @@
|
|
|
-package com.yh.saas.plugin.yiliangyiyun.util;/**
|
|
|
+package com.yh.saas.plugin.yiliangyiyun.util;
|
|
|
+
|
|
|
+import cn.hutool.extra.servlet.ServletUtil;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.aliyun.oss.HttpMethod;
|
|
|
+import com.winsea.svc.base.security.util.AuthSecurityUtils;
|
|
|
+import com.yh.saas.plugin.yiliangyiyun.entity.HyOperLog;
|
|
|
+import com.yh.saas.plugin.yiliangyiyun.service.IHyOperLogService;
|
|
|
+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.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.validation.BindingResult;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+import org.springframework.web.servlet.HandlerMapping;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.util.Collection;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
* @Author:chengchangjiang
|
|
|
- * @Description:
|
|
|
+ * @Description:
|
|
|
* @Date:Created in 9:39 2022-08-02
|
|
|
- */public class LogAspect {
|
|
|
-}
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+@Aspect
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class LogAspect {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IHyOperLogService operLogService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理完请求后执行
|
|
|
+ * @param joinPoint 切点
|
|
|
+ */
|
|
|
+ @AfterReturning(pointcut = "@annotation(controllerLog)", returning = "jsonResult")
|
|
|
+ public void doAfterReturnibng(JoinPoint joinPoint, Log controllerLog, Object jsonResult) {
|
|
|
+ handleLog(joinPoint, controllerLog, null, jsonResult);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected void handleLog(final JoinPoint joinPoint, Log controllerLog, final Exception e, Object jsonResult) {
|
|
|
+ try {
|
|
|
+ // 获取当前的用户
|
|
|
+ String accountId = AuthSecurityUtils.getCurrentUserInfo().getUserId();
|
|
|
+ // 日志记录
|
|
|
+ HyOperLog operLog = new HyOperLog();
|
|
|
+ operLog.setStatus("成功");
|
|
|
+ // 请求的IP地址
|
|
|
+ String iP = ServletUtil.getClientIP(ServletUtils.getRequest());
|
|
|
+ if ("0:0:0:0:0:0:0:1".equals(iP)) {
|
|
|
+ iP = "127.0.0.1";
|
|
|
+ }
|
|
|
+ operLog.setIp(iP);
|
|
|
+ operLog.setUrl(ServletUtils.getRequest().getRequestURI());
|
|
|
+ if (accountId != null) {
|
|
|
+ operLog.setCreateUserId(accountId);
|
|
|
+ }
|
|
|
+ if (e != null) {
|
|
|
+ operLog.setStatus("异常");
|
|
|
+ operLog.setErrMsg(StringUtils.substring(e.getMessage(), 0, 2000));
|
|
|
+ }
|
|
|
+ // 设置方法名称
|
|
|
+ String className = joinPoint.getTarget().getClass().getName();
|
|
|
+ String methodName = joinPoint.getSignature().getName();
|
|
|
+ operLog.setMethod(className + "." + methodName + "()");
|
|
|
+ operLog.setRequestMethod(ServletUtils.getRequest().getMethod());
|
|
|
+ operLog.setCreateDate(new Date());
|
|
|
+ // 处理设置注解上的参数
|
|
|
+ getControllerMethodDescription(joinPoint, controllerLog, operLog, jsonResult);
|
|
|
+ // 保存数据库
|
|
|
+ operLogService.insert(operLog);
|
|
|
+
|
|
|
+ } catch (Exception exp) {
|
|
|
+ log.error("异常信息:{}", exp.getMessage());
|
|
|
+ exp.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取注解中对方法的描述信息 用于Controller层注解
|
|
|
+ * @param log 日志
|
|
|
+ * @param operLog 操作日志
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public void getControllerMethodDescription(JoinPoint joinPoint, Log log, HyOperLog operLog, Object jsonResult) throws Exception {
|
|
|
+ // 设置标题
|
|
|
+ operLog.setTitle(log.title());
|
|
|
+ // 是否需要保存request,参数和值
|
|
|
+ if (log.isSaveRequestData()) {
|
|
|
+ // 设置参数的信息
|
|
|
+ setRequestValue(joinPoint, operLog);
|
|
|
+ }
|
|
|
+ // 是否需要保存response,参数和值
|
|
|
+ if (log.isSaveResponseData() && jsonResult!=null&&!jsonResult.toString().isEmpty()) {
|
|
|
+ operLog.setJsonResult(StringUtils.substring(JSONObject.toJSONString(jsonResult), 0, 5000));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取请求的参数,放到log中
|
|
|
+ * @param operLog 操作日志
|
|
|
+ * @throws Exception 异常
|
|
|
+ */
|
|
|
+ private void setRequestValue(JoinPoint joinPoint, HyOperLog operLog) throws Exception {
|
|
|
+ String requsetMethod = operLog.getRequestMethod();
|
|
|
+ if (HttpMethod.PUT.name().equals(requsetMethod) || HttpMethod.POST.name().equals(requsetMethod)) {
|
|
|
+ String parsams = argsArrayToString(joinPoint.getArgs());
|
|
|
+ operLog.setRequestValue(StringUtils.substring(parsams,0,2000));
|
|
|
+ } else {
|
|
|
+ Map<?,?> paramsMap = (Map<?,?>) ServletUtils.getRequest().getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
|
|
|
+ operLog.setRequestValue(StringUtils.substring(paramsMap.toString(),0,2000));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 参数拼装
|
|
|
+ */
|
|
|
+ private String argsArrayToString(Object[] paramsArray) {
|
|
|
+ String params = "";
|
|
|
+ if (paramsArray != null && paramsArray.length > 0) {
|
|
|
+ for (Object object : paramsArray) {
|
|
|
+ // 不为空 并且是不需要过滤的 对象
|
|
|
+ if (object!=null&&!object.toString().isEmpty() && !isFilterObject(object)) {
|
|
|
+ Object jsonObj = JSONObject.toJSON(object);
|
|
|
+ params += jsonObj.toString() + " ";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return params.trim();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断是否需要过滤的对象。
|
|
|
+ * @param object 对象信息。
|
|
|
+ * @return 如果是需要过滤的对象,则返回true;否则返回false。
|
|
|
+ */
|
|
|
+ @SuppressWarnings("rawtypes")
|
|
|
+ public boolean isFilterObject(final Object object) {
|
|
|
+ Class<?> clazz = object.getClass();
|
|
|
+ if (clazz.isArray()) {
|
|
|
+ return clazz.getComponentType().isAssignableFrom(MultipartFile.class);
|
|
|
+ } else if (Collection.class.isAssignableFrom(clazz)) {
|
|
|
+ Collection collection = (Collection) object;
|
|
|
+ for (Object value : collection) {
|
|
|
+ return value instanceof MultipartFile;
|
|
|
+ }
|
|
|
+ } else if (Map.class.isAssignableFrom(clazz)) {
|
|
|
+ Map map = (Map) object;
|
|
|
+ for (Object value : map.entrySet()) {
|
|
|
+ Map.Entry entry = (Map.Entry) value;
|
|
|
+ return entry.getValue() instanceof MultipartFile;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return object instanceof MultipartFile || object instanceof HttpServletRequest
|
|
|
+ || object instanceof HttpServletResponse || object instanceof BindingResult;
|
|
|
+ }
|
|
|
+}
|