From 3d290165fb6b37544d23755f5182fac3478210f4 Mon Sep 17 00:00:00 2001 From: skarafaz Date: Sun, 7 May 2017 17:53:44 +0200 Subject: [PATCH] Find exact matches in WebJarsResourceResolver Prior to this commit, resolving resources from webjars using the `WebJarAssetLocator.getFullPath` could lead to multiple candidates, since this method is trying to find *any* resource matching that path under the given webjar location. This commit replaces that call with `WebJarAssetLocator.getFullPathExact`, which avoids those multiple matches and only resolves resources if the given path is exact. Issue: SPR-15526 (cherry picked from commit e2aa117ff9) --- .../resource/WebJarsResourceResolver.java | 28 ++++++------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/WebJarsResourceResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/WebJarsResourceResolver.java index a76214c393..71c2a490e5 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/WebJarsResourceResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/WebJarsResourceResolver.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. @@ -17,9 +17,9 @@ package org.springframework.web.servlet.resource; import java.util.List; + import javax.servlet.http.HttpServletRequest; -import org.webjars.MultipleMatchesException; import org.webjars.WebJarAssetLocator; import org.springframework.core.io.Resource; @@ -99,26 +99,16 @@ public class WebJarsResourceResolver extends AbstractResourceResolver { } protected String findWebJarResourcePath(String path) { - try { - int startOffset = (path.startsWith("/") ? 1 : 0); - int endOffset = path.indexOf("/", 1); - if (endOffset != -1) { - String webjar = path.substring(startOffset, endOffset); - String partialPath = path.substring(endOffset); - String webJarPath = webJarAssetLocator.getFullPath(webjar, partialPath); + int startOffset = (path.startsWith("/") ? 1 : 0); + int endOffset = path.indexOf("/", 1); + if (endOffset != -1) { + String webjar = path.substring(startOffset, endOffset); + String partialPath = path.substring(endOffset + 1); + String webJarPath = webJarAssetLocator.getFullPathExact(webjar, partialPath); + if (webJarPath != null) { return webJarPath.substring(WEBJARS_LOCATION_LENGTH); } } - catch (MultipleMatchesException ex) { - if (logger.isWarnEnabled()) { - logger.warn("WebJar version conflict for \"" + path + "\"", ex); - } - } - catch (IllegalArgumentException ex) { - if (logger.isTraceEnabled()) { - logger.trace("No WebJar resource found for \"" + path + "\""); - } - } return null; }