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/**”路径外的所有路径都会被拦截,这样就解决了静态资源被拦截器拦截而导致页面没有样式的问题
个人博客地址:https://alexaccele.github.io/