如何使用Spring Boot来提供位于Dropbox文件夹中的静态内容?

 mobiledu2502910203 发布于 2023-02-02 19:48

我有一个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来限制静态内容对经过身份验证的用户的访问.

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

    以下两个"也"注释仍然有效.

    ...

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

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

    @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设置默认处理程序.

    也:

    请注意外部文件路径上的尾部斜杠.(取决于您对URL映射的期望).

    请考虑查看WebMvcAutoConfigurationAdapter的源代码.

    2023-02-02 19:49 回答
  • 您可以添加自己的静态资源处理程序(它会覆盖默认值),例如

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

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

    此外,自春季启动1.2(我认为)你可以简单地设置spring.resources.staticLocations.

    2023-02-02 19:49 回答
  • 根据@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 /sf/ask/17360801/
     @Override
     public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("redirect:/index.html");
     }
    }
    

    这允许我用参数--static.path这样启动我的春季启动应用程序

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

    这对于开发和测试非常方便.

    2023-02-02 19:49 回答
  • @马克·谢弗

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

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

    所以http://<host>/index.html现在可以到达。

    2023-02-02 19:51 回答
  • 有一个属性spring.resources.staticLocations可以设置在application.properties.请注意,这将覆盖默认位置.见org.springframework.boot.autoconfigure.web.ResourceProperties.

    2023-02-02 19:51 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有