From 57c8c759ae7659c683e409cfde6220862b87419a Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 16 Mar 2017 18:55:49 +0100 Subject: [PATCH] Avoid pattern misdetection in Tomcat "war:" URL separator Issue: SPR-15332 (cherry picked from commit 012c56a) --- .../PathMatchingResourcePatternResolver.java | 7 +++--- .../springframework/util/ResourceUtils.java | 22 +++++++++---------- 2 files changed, 14 insertions(+), 15 deletions(-) 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 0de61f467c..f7d1d7fec2 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 @@ -284,9 +284,10 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol } } else { - // Only look for a pattern after a prefix here - // (to not get fooled by a pattern symbol in a strange prefix). - int prefixEnd = locationPattern.indexOf(":") + 1; + // Generally only look for a pattern after a prefix here, + // and on Tomcat only after the "*/" separator for its "war:" protocol. + int prefixEnd = (locationPattern.startsWith("war:") ? locationPattern.indexOf("*/") + 1 : + locationPattern.indexOf(":") + 1); if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) { // a file pattern return findPathMatchingResources(locationPattern); diff --git a/spring-core/src/main/java/org/springframework/util/ResourceUtils.java b/spring-core/src/main/java/org/springframework/util/ResourceUtils.java index 9108053f80..b0a2b66e83 100644 --- a/spring-core/src/main/java/org/springframework/util/ResourceUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ResourceUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -35,12 +35,6 @@ import java.net.URLConnection; * object, which in turn allows one to obtain a {@code java.io.File} in the * file system through its {@code getFile()} method. * - *

The main reason for these utility methods for resource location handling - * is to support {@link Log4jConfigurer}, which must be able to resolve - * resource locations before the logging system has been initialized. - * Spring's {@code Resource} abstraction in the core package, on the other hand, - * already expects the logging system to be available. - * * @author Juergen Hoeller * @since 1.1.5 * @see org.springframework.core.io.Resource @@ -69,6 +63,9 @@ public abstract class ResourceUtils { /** URL protocol for an entry from a jar file: "jar" */ public static final String URL_PROTOCOL_JAR = "jar"; + /** URL protocol for an entry from a war file: "war" */ + public static final String URL_PROTOCOL_WAR = "war"; + /** URL protocol for an entry from a zip file: "zip" */ public static final String URL_PROTOCOL_ZIP = "zip"; @@ -264,7 +261,7 @@ public abstract class ResourceUtils { /** * Determine whether the given URL points to a resource in the file system, - * that is, has protocol "file", "vfsfile" or "vfs". + * i.e. has protocol "file", "vfsfile" or "vfs". * @param url the URL to check * @return whether the URL has been identified as a file system URL */ @@ -275,15 +272,16 @@ public abstract class ResourceUtils { } /** - * Determine whether the given URL points to a resource in a jar file, - * that is, has protocol "jar", "zip", "vfszip" or "wsjar". + * Determine whether the given URL points to a resource in a jar file. + * i.e. has protocol "jar", "war, ""zip", "vfszip" or "wsjar". * @param url the URL to check * @return whether the URL has been identified as a JAR URL */ public static boolean isJarURL(URL url) { String protocol = url.getProtocol(); - return (URL_PROTOCOL_JAR.equals(protocol) || URL_PROTOCOL_ZIP.equals(protocol) || - URL_PROTOCOL_VFSZIP.equals(protocol) || URL_PROTOCOL_WSJAR.equals(protocol)); + return (URL_PROTOCOL_JAR.equals(protocol) || URL_PROTOCOL_WAR.equals(protocol) || + URL_PROTOCOL_ZIP.equals(protocol) || URL_PROTOCOL_VFSZIP.equals(protocol) || + URL_PROTOCOL_WSJAR.equals(protocol)); } /**