diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceHandlerUtils.java b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceHandlerUtils.java index 9de12816cf..fdf650e71d 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceHandlerUtils.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceHandlerUtils.java @@ -65,7 +65,8 @@ public abstract class ResourceHandlerUtils { else { path = location.getURL().getPath(); } - assertLocationPath(path); + Assert.isTrue(path.endsWith(FOLDER_SEPARATOR) || path.endsWith(WINDOWS_FOLDER_SEPARATOR), + "Resource location does not end with slash: " + path); } catch (IOException ex) { // ignore @@ -73,12 +74,18 @@ public abstract class ResourceHandlerUtils { } /** - * Assert the given location path is a directory and ends on slash. + * Check if the given static resource location path ends with a trailing + * slash, and append it if necessary. + * @param path the location path + * @return the resulting path to use */ - public static void assertLocationPath(@Nullable String path) { - Assert.notNull(path, "Resource location path must not be null"); - Assert.isTrue(path.endsWith(FOLDER_SEPARATOR) || path.endsWith(WINDOWS_FOLDER_SEPARATOR), - "Resource location does not end with slash: " + path); + public static String initLocationPath(String path) { + String separator = (path.contains(FOLDER_SEPARATOR) ? FOLDER_SEPARATOR : WINDOWS_FOLDER_SEPARATOR); + if (!path.endsWith(separator)) { + path = path.concat(separator); + logger.warn("Appended trailing slash to static resource location: " + path); + } + return path; } /** diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java index 171f85ff8c..cd624891b5 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java @@ -379,7 +379,7 @@ public class ResourceWebHandler implements WebHandler, InitializingBean { Assert.isTrue(CollectionUtils.isEmpty(this.locationResources), "Please set " + "either Resource-based \"locations\" or String-based \"locationValues\", but not both."); for (String location : this.locationValues) { - ResourceHandlerUtils.assertLocationPath(location); + location = ResourceHandlerUtils.initLocationPath(location); result.add(this.resourceLoader.getResource(location)); } } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHandlerUtils.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHandlerUtils.java index fa091ef2eb..255a4080bd 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHandlerUtils.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHandlerUtils.java @@ -65,7 +65,8 @@ public abstract class ResourceHandlerUtils { else { path = location.getURL().getPath(); } - assertLocationPath(path); + Assert.isTrue(path.endsWith(FOLDER_SEPARATOR) || path.endsWith(WINDOWS_FOLDER_SEPARATOR), + "Resource location does not end with slash: " + path); } catch (IOException ex) { // ignore @@ -73,12 +74,18 @@ public abstract class ResourceHandlerUtils { } /** - * Assert the given location path is a directory and ends on slash. + * Check if the given static resource location path ends with a trailing + * slash, and append it if necessary. + * @param path the location path + * @return the resulting path to use */ - public static void assertLocationPath(@Nullable String path) { - Assert.notNull(path, "Resource location path must not be null"); - Assert.isTrue(path.endsWith(FOLDER_SEPARATOR) || path.endsWith(WINDOWS_FOLDER_SEPARATOR), - "Resource location does not end with slash: " + path); + public static String initLocationPath(String path) { + String separator = (path.contains(FOLDER_SEPARATOR) ? FOLDER_SEPARATOR : WINDOWS_FOLDER_SEPARATOR); + if (!path.endsWith(separator)) { + path = path.concat(separator); + logger.warn("Appended trailing slash to static resource location: " + path); + } + return path; } /** diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java index 836ab6f9a6..14cd680e4d 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java @@ -496,7 +496,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator charset = Charset.forName(value); location = location.substring(endIndex + 1); } - ResourceHandlerUtils.assertLocationPath(location); + location = ResourceHandlerUtils.initLocationPath(location); Resource resource = applicationContext.getResource(location); if (location.equals("/") && !(resource instanceof ServletContextResource)) { throw new IllegalStateException(