1. 程式人生 > >springboot2.0靜態資源被攔截器攔截的問題

springboot2.0靜態資源被攔截器攔截的問題

springboot2.0中如果採用以前的方式去配置攔截器,會發現靜態資源被攔截了。

首先在springboot2.0中WebMvcConfigurerAdapter類被棄用了,若是要想使用以往的功能,需要改為實現WebMvcConfigurer介面,並重寫addInterceptors()方法來

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    //新增攔截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
                .excludePathPatterns("/static/**");
    }
}
以上為springboot2.0的攔截器註冊方法,其中LoginHandlerInterceptor是我自定義的攔截器,以上路徑表示除了“/static/**”路徑外的所有路徑都會被攔截,這樣就解決了靜態資源被攔截器攔截而導致頁面沒有樣式的問題