diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMapping.java index ae3c5afbd5e..a749d2f99af 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMapping.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -501,6 +501,11 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport handler = obtainApplicationContext().getBean(handlerName); } + // Ensure presence of cached lookupPath for interceptors and others + if (!ServletRequestPathUtils.hasCachedPath(request)) { + initLookupPath(request); + } + HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request); if (logger.isTraceEnabled()) { diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/handler/HandlerMappingTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/handler/HandlerMappingTests.java index 48315e7df47..7273780514e 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/handler/HandlerMappingTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/handler/HandlerMappingTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -23,6 +23,7 @@ import java.util.stream.Stream; import javax.servlet.http.HttpServletRequest; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.provider.Arguments; import org.springframework.web.context.support.StaticWebApplicationContext; @@ -66,16 +67,30 @@ class HandlerMappingTests { mapping.setApplicationContext(new StaticWebApplicationContext()); HandlerExecutionChain chain = mapping.getHandler(requestFactory.apply("/")); + assertThat(chain).isNotNull(); assertThat(chain.getInterceptorList()).contains(i1.getInterceptor(), i2, i3.getInterceptor(), i4); } + @Test // gh-26546 + void abstractHandlerMappingEnsuresCachedLookupPath() throws Exception { + MappedInterceptor interceptor = new MappedInterceptor(new String[] {"/**"}, mock(HandlerInterceptor.class)); + TestHandlerMapping mapping = new TestHandlerMapping(); + mapping.setInterceptors(interceptor); + mapping.setApplicationContext(new StaticWebApplicationContext()); + + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); + HandlerExecutionChain chain = mapping.getHandler(request); + + assertThat(chain).isNotNull(); + assertThat(chain.getInterceptorList()).contains(interceptor.getInterceptor()); + } + private static class TestHandlerMapping extends AbstractHandlerMapping { @Override protected Object getHandlerInternal(HttpServletRequest request) { - initLookupPath(request); return new Object(); } }