The interceptor obtains the parameters and return value of the intercepted method, and judges whether the passed parameters are empty

import javax.servlet.http.HttpServletRequest;
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.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Created by xuweilin on 12/16/16.
*/
@Aspect // Define a facet
@Configuration
public class LogRecordAspect {
private static final Logger logger = LoggerFactory.getLogger(LogRecordAspect.class);

    // Define pointcut, all methods of UserController class
    @Pointcut("execution(* com.myhexin .xwl.UserController.*(..))")
    public void excudeService() {
    }

    // Define pointcut Pointcut, method execution of all classes in the controller package and all subpackages
    @Pointcut("execution(* com.myhexin. xwl.controller..*.*(..))")
    public void excudeService2() {
    }

    // Indicates that both of the above match
    @Pointcut("excudeService() || excudeService2()")
    public void excudeService3() {
    }

    @Around("excudeService3()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();

        String url = request.getRequestURL().toString();
        String method = request.getMethod();
        String uri = request.getRequestURI();
        String queryString = request.getQueryString();
        logger.info("请求开始, 各个参数, url: {}, method: {}, uri: {}, params: {}", url, method, uri, queryString);
       
        String res=queryString.replaceAll("%22", "");
        logger.info(res);
       
        String[] str=res.split("&");
        // Determine whether the incoming parameter is empty
        for(String str1:str){
        String[] str2=str1.split("= ");
        if(str2.length<=1){
        logger.info(str2[0]+"Empty!");
            break;
        }else if(StringUtils.isEmpty(str2[1])){
        logger.info( str2[0]+"Empty!");
            break;
        }
       
        }

        // Get the name of the method to be executed 
        String methodName = pjp.getSignature().getName();
        // Get the parameters of the executed method 
        Object[] args = pjp .getArgs();
        args[args.length-1]=true;
        // Get the parameter object from the parameter list 
        for(Object obj : args){// View the parameter value 
           System.out.println("***********"+obj.toString()); 
        }

        // The value of result is the return value of the intercepted method
        Object result = pjp.proceed();
        // No return, the return value of the intercepted method cannot be received (eg: the foreground cannot receive the value)
        return result;
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326846828&siteId=291194637