zl程序教程

您现在的位置是:首页 >  其他

当前栏目

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

静态资源 导致 拦截器 拦截 springboot2.0 使用
2023-09-27 14:25:37 时间

在spring1.0+的版本中,配置拦截器后是不会拦截静态资源的。其配置如下:

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private RememberAuthenticationInterceptor rememberAuthenticationInterceptor;

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

 

但是在使用spring2.0+时,配置拦截器之后,就会拦截静态资源访问,此时我们需要用对应版本的方式去解决,如下:

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

此处要实现的接口是WebMvcConfigurer