直接贴拦截器的配置文件
@Configuration
public class SpringMVCConfig extends WebMvcConfigurationSupport {
@Value("${absoluteImgPath}")
String absoluteImgPath;
@Value("${sonImgPath}")
String sonImgPath;
@Bean
public AuthInterceptor getAuthInterceptor() {
return new AuthInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry
.addInterceptor(getAuthInterceptor())
.addPathPatterns("/user/**")
.excludePathPatterns("/user/wx/login")
.excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**","/images/**");
super.addInterceptors(registry);
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
registry.addResourceHandler(sonImgPath + "**").addResourceLocations("file:"+absoluteImgPath);
super.addResourceHandlers(registry);
}
}
经过接下来的配置我们要达到这个效果:通过https://localhost:8011/images/test.jpg ,访问到“test.jpg”这个图片,而8011为该springboot进程的端口号,因此,关闭springboot进程就无法访问到images中的图片,或可以通过nginx访问。
- 如下图所示,1号圈指的是在拦截器中放行路径"/images/**“
- 2号圈中,
sonImgPath="/images/" ,是前端URL访问路径。absoluteImgPath="C:/images/" ,这个是图片真实保存的路径。这样配置后访问 /images/**这个路径就会去本地C://image/ 找对应的文件。 经过如上配置后,放一张名为test.jpg 的图片在C:/images/ 目录下,就可通过https://localhost:8011/images/test.jpg 来访问。
|