spring boot 2.x拦截器导致静态资源404终极解决办法
首先添加application文件static路径,我的是yml文件
spring:
mvc:
static-path-pattern: /static/**
然后注册拦截器类如下方法;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){
registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/")
.addResourceLocations("classpath:/static/");
super.addResourceHandlers(registry);
}
上图标红的路径必须写成这样。
如需开放一个拦截如下配置,这也是坑,老是找不到静态资源,后来这样写可以了。
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/login","/boo","registry","/css/**","/js/**","/img/**");
}