package com.ots.framework.aspectj; import com.alibaba.fastjson.JSONObject; import com.ots.common.utils.ServletUtils; import com.ots.common.utils.StringUtils; import com.ots.common.utils.security.ShiroUtils; import com.ots.framework.aspectj.lang.annotation.Log; import com.ots.framework.aspectj.lang.enums.BusinessStatus; import com.ots.framework.manager.AsyncManager; import com.ots.framework.manager.factory.AsyncFactory; import com.ots.project.monitor.operlog.domain.OperLog; import com.ots.project.system.user.domain.User; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import java.lang.reflect.Method; import java.util.Map; @Aspect @Component public class LogAspect { private static final Logger log = LoggerFactory.getLogger(LogAspect.class); @Pointcut("@annotation(com.ots.framework.aspectj.lang.annotation.Log)") public void logPointCut() { } @AfterReturning(pointcut = "logPointCut()") public void doAfterReturning(JoinPoint joinPoint) { handleLog(joinPoint, null); } @AfterThrowing(value = "logPointCut()", throwing = "e") public void doAfterThrowing(JoinPoint joinPoint, Exception e) { handleLog(joinPoint, e); } protected void handleLog(final JoinPoint joinPoint, final Exception e) { try { Log controllerLog = getAnnotationLog(joinPoint); if (controllerLog == null) { return; } User currentUser = ShiroUtils.getSysUser(); OperLog operLog = new OperLog(); operLog.setStatus(BusinessStatus.SUCCESS.ordinal()); String ip = ShiroUtils.getIp(); operLog.setOperIp(ip); operLog.setOperUrl(ServletUtils.getRequest().getRequestURI()); if (currentUser != null) { operLog.setOperName(currentUser.getLoginName()); if (StringUtils.isNotNull(currentUser.getDept()) && StringUtils.isNotEmpty(currentUser.getDept().getDeptName())) { operLog.setDeptName(currentUser.getDept().getDeptName()); } } if (e != null) { operLog.setStatus(BusinessStatus.FAIL.ordinal()); operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000)); } String className = joinPoint.getTarget().getClass().getName(); String methodName = joinPoint.getSignature().getName(); operLog.setMethod(className + "." + methodName + "()"); getControllerMethodDescription(controllerLog, operLog); AsyncManager.me().execute(AsyncFactory.recordOper(operLog)); } catch (Exception exp) { log.error("==前置通知异常=="); log.error("异常信息:{}", exp.getMessage()); exp.printStackTrace(); } } public void getControllerMethodDescription(Log log, OperLog operLog) throws Exception { operLog.setBusinessType(log.businessType().ordinal()); operLog.setTitle(log.title()); operLog.setOperatorType(log.operatorType().ordinal()); if (log.isSaveRequestData()) { setRequestValue(operLog); } } private void setRequestValue(OperLog operLog) { Map map = ServletUtils.getRequest().getParameterMap(); String params = JSONObject.toJSONString(map); operLog.setOperParam(StringUtils.substring(params, 0, 2000)); } private Log getAnnotationLog(JoinPoint joinPoint) throws Exception { Signature signature = joinPoint.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; Method method = methodSignature.getMethod(); if (method != null) { return method.getAnnotation(Log.class); } return null; } }