|
@@ -0,0 +1,239 @@
|
|
|
+package com.fdkankan.scene.aop;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.fdkankan.common.model.SSOUser;
|
|
|
+import com.fdkankan.common.user.SSOLoginHelper;
|
|
|
+import com.fdkankan.scene.annotation.SystemControllerLog;
|
|
|
+import com.fdkankan.scene.annotation.SystemServiceLog;
|
|
|
+import com.fdkankan.scene.entity.Log;
|
|
|
+import com.fdkankan.scene.service.ILogService;
|
|
|
+import java.io.IOException;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Enumeration;
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpSession;
|
|
|
+import lombok.extern.log4j.Log4j2;
|
|
|
+import org.aspectj.lang.JoinPoint;
|
|
|
+import org.aspectj.lang.annotation.AfterThrowing;
|
|
|
+import org.aspectj.lang.annotation.Aspect;
|
|
|
+import org.aspectj.lang.annotation.Before;
|
|
|
+import org.aspectj.lang.annotation.Pointcut;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.core.annotation.Order;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 记录注册用户操作记录、异常记录
|
|
|
+ */
|
|
|
+@Log4j2
|
|
|
+@Aspect
|
|
|
+@Component
|
|
|
+@Order(2)
|
|
|
+public class SystemLogAspect {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ILogService logService;
|
|
|
+ @Autowired
|
|
|
+ private SSOLoginHelper ssoLoginHelper;
|
|
|
+
|
|
|
+ // Service层切点
|
|
|
+ @Pointcut("@annotation(com.fdkankan.scene.annotation.SystemServiceLog)")
|
|
|
+ public void serviceAspect() {
|
|
|
+ }
|
|
|
+
|
|
|
+ // Controller层切点
|
|
|
+ @Pointcut("@annotation(com.fdkankan.scene.annotation.SystemControllerLog)")
|
|
|
+ public void controllerAspect() {
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 异常通知 用于拦截service层记录异常日志
|
|
|
+ *
|
|
|
+ * @param joinPoint
|
|
|
+ * @param e
|
|
|
+ */
|
|
|
+ @AfterThrowing(pointcut = "serviceAspect()", throwing = "e")
|
|
|
+ public void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
|
|
|
+ HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
|
|
|
+ .getRequest();
|
|
|
+ HttpSession session = request.getSession();
|
|
|
+ // 读取session中的用户
|
|
|
+ SSOUser user = ssoLoginHelper.loginCheckV3(request.getHeader("token"));
|
|
|
+ // 获取请求ip
|
|
|
+ String ip = request.getRemoteAddr();
|
|
|
+ // 获取用户请求方法的参数并序列化为JSON格式字符串
|
|
|
+ String params = "";
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ for(Object obj : joinPoint.getArgs()){
|
|
|
+ if(obj instanceof HttpServletRequest){
|
|
|
+ Enumeration enu = ((HttpServletRequest)obj).getParameterNames();
|
|
|
+ while(enu.hasMoreElements()){
|
|
|
+ String paraName=(String)enu.nextElement();
|
|
|
+ jsonObject.put(paraName, request.getParameter(paraName));
|
|
|
+ params = jsonObject.toString();
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ if("".equals(params)){
|
|
|
+ params = obj.toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ /* ========控制台输出========= */
|
|
|
+ log.info("=====异常通知开始=====");
|
|
|
+ log.info("异常代码:" + e.getClass().getName());
|
|
|
+ log.info("异常信息:" + e.getMessage());
|
|
|
+ log.info("异常方法:"
|
|
|
+ + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));
|
|
|
+ log.info("方法描述:" + getServiceMthodDescription(joinPoint));
|
|
|
+ log.info("请求人:" + (user == null ? "" : user.getUserName()));
|
|
|
+ log.info("请求IP:" + ip);
|
|
|
+ log.info("请求参数:" + params);
|
|
|
+
|
|
|
+ /* ==========数据库日志========= */
|
|
|
+ Log logEntity = new Log();
|
|
|
+ logEntity.setDescription(getServiceMthodDescription(joinPoint));
|
|
|
+ logEntity.setExceptionCode(e.getClass().getName());
|
|
|
+ logEntity.setType("1");
|
|
|
+ logEntity.setExceptionDetail(e.getMessage());
|
|
|
+ logEntity.setMethod(
|
|
|
+ (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));
|
|
|
+ logEntity.setParams(params);
|
|
|
+ logEntity.setCreateBy(user == null ? null : user.getId());
|
|
|
+ logEntity.setCreateTime(new Date());
|
|
|
+ logEntity.setRequestIp(ip);
|
|
|
+ // 保存数据库
|
|
|
+ logService.save(logEntity);
|
|
|
+ log.info("=====异常通知结束=====");
|
|
|
+ } catch (Exception ex) {
|
|
|
+ // 记录本地异常日志
|
|
|
+ log.error("==异常通知异常==");
|
|
|
+ log.error("异常信息:{}", ex.getMessage());
|
|
|
+ }
|
|
|
+ /* ==========记录本地异常日志========== */
|
|
|
+ log.error("异常方法:{}异常代码:{}异常信息:{}参数:{}",
|
|
|
+ joinPoint.getTarget().getClass().getName() + joinPoint.getSignature().getName(), e.getClass().getName(),
|
|
|
+ e.getMessage(), params);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 前置通知 用于拦截Controller层记录用户的操作
|
|
|
+ *
|
|
|
+ * @param joinPoint
|
|
|
+ * 切点
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @Before("controllerAspect()")
|
|
|
+ public void doBefore(JoinPoint joinPoint) throws IOException {
|
|
|
+ HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
|
|
|
+ .getRequest();
|
|
|
+ HttpSession session = request.getSession();
|
|
|
+ // 读取session中的用户
|
|
|
+ SSOUser user = ssoLoginHelper.loginCheckV3(request.getHeader("token"));
|
|
|
+ // 请求的IP
|
|
|
+ String ip = request.getRemoteAddr();
|
|
|
+ String params = "";
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ try {
|
|
|
+ // *========控制台输出=========*//
|
|
|
+ log.info("=====前置通知开始=====");
|
|
|
+ log.info("请求方法:"
|
|
|
+ + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));
|
|
|
+ log.info("方法描述:" + getControllerMethodDescription(joinPoint));
|
|
|
+ log.info("请求人:" + (user == null ? "" : user.getUserName()));
|
|
|
+ log.info("请求IP:" + ip);
|
|
|
+ for(Object obj : joinPoint.getArgs()){
|
|
|
+ if(obj instanceof HttpServletRequest){
|
|
|
+ Enumeration enu = ((HttpServletRequest)obj).getParameterNames();
|
|
|
+ while(enu.hasMoreElements()){
|
|
|
+ String paraName=(String)enu.nextElement();
|
|
|
+ jsonObject.put(paraName, request.getParameter(paraName));
|
|
|
+ params = jsonObject.toString();
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ if("".equals(params)){
|
|
|
+ params = obj.toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // *========数据库日志=========*//
|
|
|
+ Log logEntity = new Log();
|
|
|
+ logEntity.setDescription(getControllerMethodDescription(joinPoint));
|
|
|
+ logEntity.setMethod(
|
|
|
+ (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));
|
|
|
+ logEntity.setType("0");
|
|
|
+ logEntity.setRequestIp(ip);
|
|
|
+ logEntity.setExceptionCode(null);
|
|
|
+ logEntity.setExceptionDetail(null);
|
|
|
+ logEntity.setParams(params);
|
|
|
+ logEntity.setCreateBy(user == null ? null : user.getId());
|
|
|
+ logEntity.setCreateTime(new Date());
|
|
|
+ // 保存数据库
|
|
|
+ logService.save(logEntity);
|
|
|
+ log.info("=====前置通知结束=====");
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 记录本地异常日志
|
|
|
+ log.error("==前置通知异常==");
|
|
|
+ log.error("异常信息:{}", e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取注解中对方法的描述信息 用于service层注解
|
|
|
+ *
|
|
|
+ * @param joinPoint
|
|
|
+ * 切点
|
|
|
+ * @return 方法描述
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static String getServiceMthodDescription(JoinPoint joinPoint) throws Exception {
|
|
|
+ String targetName = joinPoint.getTarget().getClass().getName();
|
|
|
+ String methodName = joinPoint.getSignature().getName();
|
|
|
+ Object[] arguments = joinPoint.getArgs();
|
|
|
+ Class targetClass = Class.forName(targetName);
|
|
|
+ Method[] methods = targetClass.getMethods();
|
|
|
+ String description = "";
|
|
|
+ for (Method method : methods) {
|
|
|
+ if (method.getName().equals(methodName)) {
|
|
|
+ Class[] clazzs = method.getParameterTypes();
|
|
|
+ if (clazzs.length == arguments.length) {
|
|
|
+ description = method.getAnnotation(SystemServiceLog.class).description();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return description;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取注解中对方法的描述信息 用于Controller层注解
|
|
|
+ *
|
|
|
+ * @param joinPoint
|
|
|
+ * 切点
|
|
|
+ * @return 方法描述
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static String getControllerMethodDescription(JoinPoint joinPoint) throws Exception {
|
|
|
+ String targetName = joinPoint.getTarget().getClass().getName();
|
|
|
+ String methodName = joinPoint.getSignature().getName();
|
|
|
+ Object[] arguments = joinPoint.getArgs();
|
|
|
+ Class targetClass = Class.forName(targetName);
|
|
|
+ Method[] methods = targetClass.getMethods();
|
|
|
+ String description = "";
|
|
|
+ for (Method method : methods) {
|
|
|
+ if (method.getName().equals(methodName)) {
|
|
|
+ Class[] clazzs = method.getParameterTypes();
|
|
|
+ if (clazzs.length == arguments.length) {
|
|
|
+ description = method.getAnnotation(SystemControllerLog.class).description();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return description;
|
|
|
+ }
|
|
|
+}
|