zoukankan      html  css  js  c++  java
  • 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);
        }
    
    }
  • 相关阅读:
    Hdu 2564 单词缩写(字符串输入流的使用)
    Hdu2824 快速求欧拉函数和
    hdu 1787 欧拉函数模板
    Hdu2558(欧拉函数)
    hdu 1175连连看 (bfs带方向变化次数)
    pandas 使用总结
    APScheduler 定时任务使用总结
    watchdog 监控文件变化使用总结
    js 鼠标特效
    js 生成雪花间隔
  • 原文地址:https://www.cnblogs.com/vipsoft/p/15102232.html
Copyright © 2011-2022 走看看