- WebMvcConfigurerAdapter拦截器在springboot2.1之后被废除,在自定义拦截器的时候需要继承WebMvcConfigurationSupport类来重写addInterceptors()方法,添加我们自定义的拦截器。
- 关于配置自定义连接器之后静态资源无法访问的问题
需要在继承WebMvcConfigurationSupport类中重写addResourceHandlers()方法,如下:
package com.lhb.blog.interceptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class LoginConfig extends WebMvcConfigurationSupport { @Autowired private LoginInterceptor interceptor; @Override protected void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(interceptor).addPathPatterns("/admin/**") .excludePathPatterns("/admin/") .excludePathPatterns("/admin/login"); } /** * 配置静态资源访问权限 * @param registry */ @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { if(!registry.hasMappingForPattern("/webjars/**")){ registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } if (!registry.hasMappingForPattern("/**")){ registry.addResourceHandler("/**") .addResourceLocations("classpath:/META-INF/resources/") .addResourceLocations("classpath:/resources/") .addResourceLocations("classpath:/static/") .addResourceLocations("classpath:/public/"); } } }