定义拦截器
拦截器:所谓拦截器,就是能够在进行某个操作之前拦截请求,如果请求符合条件就允许在往下执行。比如说,海关就是一个拦截器,他拦截进出口的货物,如果货物满足进出口条件,则放行,否则就拦截,退回处理。
定义拦截器的几种方式:
实现HandleInterceptor接口
自定义拦截器类实现HandleInterceptor接口,并使用@Component注解标注为一个组件。
- public class MySelfInterceptor implements HandlerInterceptor {
- @Override
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
- System.out.println("在业务处理器处理请求之前被调用");
- return true;
- }
-
- @Override
- public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
- System.out.println("在业务处理器处理请求执行完成后,生成视图之前执行");
- }
-
- @Override
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
- System.out.println("在DispatcherServlet完全处理完请求后被调用");
- }
- }
-
根据三种情况,可以在不同的方法针对请求进行额外的处理。
在preHandle中,可以进行权限校验,安全控制。
在postHandle中,可以对返回来的ModelAndView进行处理,这个时候还未渲染视图。
在afterCompletion中,请求已经完成,页面已经渲染,数据已经返回。这个时候可以做一些资源清理,或者记录请求调用时间,做性能监控
继承HandleInterceptorAdapter类
自定义拦截器类继承HandleInterceptor接口的实现类HandleInterceptorAdapter来定义,并使用@Component注解标注为一个组件。建议使用此方式可以根据需要覆盖一些方法
- @Component
- public class MyInterceptor extends HandlerInterceptorAdapter {
-
- public SingleLoginInterceptor() {
- super();
- }
-
- @Override
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
- return super.preHandle(request, response, handler);
- }
-
- @Override
- public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
- super.postHandle(request, response, handler, modelAndView);
- }
-
- @Override
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
- super.afterCompletion(request, response, handler, ex);
- }
-
- @Override
- public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
- super.afterConcurrentHandlingStarted(request, response, handler);
- }
- }
可以看到HandlerInterceptorAdapter类底层是实现了HandlerInterceptor接口,多了两个方法,要比
实现HandlerInterceptor接口的方式功能强大。
这两个方法都是HandlerInterceptorAdapter类实现的org.springframework.web.servlet.AsyncHandlerInterceptor接口提供的,而AsyncHandlerInterceptor接口又继承了HandlerInterceptor接口,所以HandlerInterceptorAdapter底层是实现类HandlerInterceptor接口。
实现WebRequestInterceptor接口
自定义拦截器类实现WebRequestInterceptor接口,并使用@Component注解标注为一个组件。
- @Component
- public class MyInterceptor implements WebRequestInterceptor {
-
- @Override
- public void preHandle(WebRequest webRequest) throws Exception {
-
- }
-
- @Override
- public void postHandle(WebRequest webRequest, ModelMap modelMap) throws Exception {
-
- }
-
- @Override
- public void afterCompletion(WebRequest webRequest, Exception e) throws Exception {
-
- }
- }
两个实现接口方式的异同点 相同点 都可以实现controller层的拦截请求 不同点
1.WebRequestInterceptor的入参WebRequest是包装了HttpServletRequest 和HttpServletResponse的,通过WebRequest获取Request中的信息更简便。
2.WebRequestInterceptor的preHandle是没有返回值的,说明该方法中的逻辑并不影响后续的方法执行,所以这个接口实现就是为了获取Request中的信息,或者预设一些参数供后续流程使用。
3.HandlerInterceptor的功能更强大也更基础,可以在preHandle方法中就直接拒绝请求进入controller方法。
4.使用场景:这个在上条已经说了,如果想更方便获取HttpServletRequest的信息就使用WebRequestInterceptor,当然这些HandlerInterceptor都能做,只不过要多写点代码
实现RequestInterceptor接口
自定义类实现RequestInterceptor接口,此方式为微服务Feign调用的自定义拦截器,实现各个微服务之间的参数传递。
- @Configuration
- public class CenterinsRequestInterceptor implements RequestInterceptor {
-
- @Override
- public void apply(RequestTemplate requestTemplate) {
-
- }
- }
小插曲 @Configuration和@Component区别
这里使用了@Configuration注解而不是@Component,其实@Configuration本质上也是一个@Component,只不过前者描述的类中所有用@Bean标记的方法都会由CGLIB动态代理执行,在首次调用的时候会执行,然后把执行结果放到Spring上下文,之后对该方法的调用都是从Spring上下文上取的结果,所以都是指同一个实例。而使用了@Component,所有用@Bean标记的方法都是纯Java调用,每次都是生成不同的实例对象。如果要让使用了@Compnent注解的类中其@Bean标记的方法生成都是同一个实例,只需要使用@AutoWired标记属性,自动注入即可。
- @Configuration
- public class MyBeanConfig {
-
- @Bean
- public Country country(){
- return new Country();
- }
-
- @Bean
- public UserInfo userInfo(){
- return new UserInfo(country());
- }
-
- }
-
- // 以上代码 等同于以下代码
- // 以下代码去掉Autowired注入,则country方法是不同的实例对象。
-
- @Component
- public class MyBeanConfig {
-
- @Autowired
- private Country country;
-
- @Bean
- public Country country(){
- return new Country();
- }
-
- @Bean
- public UserInfo userInfo(){
- return new UserInfo(country);
- }
-
- }
注册拦截器
注册拦截器在springboot中直接使用注解便可实现。
继承WebMvcConfigurerAdapter类
1.创建一个自定义类,继承WebMvcConfigurerAdapter类重写addInterceptors方法。
- @Configuration
- public class WebCofiguration extends WebMvcConfigurerAdapter {
- public void addInterceptors(InterceptorRegistry registry) {
- // 将自己定义的拦截器注入进来进行拦截操作
- registry.addInterceptor(new MySelfInterceptor ())
- .addPathPatterns("/**")
- .excludePathPatterns("/logout");
- //过滤器可以添加多个,这里的addPathPatterns的/**是对所有的请求都做拦截。
- //excludePathPatterns代表排除url的拦截路径,即不拦截
- }
- }
此类在SpringBoot2.0以后已经废除,但仍可使用。推荐使用以下两种方式来代替此方式。
继承WebMvcConfigurationSupport类
2.创建一个自定义类继承WebMvcConfigurationSupport类,实现addInterceptors。
- @Configuration
- public class MyInterceptorConfig extends WebMvcConfigurationSupport {
-
- @Override
- protected void addInterceptors(InterceptorRegistry registry) {
- registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
- super.addInterceptors(registry);
- }
- }
此方式会导致默认的静态资源被拦截,这就需要我们手动将静态资源放开。
除了重写方法外还需要重写addResourceHandlers方法来释放静态资源
- @Override
- protected void addResourceHandlers(ResourceHandlerRegistry registry) {
- registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
- super.addResourceHandlers(registry);
- }
实现WebMvcConfigurer接口
3.创建一个自定义类实现WebMvcConfigurer接口,重写addInterceptors方法。
- @Configuration
- public class MyInterceptorConfig implements WebMvcConfigurer {
- @Override
- public void addInterceptors(InterceptorRegistry registry) {
- // 实现WebMvcConfigurer不会导致静态资源被拦截
- registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
- }
- }
此方式不会拦截静态资源
应用场景
由于这两种方式的不同:
继承 WebMvcConfigurationSupport类的方式推荐用在前后端分离的项目中,后台不需要访问静态资源(就不需要放开静态资源了),当然也可以在前后端不分离中,如果需要访问静态资源使用上面的方式重写addResourceHandlers方法即可;
实现 WebMvcConfigure接口的方式推荐用在非前后端分离的项目中,因为需要读取一些图片、css、js文件等等,当然也可以使用在前后端分离项目。
拦截器执行流程
单个拦截器
对单个拦截器执行流程解释如下:
程序首先会执行拦截器类中的preHandle()方法,如果该方法返回true,则程序会继续向下执行处理器中的方法,否则程序将不再往下继续执行。在业务处理器(即控制器Controller类)处理完请求后,会执行postHandle()方法,然后会通过DispatcherServlet向客户端返回响应,在DispatcherServlet处理完请求后,才会执行afterCompletion()方法。
因此单个拦截器的执行流程:
prehandle()——Handle(也就是控制器里的方法)——postHandle()——afterCompletion()。
多个拦截器
在大型的企业级项目中,通常会配置多个拦截器来实现不同的功能。例如我们自定义了三个拦截器A、B、C。并将它们都注册到同一个拦截器配置类中,如下图
- @Configuration
- public class InterceptorConfig implements WebMvcConfigurer {
-
- @Override
- public void addInterceptors(InterceptorRegistry registry) {
- registry.addInterceptor(new AInterceptor())
- .addPathPatterns("/**");
-
- registry.addInterceptor(new BInterceptor())
- .addPathPatterns("/**");
-
- registry.addInterceptor(new CInterceptor())
- .addPathPatterns("/**");
- }
- }
它们的preHandle()方法会按照配置文件中拦截器的配置顺序执行,而它们的postHandle()和afterCompletion()方法则会按照配置顺序的反序执行。
因此当前我们的三个拦截器执行顺序如下:
preHandleA——preHandleB——preHandleC——Handle——postHandleC——postHandleB——postHandleA——afterCompletionC——afterCompletionB——afterCompletionA
总结
到此这篇关于SpringBoot拦截器的使用介绍的文章就介绍到这了,更多相关SpringBoot拦截器内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!