zoukankan      html  css  js  c++  java
  • 接口拦截加操作日志

    工具类RequestLogAspect

    package com.fxkj.manager.aspect;
    
    import com.fxkj.common.dto.OperParamDTO;
    import com.fxkj.common.dto.SystemUserDTO;
    import com.fxkj.common.mapper.SysOperRecordMapper;
    import com.fxkj.common.model.SysOperRecord;
    import com.fxkj.common.security.TokenUserAutoConfiguration;
    import com.fxkj.common.util.IDGenerator;
    import com.fxkj.common.util.IPUtils;
    import com.fxkj.common.util.JsonUtils;
    import eu.bitwalker.useragentutils.UserAgent;
    import io.swagger.annotations.ApiOperation;
    import lombok.extern.slf4j.Slf4j;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.Signature;
    import org.aspectj.lang.annotation.*;
    import org.aspectj.lang.reflect.MethodSignature;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    import org.springframework.web.multipart.MultipartFile;
    
    import javax.servlet.http.HttpServletRequest;
    import java.lang.annotation.Annotation;
    import java.lang.reflect.Method;
    import java.time.LocalDateTime;
    import java.util.HashMap;
    import java.util.Map;
    
    @Component
    @Aspect
    @Slf4j
    public class RequestLogAspect {
    
        @Autowired
        private SysOperRecordMapper sysOperRecordMapper;
    
        @Autowired
        private TokenUserAutoConfiguration tokenUserAutoConfiguration;
    
        //com.fxkj.manager.web下的所有接口 && 不是com.fxkj.manager.web.wx下的接口:完成后插入操作记录
        @Pointcut("execution(* com.fxkj.manager.web..*(..)) && !execution(* com.fxkj.manager.web.wx..*(..))")
        public void requestServer() {
        }
    
        @Before("requestServer()")
        public void doBefore(JoinPoint joinPoint) {
            log.info("Class Method       : {}.{}", joinPoint.getSignature().getDeclaringTypeName(),
                    joinPoint.getSignature().getName());
        }
    
    
        @Around("requestServer()")
        public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    
            ServletRequestAttributes attributes = (ServletRequestAttributes)
                    RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
    
            MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
            ApiOperation annotation = methodSignature.getMethod().getAnnotation(ApiOperation.class);
    
            long start = System.currentTimeMillis();
            UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
            OperParamDTO requestParams = getRequestParams(proceedingJoinPoint);
            SystemUserDTO systemUserDTO = tokenUserAutoConfiguration.systemUser(request);
            if(systemUserDTO !=null){
                requestParams.setUserId(systemUserDTO.getUserId());
                requestParams.setUserName(systemUserDTO.getUsername());
            }
    
            String value = annotation ==null ? "":annotation.value();
            
    		/*============根据自己业务需求插入操作记录============*/
            SysOperRecord sysOperRecord = SysOperRecord.builder()
                    .id(IDGenerator.generateUUID())
                    .userId(getRequestParams(proceedingJoinPoint).getUserId())
                    .userName(getRequestParams(proceedingJoinPoint).getUserName())
                    .operContent(value + ":" + requestParams.getParams() + "")
                    .operType(request.getMethod())
                    .createTime(LocalDateTime.now())
                    .build();
            sysOperRecordMapper.insert(sysOperRecord);
    
            return proceedingJoinPoint.proceed();
        }
    
        @After("requestServer()")
        public void doAfter(JoinPoint joinPoint) {
            log.info("===============================End========================");
        }
    
        /**
         * 获取入参
         * @param proceedingJoinPoint
         * @return
         * */
        private OperParamDTO getRequestParams(ProceedingJoinPoint proceedingJoinPoint) {
            Map<String, Object> requestParams = new HashMap<>();
    
            OperParamDTO operParamDTO = new OperParamDTO();
    
            //参数名
            String[] paramNames =
                    ((MethodSignature)proceedingJoinPoint.getSignature()).getParameterNames();
            //参数值
            Object[] paramValues = proceedingJoinPoint.getArgs();
    
            for (int i = 0; i < paramNames.length; i++) {
                Object value = paramValues[i];
                //如果是文件对象
                if (value instanceof MultipartFile) {
                    MultipartFile file = (MultipartFile) value;
                    value = file.getOriginalFilename();  //获取文件名
                }
                if(! (value instanceof SystemUserDTO)){
                    requestParams.put(paramNames[i], value);
                }
            }
            operParamDTO.setParams(requestParams);
            return operParamDTO;
        }
    }
    

      

  • 相关阅读:
    Unity The Method Signature Matching Rule
    Unity The Property Matching Rule
    Unity The Type Matching Rule
    Unity The Custom Attribute Matching Rule
    Unity The Member Name Matching Rule
    Unity No Policies
    Unity The Return Type Matching Rule
    Unity The Parameter Type Matching Rule
    Unity The Namespace Matching Rule
    关于TSQL递归查询的(转)
  • 原文地址:https://www.cnblogs.com/csyzlm/p/14048337.html
Copyright © 2011-2022 走看看