Skip to content

Commit 2c47098

Browse files
committed
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
1 parent b4d9fb9 commit 2c47098

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ else if (resource instanceof ServletContextResource) {
179179
resourcePath = resource.getURL().getPath();
180180
locationPath = StringUtils.cleanPath(location.getURL().getPath());
181181
}
182+
if(locationPath.equals(resourcePath)) {
183+
return true;
184+
}
182185
locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/");
183186
if (!resourcePath.startsWith(locationPath)) {
184187
return false;

spring-webmvc/src/test/java/org/springframework/web/servlet/resource/PathResourceResolverTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,11 @@ public void checkRelativeLocation() throws Exception {
117117
assertNotNull(this.resolver.resolveResource(null, "main.css", Arrays.asList(location), null));
118118
}
119119

120+
// SPR-12747
121+
@Test
122+
public void checkFileLocation() throws Exception {
123+
Resource resource = new ClassPathResource("test/main.css", PathResourceResolver.class);
124+
assertTrue(this.resolver.checkResource(resource, resource));
125+
}
126+
120127
}

0 commit comments

Comments
 (0)