Skip to content

Commit 59ec871

Browse files
committed
Append trailing slash to static location
Closes gh-33815
1 parent 37243f4 commit 59ec871

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

Diff for: spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceHandlerUtils.java

+13-6
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,27 @@ else if (location instanceof ClassPathResource classPathResource) {
6565
else {
6666
path = location.getURL().getPath();
6767
}
68-
assertLocationPath(path);
68+
Assert.isTrue(path.endsWith(FOLDER_SEPARATOR) || path.endsWith(WINDOWS_FOLDER_SEPARATOR),
69+
"Resource location does not end with slash: " + path);
6970
}
7071
catch (IOException ex) {
7172
// ignore
7273
}
7374
}
7475

7576
/**
76-
* Assert the given location path is a directory and ends on slash.
77+
* Check if the given static resource location path ends with a trailing
78+
* slash, and append it if necessary.
79+
* @param path the location path
80+
* @return the resulting path to use
7781
*/
78-
public static void assertLocationPath(@Nullable String path) {
79-
Assert.notNull(path, "Resource location path must not be null");
80-
Assert.isTrue(path.endsWith(FOLDER_SEPARATOR) || path.endsWith(WINDOWS_FOLDER_SEPARATOR),
81-
"Resource location does not end with slash: " + path);
82+
public static String initLocationPath(String path) {
83+
String separator = (path.contains(FOLDER_SEPARATOR) ? FOLDER_SEPARATOR : WINDOWS_FOLDER_SEPARATOR);
84+
if (!path.endsWith(separator)) {
85+
path = path.concat(separator);
86+
logger.warn("Appended trailing slash to static resource location: " + path);
87+
}
88+
return path;
8289
}
8390

8491
/**

Diff for: spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ private void resolveResourceLocations() {
379379
Assert.isTrue(CollectionUtils.isEmpty(this.locationResources), "Please set " +
380380
"either Resource-based \"locations\" or String-based \"locationValues\", but not both.");
381381
for (String location : this.locationValues) {
382-
ResourceHandlerUtils.assertLocationPath(location);
382+
location = ResourceHandlerUtils.initLocationPath(location);
383383
result.add(this.resourceLoader.getResource(location));
384384
}
385385
}

Diff for: spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHandlerUtils.java

+13-6
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,27 @@ else if (location instanceof ClassPathResource classPathResource) {
6565
else {
6666
path = location.getURL().getPath();
6767
}
68-
assertLocationPath(path);
68+
Assert.isTrue(path.endsWith(FOLDER_SEPARATOR) || path.endsWith(WINDOWS_FOLDER_SEPARATOR),
69+
"Resource location does not end with slash: " + path);
6970
}
7071
catch (IOException ex) {
7172
// ignore
7273
}
7374
}
7475

7576
/**
76-
* Assert the given location path is a directory and ends on slash.
77+
* Check if the given static resource location path ends with a trailing
78+
* slash, and append it if necessary.
79+
* @param path the location path
80+
* @return the resulting path to use
7781
*/
78-
public static void assertLocationPath(@Nullable String path) {
79-
Assert.notNull(path, "Resource location path must not be null");
80-
Assert.isTrue(path.endsWith(FOLDER_SEPARATOR) || path.endsWith(WINDOWS_FOLDER_SEPARATOR),
81-
"Resource location does not end with slash: " + path);
82+
public static String initLocationPath(String path) {
83+
String separator = (path.contains(FOLDER_SEPARATOR) ? FOLDER_SEPARATOR : WINDOWS_FOLDER_SEPARATOR);
84+
if (!path.endsWith(separator)) {
85+
path = path.concat(separator);
86+
logger.warn("Appended trailing slash to static resource location: " + path);
87+
}
88+
return path;
8289
}
8390

8491
/**

Diff for: spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ private void resolveResourceLocations() {
496496
charset = Charset.forName(value);
497497
location = location.substring(endIndex + 1);
498498
}
499-
ResourceHandlerUtils.assertLocationPath(location);
499+
location = ResourceHandlerUtils.initLocationPath(location);
500500
Resource resource = applicationContext.getResource(location);
501501
if (location.equals("/") && !(resource instanceof ServletContextResource)) {
502502
throw new IllegalStateException(

0 commit comments

Comments
 (0)