【问题标题】:How do I use Spring Boot to serve static content located in Dropbox folder?如何使用 Spring Boot 提供位于 Dropbox 文件夹中的静态内容?
【发布时间】:2014-02-03 02:18:57
【问题描述】:

我有一个 Spring Boot Web 应用程序,我想在我的 Linode VPS (~/Dropbox/images) 上的共享 Dropbox 目录中提供静态内容。我读过 Spring Boot 会自动提供来自

的静态内容
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/",

当然,我的 Dropbox 目录不在类路径中。

虽然我可以将 Apache 配置为提供 Dropbox 文件夹中的图像,但我想利用 Spring Security 将静态内容的访问权限限制为经过身份验证的用户。

【问题讨论】:

    标签: apache spring-mvc dropbox static-content spring-boot


    【解决方案1】:

    您可以添加自己的静态资源处理程序(它会覆盖默认值),例如

    @Configuration
    public class StaticResourceConfiguration extends WebMvcConfigurerAdapter {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/**").addResourceLocations("file:/path/to/my/dropbox/");
        }
    }
    

    Spring Boot 中有一些关于此的文档,但它实际上只是一个普通的 Spring MVC 功能。

    从 spring boot 1.2 开始(我认为)你可以简单地设置spring.resources.staticLocations

    【讨论】:

    • 我在上面的示例中没有找到超类 WebMvcAdapter 。哪个 Spring JAR 包含该类?
    • 我改为扩展了 WebMvcConfigurerAdapter。
    • 正如@kaliatech 提到的,不要忘记资源位置路径上的尾部斜杠。
    • 建议保留默认资源映射并添加 dropbbox 文件夹作为添加资源重命名 resourceHandler 路径,例如:registry.addResourceHandler("/files/**").addResourceLocations("file:/path/ to/my/dropbox/");
    • 这仍然是 2020 年最好的方式吗?鉴于“WebMvcConfigurerAdapter”在较新版本的 spring 中已被弃用。
    【解决方案2】:

    Springboot(通过 Spring)现在可以轻松添加到现有资源处理程序。见Dave Syers answer。要添加到现有的静态资源处理程序,只需确保使用不覆盖现有路径的资源处理程序路径。

    下面的两个“也”注释仍然有效。

    。 . .

    [编辑:以下方法不再有效]

    如果你想扩展默认的静态资源处理程序,那么这样的事情似乎可以工作:

    @Configuration
    @AutoConfigureAfter(DispatcherServletAutoConfiguration.class)
    public class CustomWebMvcAutoConfig extends
                        WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter {
    
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String myExternalFilePath = "file:///C:/Temp/whatever/m/";
    
        registry.addResourceHandler("/m/**").addResourceLocations(myExternalFilePath);
    
        super.addResourceHandlers(registry);
      }
    
    }
    

    super.addResourceHandlers 的调用设置默认处理程序。

    还有:

    【讨论】:

    • 太棒了,谢谢!我还要提到在资源处理程序映射中包含尾随 /** 也很重要,我忘记添加了,我一直收到 404 错误
    • 这个解决方案的方向是正确的,但是它不可能从 WebMvcAutoConfigurationAdapter 继承,因为构造函数参数不是全部公开的。
    • @GeoffroyWarin 这个答案最初是为旧版本编写的。我刚刚对其进行了编辑以表明这一点。请参阅 Dave Syer 的回答。要添加到现有资源处理程序,只需确保不要覆盖现有资源路径。
    【解决方案3】:

    根据@Dave Syers 的回答,我将以下类添加到我的 Spring Boot 项目中:

    @Configuration
    public class StaticResourceConfiguration extends WebMvcConfigurerAdapter {
    
     private static final Logger LOG = LoggerFactory.getLogger(StaticResourceConfiguration.class);
    
     @Value("${static.path}")
     private String staticPath;
    
     @Override
     public void addResourceHandlers(ResourceHandlerRegistry registry) {
    
        if(staticPath != null) {
            LOG.info("Serving static content from " + staticPath);
            registry.addResourceHandler("/**").addResourceLocations("file:" + staticPath);
        }
     }
    
     // see https://stackoverflow.com/questions/27381781/java-spring-boot-how-to-map-my-my-app-root-to-index-html
     @Override
     public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("redirect:/index.html");
     }
    }
    

    这允许我使用参数--static.path like 启动我的 Spring Boot 应用程序

    java -jar spring-app-1.0-SNAPSHOT.jar --static.path=/path/to/my/static-files/
    

    这对于开发和测试来说非常方便。

    【讨论】:

    • 有没有办法直接使用'index.html'而不是'/'而不重定向?
    【解决方案4】:

    有一个属性spring.resources.staticLocations 可以在application.properties 中设置。请注意,这将覆盖默认位置。见org.springframework.boot.autoconfigure.web.ResourceProperties

    【讨论】:

      【解决方案5】:
      • 操作系统:Win 10
      • Spring Boot:2.1.2

      我想从 c:/images 提供静态内容

      添加此属性对我有用:

      spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:///C:/images/
      

      我在 Spring Boot DocAppendix A 中找到了该属性的原始值

      这将使 c:/images/image.jpg 可以作为 http://localhost:8080/image.jpg 访问

      【讨论】:

        【解决方案6】:

        @马克谢弗

        永远不会太晚,但在静态之后添加一个斜杠 (/):

        spring.resources.static-locations=file:/opt/x/y/z/static/
        

        所以http://<host>/index.html 现在可以访问了。

        【讨论】:

          【解决方案7】:

          基于@Dave Syer、@kaliatech 和@asmaier 的回答,springboot v2+ 的方式是:

          @Configuration
          @AutoConfigureAfter(DispatcherServletAutoConfiguration.class)
          public class StaticResourceConfiguration implements WebMvcConfigurer  {
          
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
              String myExternalFilePath = "file:///C:/temp/whatever/m/";
          
               registry.addResourceHandler("/m/**").addResourceLocations(myExternalFilePath);
          
            }
          
          }
          

          【讨论】:

          • @AutoConfigureAfter(DispatcherServletAutoConfiguration.class) 添加这个拯救了我的一天。谢谢
          【解决方案8】:

          从文件系统提供服务

          我在application.properties中添加了spring.resources.static-location=file:../frontend/build

          index.html 存在于 build 文件夹中

          使用也可以添加绝对路径

          spring.resources.static-location=file:/User/XYZ/Desktop/frontend/build

          我认为同样你可以尝试添加 Dropbox 文件夹路径。

          【讨论】:

            【解决方案9】:

            对于当前的 Spring-Boot 版本 1.5.3,参数为

            spring.resources.static-locations

            更新 我配置了

            `spring.resources.static-locations=file:/opt/x/y/z/static``

            并希望在调用时将我的 index.html 放在此文件夹中

            http://<host>/index.html

            这不起作用。我必须在 URL 中包含 文件夹名称

            http://<host>/static/index.html

            【讨论】:

              【解决方案10】:

              FWIW,上面推荐的spring.resources.static-locations 没有成功;对我有用的是设置 spring.thymeleaf.prefix:

              report.location=file:/Users/bill/report/html/
              spring.thymeleaf.prefix=${report.location}
              

              【讨论】:

                【解决方案11】:

                请注意,WebMvcConfigurerAdapter 现在已弃用(请参阅WebMvcConfigurerAdapter)。由于 Java 8 默认方法,您只需实现 WebMvcConfigurer

                【讨论】:

                  【解决方案12】:

                  您可以将文件夹放在 ServletContext 的根目录中。

                  然后在application.yml中指定这个目录的相对或绝对路径:

                  spring:
                    resources:
                      static-locations: file:some_temp_files/
                  

                  此文件夹中的资源将在以下位置提供(例如用于下载):

                  http://<host>:<port>/<context>/your_file.csv
                  

                  【讨论】:

                    猜你喜欢
                    • 2020-08-07
                    • 2021-09-11
                    • 2016-12-20
                    • 1970-01-01
                    • 2015-09-21
                    • 2016-07-14
                    • 2019-03-30
                    • 2011-03-13
                    • 2015-10-30
                    相关资源
                    最近更新 更多