|
@@ -0,0 +1,97 @@
|
|
|
+package com.iamberry.rst.controllers.log;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.iamberry.rst.core.sys.Admin;
|
|
|
+import com.iamberry.rst.core.sys.SystemLogs;
|
|
|
+import com.iamberry.rst.faces.sys.SysService;
|
|
|
+import com.iamberry.rst.utils.AdminUtils;
|
|
|
+import org.apache.shiro.authz.annotation.RequiresPermissions;
|
|
|
+import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
+import org.aspectj.lang.annotation.Around;
|
|
|
+import org.aspectj.lang.annotation.Aspect;
|
|
|
+import org.aspectj.lang.annotation.Pointcut;
|
|
|
+import org.aspectj.lang.reflect.MethodSignature;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.context.request.RequestAttributes;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Enumeration;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 统一日志处理
|
|
|
+ * @author 献
|
|
|
+ */
|
|
|
+public class LogAopHandler {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SysService sysService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * controller层面记录操作日志
|
|
|
+ * 注意此处是aop:around的 因为需要得到请求前的参数以及请求后接口返回的结果
|
|
|
+ * @throws Throwable
|
|
|
+ */
|
|
|
+ public Object doSaveLog(ProceedingJoinPoint joinPoint) throws Throwable {
|
|
|
+
|
|
|
+ System.out.println("日日日日-----------------------------");
|
|
|
+
|
|
|
+ // 获取Reqest
|
|
|
+ RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
|
|
|
+ if (requestAttributes == null) {
|
|
|
+ return joinPoint.proceed(joinPoint.getArgs());
|
|
|
+ }
|
|
|
+ // 获取请求方法
|
|
|
+ MethodSignature method = (MethodSignature) joinPoint.getSignature();
|
|
|
+ // 此处不能用getAnnotationByType 是JAVA8的特性,因为注解能够重名,所以得到的是数组
|
|
|
+ RequiresPermissions annotation = method.getMethod().getAnnotation(RequiresPermissions.class);
|
|
|
+ if (annotation == null) {
|
|
|
+ return joinPoint.proceed(joinPoint.getArgs());
|
|
|
+ }
|
|
|
+
|
|
|
+ /* 只是记录经过拦截的日志 */
|
|
|
+ HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
|
|
|
+
|
|
|
+ // 请求参数实例化为JSON格式的字符串
|
|
|
+ Enumeration<String> params = request.getParameterNames();
|
|
|
+ Map<String, Object> paramMap = new HashMap<>();
|
|
|
+ while (params.hasMoreElements()) {
|
|
|
+ // 获取请求参数以及对应的值,防止多个值的问题
|
|
|
+ String param = params.nextElement();
|
|
|
+ String[] values = request.getParameterValues(param);
|
|
|
+ if (values == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (values.length == 1) {
|
|
|
+ paramMap.put(param, values[0]);
|
|
|
+ } else {
|
|
|
+ paramMap.put(param, values);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 请求参数
|
|
|
+ String requestJSON = JSONObject.toJSONString(paramMap);
|
|
|
+ // 操作用户id
|
|
|
+ Integer adminId = AdminUtils.getLoginAdminId();
|
|
|
+ Admin temp = new Admin();
|
|
|
+ temp.setAdminId(adminId);
|
|
|
+ temp = sysService.getAdminInfo(temp);
|
|
|
+ // 操作权限
|
|
|
+ String symbol = annotation.value()[0];
|
|
|
+ String symbolName = sysService.getSymbolName(symbol);
|
|
|
+ // 描述 【XX】在【什么时候】操作了【什么】
|
|
|
+ String details = "【" + temp.getAdminName() + "】操作【" + symbolName + "】";
|
|
|
+ SystemLogs logs = new SystemLogs();
|
|
|
+ logs.setAdminId(adminId);
|
|
|
+ logs.setLogsSymbol(symbol);
|
|
|
+ logs.setLogsDetails(details);
|
|
|
+ logs.setLogsRequest(requestJSON);
|
|
|
+ sysService.addLogs(logs);
|
|
|
+ // 注意around的情况下需要返回result 否则将不会返回值给请求者
|
|
|
+ return joinPoint.proceed(joinPoint.getArgs());
|
|
|
+ }
|
|
|
+}
|