在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。
本文详细解析了在Spring不同版本中配置拦截器的方法,特别是在Spring2.0+版本中如何正确配置以避免拦截静态资源,提供了具体的代码示例。
3665

被折叠的 条评论
为什么被折叠?



