记一次 SpringBoot 静态资源映射本地文件会被拦截的坑
application-dev.yaml 中配置本地路径#
1
| upload-path: F:\upload-server
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| @Configuration
public class WebConfig implements WebMvcConfigurer {
@Resource
private LoginInterceptor loginInterceptor;
@Value("${upload-path}")
String uploadPath;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginInterceptor)
.addPathPatterns("/**")
.excludePathPatterns("/swagger*/**", "/v2/api-docs");
WebMvcConfigurer.super.addInterceptors(registry);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// 映射本地上传文件夹
registry.addResourceHandler("/upload/**")
.addResourceLocations("file:" + uploadPath);
WebMvcConfigurer.super.addResourceHandlers(registry);
}
}
|
结果就是访问 /upload/*
会进入拦截器
解决办法就是 再本地路径后面一定要加上 \
1
| upload-path: F:\upload-server\
|