码迷,mamicode.com
首页 > 编程语言 > 详细

springboot 2.0+ 自定义拦截器 静态资源问题

时间:2018-09-03 12:12:15      阅读:367      评论:0      收藏:0      [点我收藏+]

标签:ons   apt   ash   自动   html   style   boot   clu   code   

之前项目的springboot自定义拦截器使用的是继承WebMvcConfigurerAdapter重写常用方法的方式来实现的.静态文件不需要进行放行,springboot会自动帮你放行。

springboot2.0之后如果想要自定义的话就不可以了,需要手动放行静态资源。此处我是实现了WebMvcConfigurer来自定义拦截器(根据需求也可以继承WebMvcConfigurationSupport,此处不再赘述)。下面是实现代码

@Configuration
public class MyMvcConfig implements  WebMvcConfigurer {

    //所有的WebMvcConfigurerAdapter组件都会一起起作用
    @Bean //将组件注册在容器
    public WebMvcConfigurer webMvcConfigurer(){
        WebMvcConfigurer adapter = new WebMvcConfigurer() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("login");
                registry.addViewController("/index.html").setViewName("login");
                registry.addViewController("/main.html").setViewName("dashboard");
            }

            //注册拦截器
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                //super.addInterceptors(registry);
                //静态资源;  *.css , *.js
                //SpringBoot已经做好了静态资源映射
                registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("**")
                        .excludePathPatterns("/index.html","/","/hello1","/user/login")
                        .excludePathPatterns("/static/**");
            }
        };
        return adapter;
    }

}

注意:(大坑)此处的addPathPatterns("**")不要使用 “/**”,否则静态资源还是会被拦截

springboot 2.0+ 自定义拦截器 静态资源问题

标签:ons   apt   ash   自动   html   style   boot   clu   code   

原文地址:https://www.cnblogs.com/pigwood/p/9577426.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!