From 2c47098b354322aa7d0d8113c9a8a7526d4075b3 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Fri, 27 Feb 2015 18:46:10 +0100 Subject: [PATCH] Allow file locations for resource handling Prior to this change, location checks for serving resources would append `/` to the location path it didn't already have one. This commit makes sure not to append a `/` if the provided location is actually a file. Issue: SPR-12747 --- .../web/servlet/resource/PathResourceResolver.java | 3 +++ .../web/servlet/resource/PathResourceResolverTests.java | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java index 475f40b390..45f3b9e286 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java @@ -179,6 +179,9 @@ public class PathResourceResolver extends AbstractResourceResolver { resourcePath = resource.getURL().getPath(); locationPath = StringUtils.cleanPath(location.getURL().getPath()); } + if(locationPath.equals(resourcePath)) { + return true; + } locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/"); if (!resourcePath.startsWith(locationPath)) { return false; diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/PathResourceResolverTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/PathResourceResolverTests.java index d4839e53ed..eb9162ac9e 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/PathResourceResolverTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/PathResourceResolverTests.java @@ -117,4 +117,11 @@ public class PathResourceResolverTests { assertNotNull(this.resolver.resolveResource(null, "main.css", Arrays.asList(location), null)); } + // SPR-12747 + @Test + public void checkFileLocation() throws Exception { + Resource resource = new ClassPathResource("test/main.css", PathResourceResolver.class); + assertTrue(this.resolver.checkResource(resource, resource)); + } + }