SpringBoot项目配置HandlerInterceptorAdapter继承WebMvcConfigurationSupport导致自动化配置失效的解决方案

  • 相关背景描述
    Spring boot 版本:2.1.4.RELEASE
    项目开发过程中需要简单校验一下session的有效性,相关用户权限信息。我决定使用Spring mvc 的 HandlerInterceptorAdapter 拦截器在Spring boot 1.X版本中我们只需配置WebMvcConfigurerAdapter即可,Spring boot 2.x 开始,使用Spring 组件的版本都是5.x,WebMvcConfigurerAdapter在Spring5中已经被遗弃,查询资料发现需要使用新的WebMvcConfigurationSupport。使用WebMvcConfigurationSupport新的问题就来了,Spring mvc的自动配置就失效了,静态资源无法正常访问。我在本项目中还想继续使用Spring mvc 的自动配置,找出了解决方案。


  • 自定义拦截器代码

        public class MyHandlerInterceptorAdapter extends HandlerInterceptorAdapter {
          Logger log= LoggerFactory.getLogger(MyHandlerInterceptorAdapter.class);
    
          @Override
          public boolean preHandle(HttpServletRequest request,
                                   HttpServletResponse response, Object handler) throws Exception {
    
              log.info("==============执行顺序: 1、preHandle================");
              String requestUri = request.getRequestURI();
              String contextPath = request.getContextPath();
              String url = requestUri.substring(contextPath.length());
    
              log.info("requestUri:"+requestUri);
              log.info("contextPath:"+contextPath);
              log.info("url:"+url);
              Set<String> initUrl = FilterUrl.initUrl();
              if (initUrl.contains(url)) {
                  SysUser user =  (SysUser)request.getSession().getAttribute("USER_INFO");
                  if(user == null){
                      log.info("Interceptor:跳转到login页面!");
                      // /WEB-INF
                      // request.getRequestDispatcher("/sinylon.html").forward(request, response);
                      ObjectMapper mapper = new ObjectMapper();
                      Res res = new Res<>();
                      res.setRespCode(3);
                      res.setRespMsg("未登录/会话过期,请登录");
                      response.setContentType("text/json;charset=utf-8");
                      response.getWriter().print(mapper.writeValueAsString(res));
                      return false;
                  }else {
                      return true;
                  }
              }
    
              return true;
    
          }
      }
    

  • 使用WebMvcConfigurationSupport整合自定义拦截器代码
       @Configuration
       public class WebSecurityConfig extends WebMvcConfigurationSupport {
       @Bean
       public MyHandlerInterceptorAdapter getSecurityInterceptor() {
           return new MyHandlerInterceptorAdapter();
       }

       @Override
       public void addInterceptors(InterceptorRegistry registry) {

           registry.addInterceptor(getSecurityInterceptor()).addPathPatterns("/*");

           super.addInterceptors(registry);
           // 排除配置
           //addInterceptor.excludePathPatterns("/error");
           //addInterceptor.excludePathPatterns("/login**");

           // 拦截配置
          // addInterceptor.addPathPatterns("/**");
       }
   }

  • 启动程序,发现找不到静态资源,请求404

    image.png

打开浏览器,请求程序


image.png

控制台打印


image.png

由此发现静态资源无法正常访问,解决方案代码

   @Configuration
   public class WebSecurityConfig implements WebMvcConfigurer {

       @Bean
       public MyHandlerInterceptorAdapter getSecurityInterceptor() {
           return new MyHandlerInterceptorAdapter();
       }

       @Override
       public void addInterceptors(InterceptorRegistry registry) {
           registry.addInterceptor(getSecurityInterceptor());
       }
   }

将继承WebMvcConfigurationSupport改为实现WebMvcConfigurer重写添加拦截器的方法,重新启动程序


image.png

控制台日志与之前对比,现在打印出了静态资源位置,页面访问:


image.png

访问正常,控制台日志,静态资源正常访问


image.png

源码解析:


image.png

Spring boot 的 web 自动配置类WebMvcAutoConfiguration上有条件注解

 @ConditionalOnMissingBean({WebMvcConfigurationSupport.class})

这个注解的意思是在项目类路径中 缺少 WebMvcConfigurationSupport类型的bean时改自动配置类才会生效,所以继承 WebMvcConfigurationSupport 后需要自己再重写相应的方法。

spring 5.0后要使用Java8,而在Java8中接口是可以有default方法的,我们只需要在自定义配置类中直接实现 WebMvcConfigurer接口即可。

总结:

implements WebMvcConfigurer : 不会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置
@EnableWebMvc + implements WebMvcConfigurer : 会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置
extends WebMvcConfigurationSupport :会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置
extends DelegatingWebMvcConfiguration :会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置

参考连接:
相关博客,Spring boot梳理
相关博客,自动配置类分析

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容