使用SpringAOP切面实现对controller的拦截,并对url,参数和返回值记录

时间:2025-05-15 17:14:57

做这个功能之前 先讲一下AOP的环绕通知,因为这个功能我之前也想用springMVC的拦截器实现

AOP的环绕通知、切面的优先级以及重用切入点定义

一、环绕通知

  环绕通知是所有通知类型中功能最为强大的, 能够全面地控制连接点. 甚至可以控制是否执行连接点.
  对于环绕通知来说, 连接点的参数类型必须是 ProceedingJoinPoint . 它是 JoinPoint 的子接口, 允许控制何时执行, 是否执行连接点.
  在环绕通知中需要明确调用 ProceedingJoinPoint 的 proceed() 方法来执行被代理的方法. 如果忘记这样做就会导致通知被执行了, 但目标方法没有被执行.
  注意: 环绕通知的方法需要返回目标方法执行之后的结果, 即调用 (); 的返回值, 否则会出现空指针异常
   (1)环绕通知需要携带 ProceedingJoinPoint 类型的参数.
   (2)环绕通知类似于动态代理的全过程: ProceedingJoinPoint 类型的参数可以决定是否执行目标方法.且环绕通知必须有返回值, 返回值即为目标方法的返回值

@Around("execution(* com...*.*(..))")  
public Object aroundMethod(ProceedingJoinPoint pjd){  

    Object result = null;  
    String methodName = ().getName();  

    try {  
        //前置通知  
        System.out.println("The method " + methodName + " begins with " + (()));  
        //执行目标方法  
        result = ();  
        //返回通知  
        System.out.println("The method " + methodName + " ends with " + result);  
    } catch (Throwable e) {  
        //异常通知  
        System.out.println("The method " + methodName + " occurs exception:" + e);  
        throw new RuntimeException(e);  
    }  
    //后置通知  
    System.out.println("The method " + methodName + " ends");  

    return result; 

参考自:/snow_7/article/details/52077770

url获取必须通过request中获取,怎么获取request呢?

的使用

RequestContextHolder顾名思义,持有上下文的Request容器.使用是很简单的,具体使用如下:

//两个方法在没有使用JSF的项目中是没有区别的
RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
//                                            RequestContextHolder.getRequestAttributes();
//从session里面获取对应的值
String str = (String) requestAttributes.getAttribute("name",RequestAttributes.SCOPE_SESSION);

HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
HttpServletResponse response = ((ServletRequestAttributes)requestAttributes).getResponse();

看到这一般都会想到几个问题:

request和response怎么和当前请求挂钩?
request和response等是什么时候设置进去的?

2.解决疑问

2.1 request和response怎么和当前请求挂钩?

首先分析RequestContextHolder这个类,里面有两个ThreadLocal保存当前线程下的request,关于ThreadLocal可以参考这篇博文
/p/5675690b351e
后面详细的结果可以看/zzy7075/article/details/53559902

有了上面两个基础知识我们就可以完成这个功能了

1.先配置切面

<aop:config>
    <aop:aspect  ref="operationRecordAspect">
        <aop:pointcut  expression="execution(* .*.*.*(..))"/>
        <aop:around pointcut-ref="recordTask" method="aroundMethod"/>
    </aop:aspect>
</aop:config> 

2.写切面类

public class OperationRecordAspect {

    private static final Logger logger = ();

    @Autowired
    private IOperationRecordService OperationRecordService;

    public Object aroundMethod(ProceedingJoinPoint pjd) {


        Object result = null;
        //String methodName = ().getName();
        //获取request
        HttpServletRequest request = ((ServletRequestAttributes)()).getRequest();

        //请求url
        String url = ();
        if((url)){
            try {
                result =  ();
                if(("get")||("to")||("search")){
                    return result;
                }
            } catch (Throwable throwable) {
                (());
            }
        }

        ("拦截了员工从操作,url是:"+url);
        ("返回的结果是"+result);

        //("The method " + methodName + " begins with " + (()));
        //返回通知
        //("The method " + methodName + " ends with " + result);


        //参数列表
        String param = null;
        Map<String, String[]> parameterMap = ();
        if(parameterMap!=null&& ()>0 ){
            param = (parameterMap);
            if((param)){
                ("请求中的参数param"+param);
            }
        }

        //操作人
        Employee employee = (Employee)().getAttribute("employee");
        String employeeName = null;
        String loginName = null;
        if(employee!=null){
            loginName = ();
            employeeName = ();
            ("操作员工的登录名:"+loginName+"操作员工的姓名:"+employeeName);
        }

        OperationRecord operationRecord = new OperationRecord();
        (loginName);
        (employeeName);
        (url);
        (new Date());
        (param);
        (());

        (operationRecord);
        return result;
    }
}

这样就实现AOP对SpringMVC controller的拦截,当然,我们的项目是统一管理Exception,所有没有对Exception进行拦截,如果有需要也可以配置