I have a Spring Boot web application, and I would like to serve static content located in a shared Dropbox directory on my Linode VPS (~/Dropbox/images). I've read that Spring Boot will automatically serve static content from
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/",
but of course my Dropbox directory is not on the classpath.
Although I could configure Apache to serve the images in my Dropbox folder, I would like to take advantage of Spring Security to restrict access of the static content to authenticated users.
You can add your own static resource handler (it overwrites the default), e.g.
There is some documentation about this in Spring Boot, but it's really just a vanilla Spring MVC feature.
Also since spring boot 1.2 (I think) you can simply set
spring.resources.staticLocations
.FWIW, I didn't have any success with the
spring.resources.static-locations
recommended above; what worked for me was setting spring.thymeleaf.prefix:Springboot (via Spring) now makes adding to existing resource handlers easy. See Dave Syers answer. To add to the existing static resource handlers, simply be sure to use a resource handler path that doesn't override existing paths.
The two "also" notes below are still valid.
. . .
[Edit: The approach below is no longer valid]
If you want to extend the default static resource handlers, then something like this seems to work:
The call to
super.addResourceHandlers
sets up the default handlers.Also:
Based on @Dave Syers answer I add the following class to my Spring Boot project:
This allows me to start my spring boot app with the parameter
--static.path
likeThis can be very handy for development and testing.
Based on @Dave Syer, @kaliatech and @asmaier answers the springboot v2+ way would be:
There's a property
spring.resources.staticLocations
that can be set in theapplication.properties
. Note that this will override the default locations. Seeorg.springframework.boot.autoconfigure.web.ResourceProperties
.