在Spring boot 中的应用中的三大拦截机制 Filter 、Interceptor 、Aspect
1、Filter
Filter功能:可以拿到原始的http请求,但是拿不到你请求的控制器和请求控制器中的方法的信息
Filter使用:
package com.llangzh.filter;import java.io.IOException;import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;import org.springframework.stereotype.Component;@Component
public class MyFilter implements Filter{@Overridepublic void init(FilterConfig filterConfig) throws ServletException {System.out.println("myFilter init........");}@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {System.out.println("执行任务前.................");chain.doFilter(request, response);System.out.println("执行任务后...........");}@Overridepublic void destroy() {System.out.println("Myfilter destory........");}}
测试:
。。。。。。。。。myFilter init........。。。。。。。。。doFilter 执行任务前...........
任务执行...........
doFilter 执行任务后...........
2、Interceptor
Interceptor作用:可以拿到你请求的控制器和方法,却拿不到请求方法的参数
Interceptor用法:
(1)先在配置类中配置Interceptor
package com.llangzh.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import com.llangzh.interceptor.MyInterceptor;@Configuration
public class interceptorConfig extends WebMvcConfigurerAdapter{@Autowiredprivate MyInterceptor myInterceptor;@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(myInterceptor);}
}
(2)使用Interceptor
package com.llangzh.interceptor;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;@Component
public class MyInterceptor implements HandlerInterceptor{@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {System.out.println("preHandler..........");System.out.println(((HandlerMethod)handler).getBean().getClass().getName());request.setAttribute("abc", "abc");return true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,ModelAndView modelAndView) throws Exception {System.out.println("postHandler...........");System.out.println(request.getAttribute("abc"));}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)throws Exception {System.out.println("afterHandler..........");System.out.println("ex is" + ex);}
}
测试:
preHandler..........
com.llangzh.controller.UserController
任务执行...........
postHandler...........
abc
afterHandler..........
ex isnull
3、Aspect
aspect作用:可以拿到方法的参数,但是却拿不到http请求和响应的对象
aspect用法:
package com.llangzh.aspect;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;@Aspect
@Component
public class MyAspect {//环绕方式@Around("execution(* com.llangzh.controller.UserController.*(..))")public Object handlerControllerMethod(ProceedingJoinPoint point) throws Throwable {System.out.println("MyAspect start............");Object[] args = point.getArgs();for (Object arg : args) {System.out.println(arg);}Object proceed = point.proceed();System.out.println("MyAspect end............");return proceed;}
}
测试:
MyAspect start............
1
任务执行...........
MyAspect end............
当Filter、Interceptor、Aspect一起存在的时候,测试结果为:
doFilter 执行任务前...........
preHandler..........
com.llangzh.controller.UserController$$EnhancerBySpringCGLIB$$a43b51b8
MyAspect start............
1
任务执行...........
MyAspect end............
postHandler...........
abc
afterHandler..........
ex isnull
doFilter 执行任务后...........
可以看到拦截过程为:Filter——>Interceptor——>Aspect