springboot2.0+ 使用拦截器导致静态资源被拦截
文章目录
在spring1.0+的版本中,配置拦截器后是不会拦截静态资源的。其配置如下:
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Autowired
private RememberAuthenticationInterceptor rememberAuthenticationInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(rememberAuthenticationInterceptor)
.excludePathPatterns("/static/**")
.addPathPatterns("/**");
}
}
但是在使用spring2.0+时,配置拦截器之后,就会拦截静态资源访问,此时我们需要用对应版本的方式去解决,如下:
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/static/**");
}
}
此处要实现的接口是WebMvcConfigurer。
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论