在SpringBoot中使用AOP获取HttpServletRequest、HttpSession 内容

POM.xml添加AOP依赖:

  
      org.springframework.boot
      spring-boot-starter-aop
  

然后在拦截器类中添加切面代码:

package com.tbms2;

import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import com.alibaba.fastjson.JSON;

@Component
@Aspect
public class WebControllerAop {

    Logger logger = LoggerFactory.getLogger(WebControllerAop.class);
    
    @Pointcut("execution(* com.tbms2.web..*.json*(..))")
    public void executeService(){
        
    }
    
    @Before("executeService()")
    public void doBeforeAdvice(JoinPoint joinPoint) {
        logger.info("==> json方法调用开始...");
        //获取目标方法的参数信息  
        Object[] obj = joinPoint.getArgs();  
        //AOP代理类的信息  
        joinPoint.getThis();  
        //代理的目标对象  
        joinPoint.getTarget();  
        //用的最多 通知的签名  
        Signature signature = joinPoint.getSignature();  
        //代理的是哪一个方法  
        logger.info("==> 代理的是哪一个方法 :"+signature.getName());
        //AOP代理类的名字  
        logger.info("==> AOP代理类的名字:"+signature.getDeclaringTypeName());
        //AOP代理类的类(class)信息  
        signature.getDeclaringType();  
        //获取RequestAttributes  
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();  
        //从获取RequestAttributes中获取HttpServletRequest的信息  
        HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);  
        logger.info("==> 请求者的IP:"+request.getRemoteAddr());
        //如果要获取Session信息的话,可以这样写:  
        //HttpSession session = (HttpSession) requestAttributes.resolveReference(RequestAttributes.REFERENCE_SESSION);  
        Enumeration enumeration = request.getParameterNames();  
        Map parameterMap = new HashMap();
        while (enumeration.hasMoreElements()){
            String parameter = enumeration.nextElement();  
            parameterMap.put(parameter,request.getParameter(parameter));  
        }  
        String str = JSON.toJSONString(parameterMap);  
        if(obj.length > 0) {  
            logger.info("==> 请求的参数信息为:"+str);
        }
    }
    
}

你可能感兴趣的:(JAVA)