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

77

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

"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/",

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

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

12个回答

81

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

@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包含该类? - Shannon Kendrick
我扩展了WebMvcConfigurerAdapter。 - Shannon Kendrick
7
如@kaliatech所提到的,不要忘记在资源位置路径末尾加上斜杠。 - 1in9ui5t
1
建议保留默认资源映射并将Dropbox文件夹作为附加资源添加,可以重命名resourceHandler路径,例如:registry.addResourceHandler(“/ files / **”)。addResourceLocations(“file:/ path / to / my / dropbox /”); - Dmitry Stolbov
1
在2020年,这仍然是最好的方法吗?鉴于新版本的Spring中“WebMvcConfigurerAdapter”已被弃用。 - z atef
显示剩余5条评论

35

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

以下两个“also”注意事项仍然有效。

. . .

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

如果要扩展默认的静态资源处理程序,则类似以下内容似乎可以工作:

@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错误。 - Trevor
这个解决方案朝着正确的方向前进,但是无法从WebMvcAutoConfigurationAdapter继承,因为构造函数参数不全是公共的。 - Geoffroy Warin
@GeoffroyWarin 这个答案最初是针对旧版本编写的。我刚刚编辑了它以表明这一点。请参考Dave Syer的答案。要添加到现有的资源处理程序中,只需确保不覆盖现有的资源路径即可。 - kaliatech

21

根据@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://dev59.com/xl4c5IYBdhLWcg3w7t7E
 @Override
 public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("redirect:/index.html");
 }
}

这使我能够使用参数--static.path启动我的Spring Boot应用程序,例如:

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

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


有没有一种方法可以直接使用“index.html”而不是“/”,而无需重定向? - Balazs Kelemen

10

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


1
spring.resources.staticLocations 已被弃用,但你可以使用 spring.web.resources.staticLocations 代替。 - RiZKiT

9
  • 操作系统: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文档附录A中找到了该属性的原始值。
这将使c:/images/image.jpg作为http://localhost:8080/image.jpg可访问。

1
spring.resources.static-locations 已经被弃用,但是你可以使用 spring.web.resources.static-locations 来代替。 - RiZKiT
它不是spring.resources.static-locations,而是spring.web.resources.static-locations吗? - Iorweth333

9

根据 @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);

  }

}

2
@AutoConfigureAfter(DispatcherServletAutoConfiguration.class) 加上这个让我的一天都救了。谢谢。 - exexzian

8

@Mark Schäfer

不算晚,但是在static后面加一条斜杠(/):

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

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


2

使用文件系统提供服务

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

build文件夹中包含index.html

您也可以添加绝对路径

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

我认为类似地,您可以尝试添加Dropbox文件夹路径。


1

对于当前的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


1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接