Validate Resource type in ResourceHttpRequestHandler

This commit is contained in:
Rossen Stoyanchev
2021-01-11 20:54:27 +00:00
parent e5ab67bd71
commit fceceb480f
2 changed files with 28 additions and 0 deletions

View File

@@ -57,6 +57,7 @@ import org.springframework.util.StringValueResolver;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.support.ServletContextResource;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.servlet.HandlerMapping;
@@ -445,6 +446,12 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
location = location.substring(endIndex + 1);
}
Resource resource = applicationContext.getResource(location);
if (location.equals("/") && !(resource instanceof ServletContextResource)) {
throw new IllegalStateException(
"The String-based location \"/\" should be relative to the web application root " +
"but resolved to a Resource of type: " + resource.getClass() + ". " +
"If this is intentional, please pass it as a pre-configured Resource via setLocations.");
}
this.locationsToUse.add(resource);
if (charset != null) {
if (!(resource instanceof UrlResource)) {