经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
SpringBoot拦截器的使用介绍
来源:jb51  时间:2022/1/2 14:20:16  对本文有异议

定义拦截器

拦截器:所谓拦截器,就是能够在进行某个操作之前拦截请求,如果请求符合条件就允许在往下执行。比如说,海关就是一个拦截器,他拦截进出口的货物,如果货物满足进出口条件,则放行,否则就拦截,退回处理。

定义拦截器的几种方式:

实现HandleInterceptor接口

自定义拦截器类实现HandleInterceptor接口,并使用@Component注解标注为一个组件。

  1. public class MySelfInterceptor implements HandlerInterceptor {
  2. @Override
  3. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  4. System.out.println("在业务处理器处理请求之前被调用");
  5. return true;
  6. }
  7.  
  8. @Override
  9. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  10. System.out.println("在业务处理器处理请求执行完成后,生成视图之前执行");
  11. }
  12.  
  13. @Override
  14. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  15. System.out.println("在DispatcherServlet完全处理完请求后被调用");
  16. }
  17. }
  18.  

根据三种情况,可以在不同的方法针对请求进行额外的处理。
在preHandle中,可以进行权限校验,安全控制。
在postHandle中,可以对返回来的ModelAndView进行处理,这个时候还未渲染视图。
在afterCompletion中,请求已经完成,页面已经渲染,数据已经返回。这个时候可以做一些资源清理,或者记录请求调用时间,做性能监控

继承HandleInterceptorAdapter类

自定义拦截器类继承HandleInterceptor接口的实现类HandleInterceptorAdapter来定义,并使用@Component注解标注为一个组件。建议使用此方式可以根据需要覆盖一些方法

  1. @Component
  2. public class MyInterceptor extends HandlerInterceptorAdapter {
  3.  
  4. public SingleLoginInterceptor() {
  5. super();
  6. }
  7.  
  8. @Override
  9. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  10. return super.preHandle(request, response, handler);
  11. }
  12.  
  13. @Override
  14. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  15. super.postHandle(request, response, handler, modelAndView);
  16. }
  17.  
  18. @Override
  19. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  20. super.afterCompletion(request, response, handler, ex);
  21. }
  22.  
  23. @Override
  24. public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  25. super.afterConcurrentHandlingStarted(request, response, handler);
  26. }
  27. }

可以看到HandlerInterceptorAdapter类底层是实现了HandlerInterceptor接口,多了两个方法,要比
实现HandlerInterceptor接口的方式功能强大。
这两个方法都是HandlerInterceptorAdapter类实现的org.springframework.web.servlet.AsyncHandlerInterceptor接口提供的,而AsyncHandlerInterceptor接口又继承了HandlerInterceptor接口,所以HandlerInterceptorAdapter底层是实现类HandlerInterceptor接口。

实现WebRequestInterceptor接口

自定义拦截器类实现WebRequestInterceptor接口,并使用@Component注解标注为一个组件。

  1. @Component
  2. public class MyInterceptor implements WebRequestInterceptor {
  3. @Override
  4. public void preHandle(WebRequest webRequest) throws Exception {
  5. }
  6.  
  7. @Override
  8. public void postHandle(WebRequest webRequest, ModelMap modelMap) throws Exception {
  9.  
  10. }
  11.  
  12. @Override
  13. public void afterCompletion(WebRequest webRequest, Exception e) throws Exception {
  14.  
  15. }
  16. }

两个实现接口方式的异同点 相同点 都可以实现controller层的拦截请求 不同点
1.WebRequestInterceptor的入参WebRequest是包装了HttpServletRequest 和HttpServletResponse的,通过WebRequest获取Request中的信息更简便。
2.WebRequestInterceptor的preHandle是没有返回值的,说明该方法中的逻辑并不影响后续的方法执行,所以这个接口实现就是为了获取Request中的信息,或者预设一些参数供后续流程使用。
3.HandlerInterceptor的功能更强大也更基础,可以在preHandle方法中就直接拒绝请求进入controller方法。
4.使用场景:这个在上条已经说了,如果想更方便获取HttpServletRequest的信息就使用WebRequestInterceptor,当然这些HandlerInterceptor都能做,只不过要多写点代码

实现RequestInterceptor接口

自定义类实现RequestInterceptor接口,此方式为微服务Feign调用的自定义拦截器,实现各个微服务之间的参数传递。

  1. @Configuration
  2. public class CenterinsRequestInterceptor implements RequestInterceptor {
  3.  
  4. @Override
  5. public void apply(RequestTemplate requestTemplate) {
  6.  
  7. }
  8. }

小插曲 @Configuration和@Component区别

这里使用了@Configuration注解而不是@Component,其实@Configuration本质上也是一个@Component,只不过前者描述的类中所有用@Bean标记的方法都会由CGLIB动态代理执行,在首次调用的时候会执行,然后把执行结果放到Spring上下文,之后对该方法的调用都是从Spring上下文上取的结果,所以都是指同一个实例。而使用了@Component,所有用@Bean标记的方法都是纯Java调用,每次都是生成不同的实例对象。如果要让使用了@Compnent注解的类中其@Bean标记的方法生成都是同一个实例,只需要使用@AutoWired标记属性,自动注入即可。

  1. @Configuration
  2. public class MyBeanConfig {
  3. @Bean
  4. public Country country(){
  5. return new Country();
  6. }
  7. @Bean
  8. public UserInfo userInfo(){
  9. return new UserInfo(country());
  10. }
  11. }
  12.  
  13. // 以上代码 等同于以下代码
  14. // 以下代码去掉Autowired注入,则country方法是不同的实例对象。
  15. @Component
  16. public class MyBeanConfig {
  17. @Autowired
  18. private Country country;
  19. @Bean
  20. public Country country(){
  21. return new Country();
  22. }
  23. @Bean
  24. public UserInfo userInfo(){
  25. return new UserInfo(country);
  26. }
  27. }

注册拦截器

注册拦截器在springboot中直接使用注解便可实现。

继承WebMvcConfigurerAdapter类

 1.创建一个自定义类,继承WebMvcConfigurerAdapter类重写addInterceptors方法。

  1. @Configuration
  2. public class WebCofiguration extends WebMvcConfigurerAdapter {
  3. public void addInterceptors(InterceptorRegistry registry) {
  4. // 将自己定义的拦截器注入进来进行拦截操作
  5. registry.addInterceptor(new MySelfInterceptor ())
  6. .addPathPatterns("/**")
  7. .excludePathPatterns("/logout");
  8. //过滤器可以添加多个,这里的addPathPatterns的/**是对所有的请求都做拦截。
  9. //excludePathPatterns代表排除url的拦截路径,即不拦截
  10. }
  11. }

此类在SpringBoot2.0以后已经废除,但仍可使用。推荐使用以下两种方式来代替此方式。

继承WebMvcConfigurationSupport类

 2.创建一个自定义类继承WebMvcConfigurationSupport类,实现addInterceptors。

  1. @Configuration
  2. public class MyInterceptorConfig extends WebMvcConfigurationSupport {
  3.  
  4. @Override
  5. protected void addInterceptors(InterceptorRegistry registry) {
  6. registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
  7. super.addInterceptors(registry);
  8. }
  9. }

此方式会导致默认的静态资源被拦截,这就需要我们手动将静态资源放开。
除了重写方法外还需要重写addResourceHandlers方法来释放静态资源

  1. @Override
  2. protected void addResourceHandlers(ResourceHandlerRegistry registry) {
  3. registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
  4. super.addResourceHandlers(registry);
  5. }

实现WebMvcConfigurer接口

 3.创建一个自定义类实现WebMvcConfigurer接口,重写addInterceptors方法。

  1. @Configuration
  2. public class MyInterceptorConfig implements WebMvcConfigurer {
  3. @Override
  4. public void addInterceptors(InterceptorRegistry registry) {
  5. // 实现WebMvcConfigurer不会导致静态资源被拦截
  6. registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
  7. }
  8. }

此方式不会拦截静态资源

应用场景

由于这两种方式的不同:
继承 WebMvcConfigurationSupport类的方式推荐用在前后端分离的项目中,后台不需要访问静态资源(就不需要放开静态资源了),当然也可以在前后端不分离中,如果需要访问静态资源使用上面的方式重写addResourceHandlers方法即可;
实现 WebMvcConfigure接口的方式推荐用在非前后端分离的项目中,因为需要读取一些图片、css、js文件等等,当然也可以使用在前后端分离项目。

拦截器执行流程

单个拦截器

对单个拦截器执行流程解释如下:
程序首先会执行拦截器类中的preHandle()方法,如果该方法返回true,则程序会继续向下执行处理器中的方法,否则程序将不再往下继续执行。在业务处理器(即控制器Controller类)处理完请求后,会执行postHandle()方法,然后会通过DispatcherServlet向客户端返回响应,在DispatcherServlet处理完请求后,才会执行afterCompletion()方法。
因此单个拦截器的执行流程:
prehandle()——Handle(也就是控制器里的方法)——postHandle()——afterCompletion()。

多个拦截器

在大型的企业级项目中,通常会配置多个拦截器来实现不同的功能。例如我们自定义了三个拦截器A、B、C。并将它们都注册到同一个拦截器配置类中,如下图

  1. @Configuration
  2. public class InterceptorConfig implements WebMvcConfigurer {
  3.  
  4. @Override
  5. public void addInterceptors(InterceptorRegistry registry) {
  6. registry.addInterceptor(new AInterceptor())
  7. .addPathPatterns("/**");
  8.  
  9. registry.addInterceptor(new BInterceptor())
  10. .addPathPatterns("/**");
  11.  
  12. registry.addInterceptor(new CInterceptor())
  13. .addPathPatterns("/**");
  14. }
  15. }

它们的preHandle()方法会按照配置文件中拦截器的配置顺序执行,而它们的postHandle()和afterCompletion()方法则会按照配置顺序的反序执行。
因此当前我们的三个拦截器执行顺序如下:
preHandleA——preHandleB——preHandleC——Handle——postHandleC——postHandleB——postHandleA——afterCompletionC——afterCompletionB——afterCompletionA

总结

到此这篇关于SpringBoot拦截器的使用介绍的文章就介绍到这了,更多相关SpringBoot拦截器内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号