ResourceUrlProvider handles sanitizes double slashes
Issue: SPR-16296
This commit is contained in:
@@ -34,6 +34,7 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.util.PathMatcher;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
|
||||
@@ -217,6 +218,14 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
|
||||
*/
|
||||
@Nullable
|
||||
public final String getForLookupPath(String lookupPath) {
|
||||
|
||||
// Clean duplicate slashes or pathWithinPattern won't match lookupPath
|
||||
String previous;
|
||||
do {
|
||||
previous = lookupPath;
|
||||
lookupPath = StringUtils.replace(lookupPath, "//", "/");
|
||||
} while (!lookupPath.equals(previous));
|
||||
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Getting resource URL for lookup path \"" + lookupPath + "\"");
|
||||
}
|
||||
|
||||
@@ -35,6 +35,9 @@ import org.springframework.web.context.support.AnnotationConfigWebApplicationCon
|
||||
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
|
||||
/**
|
||||
@@ -136,6 +139,25 @@ public class ResourceUrlProviderTests {
|
||||
assertFalse(urlProviderBean.isAutodetect());
|
||||
}
|
||||
|
||||
@Test // SPR-16296
|
||||
public void getForLookupPathShouldNotFailIfPathContainsDoubleSlashes() {
|
||||
// given
|
||||
ResourceResolver mockResourceResolver = mock(ResourceResolver.class);
|
||||
when(mockResourceResolver.resolveUrlPath(any(), any(), any())).thenReturn("some-path");
|
||||
|
||||
ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
|
||||
handler.getResourceResolvers().add(mockResourceResolver);
|
||||
|
||||
ResourceUrlProvider provider = new ResourceUrlProvider();
|
||||
provider.getHandlerMap().put("/some-pattern/**", handler);
|
||||
|
||||
// when
|
||||
String lookupForPath = provider.getForLookupPath("/some-pattern/some-lib//some-resource");
|
||||
|
||||
// then
|
||||
assertEquals("/some-pattern/some-path", lookupForPath);
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
@SuppressWarnings({"unused", "WeakerAccess"})
|
||||
|
||||
Reference in New Issue
Block a user