diff --git a/test/src/main/java/org/springframework/security/test/web/support/WebTestUtils.java b/test/src/main/java/org/springframework/security/test/web/support/WebTestUtils.java index d9be1dcf58..b42281dda0 100644 --- a/test/src/main/java/org/springframework/security/test/web/support/WebTestUtils.java +++ b/test/src/main/java/org/springframework/security/test/web/support/WebTestUtils.java @@ -114,7 +114,7 @@ public abstract class WebTestUtils { } @SuppressWarnings("unchecked") - private static T findFilter(HttpServletRequest request, + static T findFilter(HttpServletRequest request, Class filterClass) { WebApplicationContext webApplicationContext = WebApplicationContextUtils .getWebApplicationContext(request.getServletContext()); @@ -131,6 +131,9 @@ public abstract class WebTestUtils { } List filters = (List) ReflectionTestUtils.invokeMethod( springSecurityFilterChain, "getFilters", request); + if(filters == null) { + return null; + } for (Filter filter : filters) { if (filterClass.isAssignableFrom(filter.getClass())) { return (T) filter; diff --git a/test/src/test/java/org/springframework/security/test/web/support/WebTestUtilsTests.java b/test/src/test/java/org/springframework/security/test/web/support/WebTestUtilsTests.java index dbca9698a0..89b06342ef 100644 --- a/test/src/test/java/org/springframework/security/test/web/support/WebTestUtilsTests.java +++ b/test/src/test/java/org/springframework/security/test/web/support/WebTestUtilsTests.java @@ -32,6 +32,7 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.web.context.HttpSessionSecurityContextRepository; +import org.springframework.security.web.context.SecurityContextPersistenceFilter; import org.springframework.security.web.context.SecurityContextRepository; import org.springframework.security.web.csrf.CsrfTokenRepository; import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository; @@ -118,6 +119,14 @@ public class WebTestUtilsTests { assertThat(getSecurityContextRepository(request)).isSameAs(contextRepo); } + // gh-3343 + @Test + public void findFilterNoMatchingFilters() { + loadConfig(PartialSecurityConfig.class); + + assertThat(WebTestUtils.findFilter(request, SecurityContextPersistenceFilter.class)).isNull(); + } + private void loadConfig(Class config) { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.register(config); @@ -157,4 +166,18 @@ public class WebTestUtilsTests { } // @formatter:on } + + + + @EnableWebSecurity + static class PartialSecurityConfig extends WebSecurityConfigurerAdapter { + + // @formatter:off + @Override + public void configure(HttpSecurity http) throws Exception { + http + .antMatcher("/willnotmatchthis"); + } + // @formatter:on + } } \ No newline at end of file