Skip to content

Add trailing slash to static resource location if missing #33817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* {@link ResourceHttpRequestHandler} and {@link org.springframework.web.servlet.function}.
*
* @author Rossen Stoyanchev
* @author Edoardo Patti
* @since 6.2
*/
public abstract class ResourceHandlerUtils {
Expand Down Expand Up @@ -81,6 +82,23 @@ public static void assertLocationPath(@Nullable String path) {
"Resource location does not end with slash: " + path);
}

/**
* Assert the given location path is not null and look for the trailing slash adding it when absent.
* @return the input string if the trailing slash was already present
* the input string plus the trailing slash otherwise.
*/
public static String addTrailingSlashIfAbsent(@Nullable String path) {
Assert.notNull(path, "Resource location path must not be null");
if(!path.endsWith(FOLDER_SEPARATOR) && !path.endsWith(WINDOWS_FOLDER_SEPARATOR)) {
logger.warn(
"Observed an attempting to register a resource with a location without trailing slash: '%s'. The framework will add it automatically."
.formatted(path));
var trailingSlash = path.contains(WINDOWS_FOLDER_SEPARATOR) ? WINDOWS_FOLDER_SEPARATOR : FOLDER_SEPARATOR;
return path.concat(trailingSlash);
}
return path;
}

/**
* Normalize the given resource path replacing the following:
* <ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ private void resolveResourceLocations() {
charset = Charset.forName(value);
location = location.substring(endIndex + 1);
}
ResourceHandlerUtils.assertLocationPath(location);
location = ResourceHandlerUtils.addTrailingSlashIfAbsent(location);
Resource resource = applicationContext.getResource(location);
if (location.equals("/") && !(resource instanceof ServletContextResource)) {
throw new IllegalStateException(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.web.servlet.resource;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link ResourceHandlerUtils}.
*
* @author Edoardo Patti
*/
public class ResourceHandlerUtilsTests {

@Test
void testTrailingSlash() {
var windowsDirectory = "META-INF\\resources\\webjars";
var directory = "META-INF/resources/webjars";
var directoryWithoutSlash = "META-INF";
var correctWindowsDirectory = "META-INF\\resources\\webjars\\";
var correctDirectory = "META-INF/resources/webjars/";
assertThat(ResourceHandlerUtils.addTrailingSlashIfAbsent(windowsDirectory).endsWith("\\")).isTrue();
assertThat(ResourceHandlerUtils.addTrailingSlashIfAbsent(directory).endsWith("/")).isTrue();
assertThat(ResourceHandlerUtils.addTrailingSlashIfAbsent(directoryWithoutSlash).endsWith("/")).isTrue();
assertThat(ResourceHandlerUtils.addTrailingSlashIfAbsent(correctWindowsDirectory)).isEqualTo(correctWindowsDirectory);
assertThat(ResourceHandlerUtils.addTrailingSlashIfAbsent(correctDirectory)).isEqualTo(correctDirectory);
}
}