拦截器不拦截静态资源
拦截器代码
项目目录

@Configuration
public class WebAppConfig extends WebMvcConfigurationSupport {
@Bean
public HandlerInterceptor getMyInterceptor() {
return new WeChatLoginInterceptor();
}
@Bean
public HandlerInterceptor getLoginOutInterceptor() {
return new WeChatLoginInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry){
registry.addInterceptor(getMyInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/static/image/**");
super.addInterceptors(registry);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/","classpath:/");
}
}
注意 addInterceptors 方法里的 .excludePathPatterns("/static/image/**");
这里让拦截器不拦截 static/image
application.properties里什么都不用加
访问静态图片
localhost:8080/static/image/Aadd.png
本文介绍如何在Spring MVC项目中配置拦截器以排除静态资源如图片的拦截,通过示例代码展示如何在WebAppConfig类中使用excludePathPatterns方法实现此功能。
403

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



