From 2bfff7fc376c4c747cc9811c54c0ba320787db11 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 11 Jul 2024 16:15:42 +0200 Subject: [PATCH] Retain original URL instance in case of custom URLStreamHandler Closes gh-33199 --- .../io/support/PathMatchingResourcePatternResolver.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java b/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java index 3bbaa35651b0..021032455866 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java @@ -428,12 +428,18 @@ protected Resource convertClassLoaderURL(URL url) { if (!cleanedPath.equals(urlString)) { // Prefer cleaned URL, aligned with UrlResource#createRelative(String) try { - return new UrlResource(ResourceUtils.toURI(cleanedPath)); + // Cannot test for URLStreamHandler directly: URL equality for same String + // in order to find out whether original URL uses default URLStreamHandler. + if (ResourceUtils.toURL(urlString).equals(url)) { + // Plain URL with default URLStreamHandler -> replace with cleaned path. + return new UrlResource(ResourceUtils.toURI(cleanedPath)); + } } catch (URISyntaxException | MalformedURLException ex) { // Fallback to regular URL construction below... } } + // Retain original URL instance, potentially including custom URLStreamHandler. return new UrlResource(url); } }