springboot2.0+ 使用拦截器导致静态资源被拦截

在spring2.0+的版本中,只要用户自定义了拦截器,则静态资源会被拦截。但是在spring1.0+的版本中,是不会拦截静态资源的。

因此,在使用spring2.0+时,配置拦截器之后,我们要把静态资源的路径加入到不拦截的路径之中。

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
       registry.addInterceptor(new LoginInterceptor())
                                .addPathPatterns("**")
                                       .excludePathPatterns("/static/**");
    }
}  

注意,要实现的接口是WebMvcConfigurer。

不拦截路径的写法是“/static/**”。网上其他写法,比如/js/** , /static/js/**, 我试了之后均无法成功。






猜你喜欢

转载自blog.csdn.net/qq_34761108/article/details/80989075