From 75df9e7bd7d02a459a0b53942a4f56c36917a2e6 Mon Sep 17 00:00:00 2001 From: Shinya Murakami Date: Tue, 24 Nov 2026 08:18:20 +0900 Subject: [PATCH 1/2] [#2969] - Propagate PathMatchingFilterChainResolver's PatternMatcher to PathMatchingFilters PathMatchingFilterChainResolver.setPathMatcher() replaced only the resolver's matcher, while each PathMatchingFilter re-matched the path with its own AntPathMatcher. With a non-Ant matcher the resolver selected a chain whose filters then skipped the request. Apply the resolver's matcher to the PathMatchingFilters in its FilterChainManager, as ShiroFilterFactoryBean already does for Spring. fixes #2969 Co-Authored-By: Claude Opus 5.5 --- .../mgt/PathMatchingFilterChainResolver.java | 23 +++++++ .../PathMatchingFilterChainResolverTest.java | 61 +++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/web/src/main/java/org/apache/shiro/web/filter/mgt/PathMatchingFilterChainResolver.java b/web/src/main/java/org/apache/shiro/web/filter/mgt/PathMatchingFilterChainResolver.java index 91968102d0..766b1a1b0c 100644 --- a/web/src/main/java/org/apache/shiro/web/filter/mgt/PathMatchingFilterChainResolver.java +++ b/web/src/main/java/org/apache/shiro/web/filter/mgt/PathMatchingFilterChainResolver.java @@ -20,11 +20,13 @@ import org.apache.shiro.util.AntPathMatcher; import org.apache.shiro.util.PatternMatcher; +import org.apache.shiro.web.filter.PathMatchingFilter; import org.apache.shiro.web.util.WebUtils; import org.owasp.encoder.Encode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import jakarta.servlet.Filter; import jakarta.servlet.FilterChain; import jakarta.servlet.FilterConfig; import jakarta.servlet.ServletRequest; @@ -83,11 +85,18 @@ public PatternMatcher getPathMatcher() { * matches a configured filter chain. Unless overridden, the * default implementation is an {@link AntPathMatcher AntPathMatcher}. * + *

+ * The matcher is also applied to every {@link PathMatchingFilter} already registered with the current + * {@link #getFilterChainManager() FilterChainManager}, so that a filter re-checks the request path with the same + * matching semantics that selected its chain. Filters registered with the manager afterwards are not updated; + * call this method again once they have been added. + * * @param pathMatcher the {@code PatternMatcher} used when determining if an incoming request's path * matches a configured filter chain. */ public void setPathMatcher(PatternMatcher pathMatcher) { this.pathMatcher = pathMatcher; + applyPathMatcherToFilters(); } public boolean isCaseInsensitive() { @@ -110,6 +119,20 @@ public FilterChainManager getFilterChainManager() { @SuppressWarnings({"UnusedDeclaration"}) public void setFilterChainManager(FilterChainManager filterChainManager) { this.filterChainManager = filterChainManager; + applyPathMatcherToFilters(); + } + + private void applyPathMatcherToFilters() { + // A filter keeps its own matcher and re-matches the path in preHandle; if it differs from the one that + // selected the chain, the filter can silently skip the request. + if (pathMatcher == null || filterChainManager == null || filterChainManager.getFilters() == null) { + return; + } + for (Filter filter : filterChainManager.getFilters().values()) { + if (filter instanceof PathMatchingFilter pathMatchingFilter) { + pathMatchingFilter.setPathMatcher(pathMatcher); + } + } } @Override diff --git a/web/src/test/java/org/apache/shiro/web/filter/mgt/PathMatchingFilterChainResolverTest.java b/web/src/test/java/org/apache/shiro/web/filter/mgt/PathMatchingFilterChainResolverTest.java index afee0690f3..b2cfcef0b4 100644 --- a/web/src/test/java/org/apache/shiro/web/filter/mgt/PathMatchingFilterChainResolverTest.java +++ b/web/src/test/java/org/apache/shiro/web/filter/mgt/PathMatchingFilterChainResolverTest.java @@ -19,6 +19,8 @@ package org.apache.shiro.web.filter.mgt; import org.apache.shiro.util.AntPathMatcher; +import org.apache.shiro.util.RegExPatternMatcher; +import org.apache.shiro.web.filter.PathMatchingFilter; import org.apache.shiro.web.WebTest; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -79,6 +81,65 @@ void testSetters() { assertThat(resolver.getFilterChainManager() instanceof DefaultFilterChainManager).isTrue(); } + @Test + void testPathMatcherReachesFiltersAlreadyInManager() throws Exception { + RejectingFilter filter = new RejectingFilter(); + resolver.getFilterChainManager().addFilter("reject", filter); + + resolver.setPathMatcher(new RegExPatternMatcher()); + resolver.getFilterChainManager().addToChain("/secure/[^/]+", "reject"); + + assertThat(isRejected(filter, "/secure/index.html")).isTrue(); + } + + @Test + void testPathMatcherReachesFiltersOfReplacedManager() throws Exception { + RejectingFilter filter = new RejectingFilter(); + DefaultFilterChainManager manager = new DefaultFilterChainManager(); + manager.addFilter("reject", filter); + manager.addToChain("/secure/[^/]+", "reject"); + + resolver.setPathMatcher(new RegExPatternMatcher()); + resolver.setFilterChainManager(manager); + + assertThat(isRejected(filter, "/secure/index.html")).isTrue(); + } + + @Test + void testPathMatcherReachesFiltersAddedBeforeReapplying() throws Exception { + resolver.setPathMatcher(new RegExPatternMatcher()); + RejectingFilter filter = new RejectingFilter(); + resolver.getFilterChainManager().addFilter("reject", filter); + resolver.getFilterChainManager().addToChain("/secure/[^/]+", "reject"); + + resolver.setPathMatcher(resolver.getPathMatcher()); + + assertThat(isRejected(filter, "/secure/index.html")).isTrue(); + } + + private boolean isRejected(RejectingFilter filter, String path) throws Exception { + HttpServletRequest request = mock(HttpServletRequest.class); + HttpServletResponse response = mock(HttpServletResponse.class); + when(request.getServletPath()).thenReturn(""); + when(request.getPathInfo()).thenReturn(path); + + FilterChain resolved = resolver.getChain(request, response, mock(FilterChain.class)); + assertThat(resolved).isNotNull(); + return !filter.runPreHandle(request, response); + } + + /** Stops the chain for every path it is configured for, like an authentication filter without credentials. */ + private static final class RejectingFilter extends PathMatchingFilter { + boolean runPreHandle(ServletRequest request, ServletResponse response) throws Exception { + return preHandle(request, response); + } + + @Override + protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) { + return false; + } + } + @Test void testGetChainsWithoutChains() { ServletRequest request = mock(HttpServletRequest.class); From 7b6105a0f093ab6f6807c2f336b21b4d5345485c Mon Sep 17 00:00:00 2001 From: Shinya Murakami Date: Tue, 24 Nov 2026 08:23:07 +0900 Subject: [PATCH 2/2] [#2969] - Trim comments and a redundant test Drop the comment that repeated the Javadoc, the null check on getFilters() that DefaultFilterChainManager never needs, and the test for re-applying the matcher, which only backed the removed Javadoc note. Co-Authored-By: Claude Opus 5.5 --- .../filter/mgt/PathMatchingFilterChainResolver.java | 9 ++------- .../mgt/PathMatchingFilterChainResolverTest.java | 13 ------------- 2 files changed, 2 insertions(+), 20 deletions(-) diff --git a/web/src/main/java/org/apache/shiro/web/filter/mgt/PathMatchingFilterChainResolver.java b/web/src/main/java/org/apache/shiro/web/filter/mgt/PathMatchingFilterChainResolver.java index 766b1a1b0c..cbf8b9cf07 100644 --- a/web/src/main/java/org/apache/shiro/web/filter/mgt/PathMatchingFilterChainResolver.java +++ b/web/src/main/java/org/apache/shiro/web/filter/mgt/PathMatchingFilterChainResolver.java @@ -86,10 +86,7 @@ public PatternMatcher getPathMatcher() { * default implementation is an {@link AntPathMatcher AntPathMatcher}. * *

- * The matcher is also applied to every {@link PathMatchingFilter} already registered with the current - * {@link #getFilterChainManager() FilterChainManager}, so that a filter re-checks the request path with the same - * matching semantics that selected its chain. Filters registered with the manager afterwards are not updated; - * call this method again once they have been added. + * The matcher is also set on every {@link PathMatchingFilter} currently in the {@link FilterChainManager}. * * @param pathMatcher the {@code PatternMatcher} used when determining if an incoming request's path * matches a configured filter chain. @@ -123,9 +120,7 @@ public void setFilterChainManager(FilterChainManager filterChainManager) { } private void applyPathMatcherToFilters() { - // A filter keeps its own matcher and re-matches the path in preHandle; if it differs from the one that - // selected the chain, the filter can silently skip the request. - if (pathMatcher == null || filterChainManager == null || filterChainManager.getFilters() == null) { + if (pathMatcher == null || filterChainManager == null) { return; } for (Filter filter : filterChainManager.getFilters().values()) { diff --git a/web/src/test/java/org/apache/shiro/web/filter/mgt/PathMatchingFilterChainResolverTest.java b/web/src/test/java/org/apache/shiro/web/filter/mgt/PathMatchingFilterChainResolverTest.java index b2cfcef0b4..3c7d0dcdac 100644 --- a/web/src/test/java/org/apache/shiro/web/filter/mgt/PathMatchingFilterChainResolverTest.java +++ b/web/src/test/java/org/apache/shiro/web/filter/mgt/PathMatchingFilterChainResolverTest.java @@ -105,18 +105,6 @@ void testPathMatcherReachesFiltersOfReplacedManager() throws Exception { assertThat(isRejected(filter, "/secure/index.html")).isTrue(); } - @Test - void testPathMatcherReachesFiltersAddedBeforeReapplying() throws Exception { - resolver.setPathMatcher(new RegExPatternMatcher()); - RejectingFilter filter = new RejectingFilter(); - resolver.getFilterChainManager().addFilter("reject", filter); - resolver.getFilterChainManager().addToChain("/secure/[^/]+", "reject"); - - resolver.setPathMatcher(resolver.getPathMatcher()); - - assertThat(isRejected(filter, "/secure/index.html")).isTrue(); - } - private boolean isRejected(RejectingFilter filter, String path) throws Exception { HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); @@ -128,7 +116,6 @@ private boolean isRejected(RejectingFilter filter, String path) throws Exception return !filter.runPreHandle(request, response); } - /** Stops the chain for every path it is configured for, like an authentication filter without credentials. */ private static final class RejectingFilter extends PathMatchingFilter { boolean runPreHandle(ServletRequest request, ServletResponse response) throws Exception { return preHandle(request, response);