今天写了个拦截器对一些mapping做了些处理,写完之后突然很想看看拦截器是怎么加进spring里面。对着源码debug了一遍。又有了新的收获。

1.拦截器的实现

  1.实现HandlerInterceptor

  1. public class MyHandlerInterceptor implements HandlerInterceptor {
  2. @Override
  3. public boolean preHandle(HttpServletRequest request,
  4. HttpServletResponse response,
  5. Object handler) throws Exception {
  6. System.out.println("---------preHandle--------");
  7. return true;
  8. }
  9.  
  10. /**
  11. * controller执行之后,且页面渲染之前调用
  12. * @param request
  13. * @param response
  14. * @param handler
  15. * @param modelAndView
  16. * @throws Exception
  17. */
  18. @Override
  19. public void postHandle(HttpServletRequest request,
  20. HttpServletResponse response,
  21. Object handler,
  22. ModelAndView modelAndView) throws Exception {
  23. System.out.println("---------postHandle--------");
  24. }
  25.  
  26. /**
  27. * 页面渲染之后调用,一般用于资源清理操作
  28. * @param request
  29. * @param response
  30. * @param handler
  31. * @param ex
  32. * @throws Exception
  33. */
  34. @Override
  35. public void afterCompletion(HttpServletRequest request,
  36. HttpServletResponse response,
  37. Object handler,
  38. Exception ex) throws Exception {
  39. System.out.println("---------afterCompletion--------");
  40. }

  2.将拦截器加入到拦截链里面去,这里可以实现

  1. WebMvcConfigurer

也可以继承

  1. WebMvcConfigurerAdapter

只是 WebMvcConfigurerAdapter这个类在Springboot2.0已经 Deprecated了,这部分内容我们后面再讲

  1. @Component
  2. public class MyWebMvcConfigurerAdapter implements WebMvcConfigurer {
  3. @Override
  4. public void addInterceptors(InterceptorRegistry registry) {
  5. registry.addInterceptor(new MyHandlerInterceptor());
  6. }
  7. }

接下来我们看看拦截是怎么被调用的,在 preHandle方法打断点

我们发现拦截器的获取在 org.springframework.web.servlet.HandlerExecutionChain#applyPreHandle 方法

  1. boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {
  2. HandlerInterceptor[] interceptors = getInterceptors();
  3. if (!ObjectUtils.isEmpty(interceptors)) {
  4. for (int i = 0; i < interceptors.length; i++) {
  5. HandlerInterceptor interceptor = interceptors[i];
  6. if (!interceptor.preHandle(request, response, this.handler)) {
  7. triggerAfterCompletion(request, response, null);
  8. return false;
  9. }
  10. this.interceptorIndex = i;
  11. }
  12. }
  13. return true;
  14. }

这里的 getInterceptors 如下所示

  1. public HandlerInterceptor[] getInterceptors() {
  2. if (this.interceptors == null && this.interceptorList != null) {
  3. this.interceptors = this.interceptorList.toArray(new HandlerInterceptor[0]);
  4. }
  5. return this.interceptors;
  6. }

那现在的问题就是要找到 interceptors是怎么初始化的呢。我们找到了HandlerExecutionChain的构造方法,发现interceptors就是在这赋值的

  1. public HandlerExecutionChain(Object handler, @Nullable HandlerInterceptor... interceptors) {
  2. if (handler instanceof HandlerExecutionChain) {
  3. HandlerExecutionChain originalChain = (HandlerExecutionChain) handler;
  4. this.handler = originalChain.getHandler();
  5. this.interceptorList = new ArrayList<>();
  6. CollectionUtils.mergeArrayIntoCollection(originalChain.getInterceptors(), this.interceptorList);
  7. CollectionUtils.mergeArrayIntoCollection(interceptors, this.interceptorList);
  8. }
  9. else {
  10. this.handler = handler;
  11. this.interceptors = interceptors;
  12. }
  13. }

再在这打个断点,找到了调用这个构造方法的类

  1. org.springframework.web.servlet.handler.AbstractHandlerMapping#getHandlerExecutionChain
    protected HandlerExecutionChain getHandlerExecutionChain(Object handler, HttpServletRequest request) {
  2. HandlerExecutionChain chain = (handler instanceof HandlerExecutionChain ?
  3. (HandlerExecutionChain) handler : new HandlerExecutionChain(handler));
  4.  
  5. String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
  6. for (HandlerInterceptor interceptor : this.adaptedInterceptors) {
  7. if (interceptor instanceof MappedInterceptor) {
  8. MappedInterceptor mappedInterceptor = (MappedInterceptor) interceptor;
  9. if (mappedInterceptor.matches(lookupPath, this.pathMatcher)) {
  10. chain.addInterceptor(mappedInterceptor.getInterceptor());
  11. }
  12. }
  13. else {
  14. chain.addInterceptor(interceptor);
  15. }
  16. }
  17. return chain;
  18. }

看到 AbstractHandlerMapping 差不多就知道是怎么一回事情了,这里再把调用的代码贴出来

  1. public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
  2. Object handler = getHandlerInternal(request);
  3. if (handler == null) {
  4. handler = getDefaultHandler();
  5. }
  6. if (handler == null) {
  7. return null;
  8. }
  9. // Bean name or resolved handler?
  10. if (handler instanceof String) {
  11. String handlerName = (String) handler;
  12. handler = obtainApplicationContext().getBean(handlerName);
  13. }
  14.  
  15. HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request);
  16. if (CorsUtils.isCorsRequest(request)) {
  17. CorsConfiguration globalConfig = this.globalCorsConfigSource.getCorsConfiguration(request);
  18. CorsConfiguration handlerConfig = getCorsConfiguration(handler, request);
  19. CorsConfiguration config = (globalConfig != null ? globalConfig.combine(handlerConfig) : handlerConfig);
  20. executionChain = getCorsHandlerExecutionChain(request, executionChain, config);
  21. }
  22. return executionChain;
  23. }

这个getHandler方法 其实就是RequestMapping注解调用的地方,这里的handle可以想象成是一个controller,getHandlerExecutionChain 这个方法的作用就是给我们的controller加上一层拦截器的属性,从HandlerExecutionChain的构造方法也能看出,HandlerExecutionChain 就是 handle和interceptor的封装。

到这里,我们大概是知道了拦截器是怎么被调用的。但是,我们还不知道拦截器是怎么被加载进spring的呢?

这里我们将重点放在 getHandlerExecutionChain 的  this.adaptedInterceptors 属性

我们找到了这个方法

  1. protected void initInterceptors() {
  2. if (!this.interceptors.isEmpty()) {
  3. for (int i = 0; i < this.interceptors.size(); i++) {
  4. Object interceptor = this.interceptors.get(i);
  5. if (interceptor == null) {
  6. throw new IllegalArgumentException("Entry number " + i + " in interceptors array is null");
  7. }
  8. this.adaptedInterceptors.add(adaptInterceptor(interceptor));
  9. }
  10. }
  11. }

这里的interceptor又是从interceptors获取而来,interceptors 的初始化是通过以下代码

  1. public void setInterceptors(Object... interceptors) {
  2. this.interceptors.addAll(Arrays.asList(interceptors));
  3. }

我们在这里打个断点,最终找到了

  1. org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#requestMappingHandlerMapping
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
  2. RequestMappingHandlerMapping mapping = createRequestMappingHandlerMapping();
  3. mapping.setOrder(0);
  4. mapping.setInterceptors(getInterceptors());
  5. mapping.setContentNegotiationManager(mvcContentNegotiationManager());
  6. mapping.setCorsConfigurations(getCorsConfigurations());
  1. protected final Object[] getInterceptors() {
  2. if (this.interceptors == null) {
  3. InterceptorRegistry registry = new InterceptorRegistry();
  4. addInterceptors(registry);
  5. registry.addInterceptor(new ConversionServiceExposingInterceptor(mvcConversionService()));
  6. registry.addInterceptor(new ResourceUrlProviderExposingInterceptor(mvcResourceUrlProvider()));
  7. this.interceptors = registry.getInterceptors();
  8. }
  9. return this.interceptors.toArray();
  10. }

还记最开始我们说的 将拦截器加入到拦截链里面的方法么。就是在这里调用的。

  1. public class MyWebMvcConfigurerAdapter implements WebMvcConfigurer {
  2. @Override
  3. public void addInterceptors(InterceptorRegistry registry) {
  4. registry.addInterceptor(new MyHandlerInterceptor());
  5. }
  6. }

到这里我们大概的就知道了拦截器是怎么加入spring的。还剩最后一个问题,requestMappingHandlerMapping 是由怎么触发的呢?

我们找到了方法的调用

  1. org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.EnableWebMvcConfiguration#requestMappingHandlerMapping    
  2.  
  3. @Bean
  4. @Primary
  5. @Override
  6. public RequestMappingHandlerMapping requestMappingHandlerMapping() {
  7. // Must be @Primary for MvcUriComponentsBuilder to work
  8. return super.requestMappingHandlerMapping();
  9. }

这个方法在 WebMvcAutoConfiguration里面,看到这个类名就知道这是个自动配置类。那么他一定和@EnableAutoconfigure 注解有关。我在 org/springframework/boot/spring-boot-autoconfigure/2.0.4.RELEASE/spring-boot-autoconfigure-2.0.4.RELEASE.jar!/META-INF/spring.factories   这个文件里面找到了AutoConfig的配置。所以 requestMappingHandlerMapping 是通过springboot自动配置扫描bean加载的。

  1. # Auto Configure
  2. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  3. org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\

最后 我们再看下 WebMvcAutoConfiguration这个类的几个注解

  1. @Configuration
  2. @ConditionalOnWebApplication(type = Type.SERVLET)
  3. @ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
  4. @ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
  5. @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
  6. @AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
  7. ValidationAutoConfiguration.class })
  8. public class WebMvcAutoConfiguration {

ConditionalOnMissingBean这个注解表明 只有不存在 WebMvcConfigurationSupport 这个bean才可以配置加载,所以这也是为什么我们在将拦截器加入到拦截链里面的方法里面是实现 WebMvcConfigurer 而不是继承WebMvcConfigurationSupport。

最后我们再总结下

1.项目启动的时候springboot会自动扫描相关配置类触发requestMappingHandlerMapping方法

2.requestMappingHandlerMapping会将系统的各个拦截添加到拦截器数组中,真正的http请求过来后会调用getHandler方法将过滤器和handle封装成HandlerExecutionChain。

3.按照过滤器添加顺序依次执行过滤器

以上,就是对拦截器的分析


转载请注明出处   https://www.cnblogs.com/xmzJava/p/9550535.html

Springboot 拦截器的背后的更多相关文章

  1. Java结合SpringBoot拦截器实现简单的登录认证模块

    Java结合SpringBoot拦截器实现简单的登录认证模块 之前在做项目时需要实现一个简单的登录认证的功能,就寻思着使用Spring Boot的拦截器来实现,在此记录一下我的整个实现过程,源码见文章 ...

  2. SpringBoot拦截器中Bean无法注入(转)

    问题 这两天遇到SpringBoot拦截器中Bean无法注入问题.下面介绍我的思考过程和解决过程: 1.由于其他bean在service,controller层注入一点问题也没有,开始根本没意识到Be ...

  3. 【SpringBoot】SpringBoot拦截器实战和 Servlet3.0自定义Filter、Listener

    =================6.SpringBoot拦截器实战和 Servlet3.0自定义Filter.Listener ============ 1.深入SpringBoot2.x过滤器Fi ...

  4. SpringBoot拦截器中无法注入bean的解决方法

    SpringBoot拦截器中无法注入bean的解决方法 在使用springboot的拦截器时,有时候希望在拦截器中注入bean方便使用 但是如果直接注入会发现无法注入而报空指针异常 解决方法: 在注册 ...

  5. Springboot拦截器未起作用

    之前遇到要使用springboot拦截器却始终未生效的状况,查了网上的博客,大抵都是@Component,@Configuration注解未加,或是使用@ComponentScan增加包扫描,但是尝试 ...

  6. SpringBoot拦截器中service或者redis注入为空的问题

    原文:https://my.oschina.net/u/1790105/blog/1490098 这两天遇到SpringBoot拦截器中Bean无法注入问题.下面介绍我的思考过程和解决过程: 1.由于 ...

  7. springboot + 拦截器 + 注解 实现自定义权限验证

    springboot + 拦截器 + 注解 实现自定义权限验证最近用到一种前端模板技术:jtwig,在权限控制上没有用springSecurity.因此用拦截器和注解结合实现了权限控制. 1.1 定义 ...

  8. Springboot 拦截器配置(登录拦截)

    Springboot 拦截器配置(登录拦截) 注意这里环境为springboot为2.1版本 1.编写拦截器实现类,实现接口   HandlerInterceptor, 重写里面需要的三个比较常用的方 ...

  9. Springboot拦截器实现IP黑名单

    Springboot拦截器实现IP黑名单 一·业务场景和需要实现的功能 以redis作为IP存储地址实现. 业务场景:针对秒杀活动或者常规电商业务场景等,防止恶意脚本不停的刷接口. 实现功能:写一个拦 ...

随机推荐

  1. 微信小程序1

    本次项目主要了解及使用微信小程序,以及更好的理解微信动画,wxss,JavaScript,ajax,xml等技术: 借助的平台是java后端设计语言以及微信小程序界面,设计 该系统分为1,个人用户端: ...

  2. NOIP2006普及组 Jam的计数法

    普及组重要的模拟题.附上题目链接 https://www.luogu.org/problem/show?pid=1061 (写水题题解算是巩固提醒自己细心吧qwq) 样例输入: bdfij 样例输出: ...

  3. 利用jquery-barcode.js实现生成条形码

    jquery-barcode官网 js下载地址-github 代码示范(官网上也有) <!DOCTYPE html> <html> <head> <meta ...

  4. VMware ESXi 6.5 安装

    1.1下载esxi镜像 此处我使用的版本是:VMware-VMvisor-Installer-6.5.0-4564106.x86_64 1.2新建一个虚拟机,硬件兼容性处选择ESXI6.5 硬盘40g ...

  5. Linux一键安装宝塔控制面板

    Linux一键安装宝塔的命令行 yum install -y wget && wget -O install.sh http://download.bt.cn/install/inst ...

  6. 判断js中的数据类型的几种方法

    判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...

  7. K8S 安装 Wordpress

    基本概念 Helm 可以理解为 Kubernetes 的包管理工具,可以方便地发现.共享和使用为Kubernetes构建的应用,它包含几个基本概念 Helm是目前Kubernetes服务编排领域的唯一 ...

  8. AWS MVC 详解

    由于新工作是在AWS PaaS平台上进行开发,为不耽误工作,先整理一下AWS MVS的使用规范,快速上手.对AWS PaaS平台的相关介绍留到以后再来补充.本文几乎是对官方学习文档的整理,有遗漏的后补 ...

  9. C#转发Post请求,包括参数和文件

    /// <summary> /// 转发Post请求 /// </summary> /// <param name="curRequest">要 ...

  10. Conda命令指标

    一.Conda相关指令 # 查看当前环境下已安装的包 conda list # 查看某个指定环境的已安装包 conda list -n tensorflow # 查找package信息 conda s ...