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
This commit is contained in:
skarafaz 2017-05-07 17:53:44 +02:00 committed by Brian Clozel
parent 8deec9569c
commit e2aa117ff9
1 changed files with 9 additions and 19 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,9 +17,9 @@
package org.springframework.web.servlet.resource; package org.springframework.web.servlet.resource;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.webjars.MultipleMatchesException;
import org.webjars.WebJarAssetLocator; import org.webjars.WebJarAssetLocator;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
@ -99,26 +99,16 @@ public class WebJarsResourceResolver extends AbstractResourceResolver {
} }
protected String findWebJarResourcePath(String path) { protected String findWebJarResourcePath(String path) {
try { int startOffset = (path.startsWith("/") ? 1 : 0);
int startOffset = (path.startsWith("/") ? 1 : 0); int endOffset = path.indexOf("/", 1);
int endOffset = path.indexOf("/", 1); if (endOffset != -1) {
if (endOffset != -1) { String webjar = path.substring(startOffset, endOffset);
String webjar = path.substring(startOffset, endOffset); String partialPath = path.substring(endOffset + 1);
String partialPath = path.substring(endOffset); String webJarPath = webJarAssetLocator.getFullPathExact(webjar, partialPath);
String webJarPath = webJarAssetLocator.getFullPath(webjar, partialPath); if (webJarPath != null) {
return webJarPath.substring(WEBJARS_LOCATION_LENGTH); 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; return null;
} }