AOP 中 获取常见的各种参数方法合集

时间:2023-02-22 17:57:33

(目录)


一、AOP中获取常见参数方法

本问总结了一下参数的获取方式:

获取方法名、参数值、参数值类型、目标注解对象、目标方法所在类

获取请求HttpServletRequestd对象、请求IP地址、请求路径(URI、URL)、获取请求方式


1、引入pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

2、创建一个自定义注解类CacheableTest

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface CacheableTest {
    String key();
    String value() default "";
    int expireTime() default 3600;
}

3、创建一个controller类,并加入该方法:

@RequestMapping("/findPage2")
@CacheableTest(key="haha",value = "hehe")
public String findPage2(Integer pageNumber, Integer pageSize){
    System.out.println("findPage2请求成功!!!");
    return "请求成功";
}

4、创建一个切面类TestAop:

package com.maxuan.aop;

import com.maxuan.service.CacheableTest;
import com.maxuan.utils.IPAddressUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
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 javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@Component
@Aspect
public class TestAop {

    /**
     *  IP 地址 解析工具类
     */
    @Autowired
    private IPAddressUtils ipAddressUtils;

    @Pointcut("execution(public * com.maxuan.controller.TestController.*(..))")    //第一个星号指返回值类型为任意
    private void pointCut(){};


    @Before(value = "pointCut()")
    public void logBefore(JoinPoint joinpoint) {
        System.out.println("----------Before开始-----------");

        System.out.println("方法名:"+ joinpoint.getSignature().getName());
        System.out.println("参数值集合:"+ Arrays.asList(joinpoint.getArgs()));
        System.out.println("参数值类型:"+ joinpoint.getArgs()[0].getClass().getTypeName());

        //获取目标注解对象,CacheableTest是自定义的一个注解
        CacheableTest cacheable = ((MethodSignature)joinpoint.getSignature()).getMethod().getAnnotation(CacheableTest.class);
        String classType = joinpoint.getTarget().getClass().getName();
        System.out.println("目标注解对象:"+ cacheable);
        System.out.println("获取目标方法所在类:"+ classType);

        //获取当前请求request对象
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        System.out.println("获取URI地址:"+request.getRequestURI());
        System.out.println("获取URL地址:"+request.getRequestURL());
        System.out.println("获取请求方式:"+request.getMethod());
        System.out.println("获取请求的ip地址:"+IPAddressUtils.getIpAdrress(request));

        System.out.println("----------Before结束-----------");
    }

    @After(value = "pointCut()")
    public void logAfter(JoinPoint joinpoint) {
        System.out.println("---------After开始------------");

        System.out.println("---------After结束-------------");
    }

}

执行结果: AOP 中 获取常见的各种参数方法合集


5. 请求用户IP的工具类

package com.maxuan.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;

@Slf4j
@Component
public class IPAddressUtils {
    /**
     * 获取IP地址
     */
    public static String getIpAdrress(HttpServletRequest request) {
        String ipAddress = request.getHeader("X-Forwarded-For");
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
            if ("127.0.0.1".equals(ipAddress) || "0:0:0:0:0:0:0:1".equals(ipAddress)) {
                // 根据网卡取本机配置的IP
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (Exception e) {
                    log.error("根据网卡获取本机配置的IP异常=>", e.getMessage());
                }
                if (inet.getHostAddress() != null) {
                    ipAddress = inet.getHostAddress();
                }
            }
        }
        // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
        if (ipAddress != null && ipAddress.length() > 15) {
            if (ipAddress.indexOf(",") > 0) {
                ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
            }
        }
        return ipAddress;
    }

}

二、环绕通知

注意:

该方法只有返回了point.proceed()后,执行接口方法后才会有返回值

@Around(value = "pointCut()")
public Object logBefore(JoinPoint joinpoint) throws Throwable {
    System.out.println("----------环绕开始-----------");

    System.out.println("方法名:"+ joinpoint.getSignature().getName());
    System.out.println("参数值集合:"+ Arrays.asList(joinpoint.getArgs()));
    System.out.println("参数值类型:"+ joinpoint.getArgs()[0].getClass().getTypeName());
    //获取目标注解对象,CacheableTest是自定义的一个注解
    CacheableTest cacheable = ((MethodSignature)joinpoint.getSignature()).getMethod().getAnnotation(CacheableTest.class);
    String classType = joinpoint.getTarget().getClass().getName();
    System.out.println("目标注解对象:"+ cacheable);
    System.out.println("获取目标方法所在类:"+ classType);

    ProceedingJoinPoint point = (ProceedingJoinPoint) joinpoint;
    System.out.println("----------环绕结束-----------");
    return point.proceed(); //放行,执行接口方法
}