package com.ms.common.config; import com.ms.common.constant.KeyConstant; import com.ms.common.interceptor.AuthInterceptor; import com.ms.common.prop.Storage; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.*; import javax.annotation.Resource; /** * @author liu pei * @version 1.0.0 * @ClassName InterceptorConfig.java * @Description 拦截器 * @createTime 2022年11月22日 21:37:00 */ @Configuration public class InterceptorConfig implements WebMvcConfigurer { @Resource public AuthInterceptor authInterceptor; @Resource private Storage storage; @Override public void addInterceptors(InterceptorRegistry registry) { //过滤要登录的URL InterceptorRegistration addInterceptor = registry.addInterceptor(authInterceptor); addInterceptor.addPathPatterns("/**/**"); addInterceptor.excludePathPatterns(KeyConstant.EXCLUDE_PATH); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //+storage.getLocationsPath() storage.getResourcePath() registry.addResourceHandler("/img/**").addResourceLocations("file:F:/test/"); } @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTION") .allowedHeaders("*") .maxAge(86400); } }
问题:
1.是映射路径不对。
2.是本地路径不对,一定要(file:F:/test/)
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//+storage.getLocationsPath() storage.getResourcePath()
registry.addResourceHandler("/img/**").addResourceLocations("file:F:/test/");
}