¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.framework.aspectj; |
| | | |
| | | import java.lang.reflect.Method; |
| | | import java.util.Map; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | 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 org.springframework.web.multipart.MultipartFile; |
| | | import org.springframework.web.servlet.HandlerMapping; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.ruoyi.common.annotation.Log; |
| | | import com.ruoyi.common.core.domain.model.LoginUser; |
| | | import com.ruoyi.common.enums.BusinessStatus; |
| | | import com.ruoyi.common.enums.HttpMethod; |
| | | import com.ruoyi.common.utils.ServletUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.ip.IpUtils; |
| | | import com.ruoyi.common.utils.spring.SpringUtils; |
| | | import com.ruoyi.framework.manager.AsyncManager; |
| | | import com.ruoyi.framework.manager.factory.AsyncFactory; |
| | | import com.ruoyi.framework.web.service.TokenService; |
| | | import com.ruoyi.system.domain.SysOperLog; |
| | | |
| | | /** |
| | | * æä½æ¥å¿è®°å½å¤ç |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @Aspect |
| | | @Component |
| | | public class LogAspect |
| | | { |
| | | private static final Logger log = LoggerFactory.getLogger(LogAspect.class); |
| | | |
| | | // é
ç½®ç»å
¥ç¹ |
| | | @Pointcut("@annotation(com.ruoyi.common.annotation.Log)") |
| | | public void logPointCut() |
| | | { |
| | | } |
| | | |
| | | /** |
| | | * å¤çå®è¯·æ±åæ§è¡ |
| | | * |
| | | * @param joinPoint åç¹ |
| | | */ |
| | | @AfterReturning(pointcut = "logPointCut()", returning = "jsonResult") |
| | | public void doAfterReturning(JoinPoint joinPoint, Object jsonResult) |
| | | { |
| | | handleLog(joinPoint, null, jsonResult); |
| | | } |
| | | |
| | | /** |
| | | * æ¦æªå¼å¸¸æä½ |
| | | * |
| | | * @param joinPoint åç¹ |
| | | * @param e å¼å¸¸ |
| | | */ |
| | | @AfterThrowing(value = "logPointCut()", throwing = "e") |
| | | public void doAfterThrowing(JoinPoint joinPoint, Exception e) |
| | | { |
| | | handleLog(joinPoint, e, null); |
| | | } |
| | | |
| | | protected void handleLog(final JoinPoint joinPoint, final Exception e, Object jsonResult) |
| | | { |
| | | try |
| | | { |
| | | // è·å¾æ³¨è§£ |
| | | Log controllerLog = getAnnotationLog(joinPoint); |
| | | if (controllerLog == null) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | // è·åå½åçç¨æ· |
| | | LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest()); |
| | | |
| | | // *========æ°æ®åºæ¥å¿=========*// |
| | | SysOperLog operLog = new SysOperLog(); |
| | | operLog.setStatus(BusinessStatus.SUCCESS.ordinal()); |
| | | // 请æ±çå°å |
| | | String ip = IpUtils.getIpAddr(ServletUtils.getRequest()); |
| | | operLog.setOperIp(ip); |
| | | // è¿ååæ° |
| | | operLog.setJsonResult(JSON.toJSONString(jsonResult)); |
| | | |
| | | operLog.setOperUrl(ServletUtils.getRequest().getRequestURI()); |
| | | if (loginUser != null) |
| | | { |
| | | operLog.setOperName(loginUser.getUsername()); |
| | | } |
| | | |
| | | 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 + "()"); |
| | | // è®¾ç½®è¯·æ±æ¹å¼ |
| | | operLog.setRequestMethod(ServletUtils.getRequest().getMethod()); |
| | | // å¤ç设置注解ä¸çåæ° |
| | | getControllerMethodDescription(joinPoint, controllerLog, operLog); |
| | | // ä¿åæ°æ®åº |
| | | AsyncManager.me().execute(AsyncFactory.recordOper(operLog)); |
| | | } |
| | | catch (Exception exp) |
| | | { |
| | | // è®°å½æ¬å°å¼å¸¸æ¥å¿ |
| | | log.error("==åç½®éç¥å¼å¸¸=="); |
| | | log.error("å¼å¸¸ä¿¡æ¯:{}", exp.getMessage()); |
| | | exp.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·å注解ä¸å¯¹æ¹æ³çæè¿°ä¿¡æ¯ ç¨äºController屿³¨è§£ |
| | | * |
| | | * @param log æ¥å¿ |
| | | * @param operLog æä½æ¥å¿ |
| | | * @throws Exception |
| | | */ |
| | | public void getControllerMethodDescription(JoinPoint joinPoint, Log log, SysOperLog operLog) throws Exception |
| | | { |
| | | // 设置actionå¨ä½ |
| | | operLog.setBusinessType(log.businessType().ordinal()); |
| | | // 设置æ é¢ |
| | | operLog.setTitle(log.title()); |
| | | // 设置æä½äººç±»å« |
| | | operLog.setOperatorType(log.operatorType().ordinal()); |
| | | // æ¯å¦éè¦ä¿årequestï¼åæ°åå¼ |
| | | if (log.isSaveRequestData()) |
| | | { |
| | | // è·ååæ°çä¿¡æ¯ï¼ä¼ å
¥å°æ°æ®åºä¸ã |
| | | setRequestValue(joinPoint, operLog); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·å请æ±çåæ°ï¼æ¾å°logä¸ |
| | | * |
| | | * @param operLog æä½æ¥å¿ |
| | | * @throws Exception å¼å¸¸ |
| | | */ |
| | | private void setRequestValue(JoinPoint joinPoint, SysOperLog operLog) throws Exception |
| | | { |
| | | String requestMethod = operLog.getRequestMethod(); |
| | | if (HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod)) |
| | | { |
| | | String params = argsArrayToString(joinPoint.getArgs()); |
| | | operLog.setOperParam(StringUtils.substring(params, 0, 2000)); |
| | | } |
| | | else |
| | | { |
| | | Map<?, ?> paramsMap = (Map<?, ?>) ServletUtils.getRequest().getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); |
| | | operLog.setOperParam(StringUtils.substring(paramsMap.toString(), 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; |
| | | } |
| | | |
| | | /** |
| | | * åæ°æ¼è£
|
| | | */ |
| | | private String argsArrayToString(Object[] paramsArray) |
| | | { |
| | | String params = ""; |
| | | if (paramsArray != null && paramsArray.length > 0) |
| | | { |
| | | for (int i = 0; i < paramsArray.length; i++) |
| | | { |
| | | if (!isFilterObject(paramsArray[i])) |
| | | { |
| | | Object jsonObj = JSON.toJSON(paramsArray[i]); |
| | | params += jsonObj.toString() + " "; |
| | | } |
| | | } |
| | | } |
| | | return params.trim(); |
| | | } |
| | | |
| | | /** |
| | | * 夿æ¯å¦éè¦è¿æ»¤ç对象ã |
| | | * |
| | | * @param o 对象信æ¯ã |
| | | * @return 妿æ¯éè¦è¿æ»¤ç对象ï¼åè¿åtrueï¼å¦åè¿åfalseã |
| | | */ |
| | | public boolean isFilterObject(final Object o) |
| | | { |
| | | return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse; |
| | | } |
| | | } |