Properly configure tldSkipPatterns for Tomcat

Fixes gh-10391
This commit is contained in:
Madhura Bhave 2017-11-07 14:54:32 -08:00
parent 424793d806
commit 8db77a6a61
3 changed files with 24 additions and 62 deletions

View File

@ -1,61 +0,0 @@
/*
* Copyright 2012-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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.web.embedded.tomcat;
import java.util.Set;
import org.apache.tomcat.JarScanner;
import org.apache.tomcat.util.scan.StandardJarScanFilter;
import org.apache.tomcat.util.scan.StandardJarScanner;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* {@link JarScanner} decorator allowing alternative default jar pattern matching. This
* class extends {@link StandardJarScanner} rather than implementing the
* {@link JarScanner} due to API changes introduced in Tomcat 8.
*
* @author Phillip Webb
* @author Stephane Nicoll
* @see #apply(TomcatEmbeddedContext, Set)
*/
class SkipPatternJarScanner extends StandardJarScanner {
private final JarScanner jarScanner;
SkipPatternJarScanner(JarScanner jarScanner, Set<String> patterns) {
Assert.notNull(jarScanner, "JarScanner must not be null");
Assert.notNull(patterns, "Patterns must not be null");
this.jarScanner = jarScanner;
StandardJarScanFilter filter = new StandardJarScanFilter();
filter.setTldSkip(StringUtils.collectionToCommaDelimitedString(patterns));
this.jarScanner.setJarScanFilter(filter);
}
/**
* Apply this decorator the specified context.
* @param context the context to apply to
* @param patterns the jar skip patterns or {@code null} for defaults
*/
static void apply(TomcatEmbeddedContext context, Set<String> patterns) {
SkipPatternJarScanner scanner = new SkipPatternJarScanner(context.getJarScanner(),
patterns);
context.setJarScanner(scanner);
}
}

View File

@ -60,6 +60,7 @@ import org.apache.catalina.webresources.EmptyResource;
import org.apache.catalina.webresources.StandardRoot;
import org.apache.coyote.AbstractProtocol;
import org.apache.coyote.http2.Http2Protocol;
import org.apache.tomcat.util.scan.StandardJarScanFilter;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.MimeMappings;
@ -201,7 +202,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto
resetDefaultLocaleMapping(context);
addLocaleMappings(context);
context.setUseRelativeRedirects(false);
SkipPatternJarScanner.apply(context, this.tldSkipPatterns);
configureTldSkipPatterns(context);
WebappLoader loader = new WebappLoader(context.getParentClassLoader());
loader.setLoaderClass(TomcatEmbeddedWebappClassLoader.class.getName());
loader.setDelegate(true);
@ -220,6 +221,12 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto
postProcessContext(context);
}
private void configureTldSkipPatterns(TomcatEmbeddedContext context) {
StandardJarScanFilter filter = new StandardJarScanFilter();
filter.setTldSkip(StringUtils.collectionToCommaDelimitedString(this.tldSkipPatterns));
context.getJarScanner().setJarScanFilter(filter);
}
/**
* Override Tomcat's default locale mappings to align with other servers. See
* {@code org.apache.catalina.util.CharsetMapperDefault.properties}.

View File

@ -22,6 +22,7 @@ import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.naming.InitialContext;
@ -43,6 +44,7 @@ import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.util.CharsetMapper;
import org.apache.catalina.valves.RemoteIpValve;
import org.apache.jasper.servlet.JspServlet;
import org.apache.tomcat.JarScanFilter;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
@ -385,6 +387,20 @@ public class TomcatServletWebServerFactoryTests
assertThat(sessionIdGenerator.getJvmRoute()).isEqualTo("test");
}
@Test
@SuppressWarnings("unchecked")
public void tldSkipPatternsShouldBeAppliedToContextJarScanner() throws Exception {
TomcatServletWebServerFactory factory = getFactory();
factory.addTldSkipPatterns("foo.jar", "bar.jar");
this.webServer = factory.getWebServer();
this.webServer.start();
Tomcat tomcat = ((TomcatWebServer) this.webServer).getTomcat();
Context context = (Context) tomcat.getHost().findChildren()[0];
JarScanFilter jarScanFilter = context.getJarScanner().getJarScanFilter();
Set<String> tldSkipSet = (Set<String>) ReflectionTestUtils.getField(jarScanFilter, "tldSkipSet");
assertThat(tldSkipSet).contains("foo.jar", "bar.jar");
}
@Override
protected JspServlet getJspServlet() throws ServletException {
Tomcat tomcat = ((TomcatWebServer) this.webServer).getTomcat();