在Spring Boot2.0+的版本中,只要用戶自定義了攔截器,則靜態資源會被攔截。但是在spring1.0+的版本中,是不會攔截靜態資源的。
因此,在使用Spring Boot2.0+時,配置攔截器之后,我們要把靜態資源的路徑加入到不攔截的路徑之中。
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor());
registration.addPathPatterns("/**"); //所有路徑都被攔截
registration.excludePathPatterns("/","/login","/error","/static/**","/qwe/**"); //添加不攔截路徑
}
}
注意,要實現的接口是WebMvcConfigurer。
不攔截路徑的寫法是“/static/”。網上其他寫法,比如/js/ , /static/js/**, 嘗試過都沒有效果,可能是因為spring Boot2.0的願意把
在application.yml中這可以配置靜態資源 不過要在上面增加不攔截 才會生效
mvc:
static-path-pattern: /qwe/**