Spring Boot实战:静态资源无法访问

发现  static 或 public 下面的图片无法访问

spring:
  profiles:
    active: dev

  resources:
    static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${vipsoft.file.path}

原因:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private AuthInterceptor authorizationInterceptor;


    /**
     * 如果重写了这个方法,yml 里面的 static-locations 将不生效
     */
   // @Override
   // public void addResourceHandlers(ResourceHandlerRegistry registry) {
   //     // 允许访问static文件
   //     registry.addResourceHandler("/**")
   //             .addResourceLocations("classpath:/resources/")
   //             .addResourceLocations("classpath:/static/")
   //             .addResourceLocations("classpath:/public/");
   //     //文件磁盘图片url映射
   //     //        registry.addResourceHandler("/file/**")
   //     //                .addResourceLocations("file:D://upload/");
   // }


    /**
     * 添加拦截器 -- 如果不加 static-locations 将不能被访问
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //拦截路径可自行配置多个 可用 ,分隔开
        registry.addInterceptor(authorizationInterceptor).addPathPatterns("/**");
    }

    /**
     * 跨域支持
     *
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH", "OPTIONS", "HEAD")
                .maxAge(3600 * 24);
    }

}
原文地址:https://www.cnblogs.com/vipsoft/p/15102232.html