1 @Configuration 2 public class MyBlogWebMvcConfigurer extends WebMvcConfigurerAdapter { 3 4 @Autowired 5 private AdminLoginInterceptor adminLoginInterceptor; 6 7 public void addInterceptors(InterceptorRegistry registry) { 8 // 添加一个拦截器,拦截以/admin为前缀的url路径 9 registry.addInterceptor(adminLoginInterceptor).addPathPatterns("/admin/**").excludePathPatterns("/admin/login").excludePathPatterns("/admin/dist/**").excludePathPatterns("/admin/plugins/**"); 10 super.addInterceptors(registry); 11 } 12 13 public void addResourceHandlers(ResourceHandlerRegistry registry) { 14 registry.addResourceHandler("/upload/**").addResourceLocations("file:" + Constants.FILE_UPLOAD_DIC); 15 16 super.addResourceHandlers(registry); 17 18 } 19 }
还有直接在配置文件中配置
这样也可以映射到本地路径但是 static下面的文件 就访问不到了 我更喜欢使用第一种
resources: static-locations: file:${web.upload-path},/static/**