springboot2.0+ 使用攔截器導致靜態資源被攔截


在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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM