首页 文章

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

提问于
浏览
54

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

9 回答

  • 29

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

  • 56

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

  • 18

    @MarkSchäfer

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

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

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

  • 2

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

    spring.resources.static-locations

    Update 我配置了

    `spring.resources.static-位置=文件中:/ opt / X / Y / Z / static``

    并期望在调用时让我的index.html生活在这个文件夹中

    http://<host>/index.html

    这没用 . 我必须在URL中包含文件夹名称:

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

  • 1

    从文件系统服务

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

    index.html 出现在 build 文件夹中

    使用也可以添加绝对路径

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

    我想同样你可以尝试添加Dropbox文件夹路径 .

  • 1

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

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

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

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

    . . .

    [Edit: The approach below is no longer valid]

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

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

    也:

  • 1

    根据@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 启动我的 Spring 季启动应用程序

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

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

  • 7

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

    @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 季启动1.2(我认为)你可以简单地设置 spring.resources.staticLocations .

相关问题