From bf6e85ec15c170508a81debd0b92c9f97eaed891 Mon Sep 17 00:00:00 2001 From: Marcus Da Coregio Date: Wed, 5 Oct 2022 13:44:08 -0300 Subject: [PATCH] Accept String varargs in securityMatcher Issue gh-9159 --- .../annotation/web/builders/HttpSecurity.java | 49 +++++++++++++------ 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/config/src/main/java/org/springframework/security/config/annotation/web/builders/HttpSecurity.java b/config/src/main/java/org/springframework/security/config/annotation/web/builders/HttpSecurity.java index e687ef1e1e..4df8524f92 100644 --- a/config/src/main/java/org/springframework/security/config/annotation/web/builders/HttpSecurity.java +++ b/config/src/main/java/org/springframework/security/config/annotation/web/builders/HttpSecurity.java @@ -3599,12 +3599,12 @@ public final class HttpSecurity extends AbstractConfiguredSecurityBuilder * Invoking {@link #securityMatchers()} will not override previous invocations of * {@link #securityMatchers()}}, {@link #securityMatchers(Customizer)} - * {@link #securityMatcher(String)} and {@link #securityMatcher(RequestMatcher)} + * {@link #securityMatcher(String...)} and {@link #securityMatcher(RequestMatcher)} *

* *

Example Configurations

@@ -3720,12 +3720,12 @@ public final class HttpSecurity extends AbstractConfiguredSecurityBuilder * Invoking {@link #securityMatchers(Customizer)} will not override previous * invocations of {@link #securityMatchers()}}, {@link #securityMatchers(Customizer)} - * {@link #securityMatcher(String)} and {@link #securityMatcher(RequestMatcher)} + * {@link #securityMatcher(String...)} and {@link #securityMatcher(RequestMatcher)} *

* *

Example Configurations

@@ -3849,12 +3849,12 @@ public final class HttpSecurity extends AbstractConfiguredSecurityBuilder * @param requestMatcher the {@link RequestMatcher} to use (i.e. new * AntPathRequestMatcher("/admin/**","GET") ) * @return the {@link HttpSecurity} for further customizations - * @see #securityMatcher(String) + * @see #securityMatcher(String...) */ public HttpSecurity securityMatcher(RequestMatcher requestMatcher) { this.requestMatcher = requestMatcher; @@ -3869,21 +3869,35 @@ public final class HttpSecurity extends AbstractConfiguredSecurityBuilder - * Invoking {@link #securityMatcher(String)} will override previous invocations of + * Invoking {@link #securityMatcher(String...)} will override previous invocations of * {@link #mvcMatcher(String)}}, {@link #requestMatchers()}, * {@link #antMatcher(String)}, {@link #regexMatcher(String)}, and * {@link #requestMatcher(RequestMatcher)}. *

- * @param pattern the pattern to match on (i.e. "/admin/**") + * @param patterns the pattern to match on (i.e. "/admin/**") * @return the {@link HttpSecurity} for further customizations * @see AntPathRequestMatcher * @see MvcRequestMatcher */ - public HttpSecurity securityMatcher(String pattern) { - if (!mvcPresent) { - this.requestMatcher = new AntPathRequestMatcher(pattern); + public HttpSecurity securityMatcher(String... patterns) { + if (mvcPresent) { + this.requestMatcher = new OrRequestMatcher(createMvcMatchers(patterns)); return this; } + this.requestMatcher = new OrRequestMatcher(createAntMatchers(patterns)); + return this; + } + + private List createAntMatchers(String... patterns) { + List matchers = new ArrayList<>(patterns.length); + for (String pattern : patterns) { + matchers.add(new AntPathRequestMatcher(pattern)); + } + return matchers; + } + + private List createMvcMatchers(String... mvcPatterns) { + ObjectPostProcessor opp = getContext().getBean(ObjectPostProcessor.class); if (!getContext().containsBean(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME)) { throw new NoSuchBeanDefinitionException("A Bean named " + HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME + " of type " + HandlerMappingIntrospector.class.getName() @@ -3891,8 +3905,13 @@ public final class HttpSecurity extends AbstractConfiguredSecurityBuilder matchers = new ArrayList<>(mvcPatterns.length); + for (String mvcPattern : mvcPatterns) { + MvcRequestMatcher matcher = new MvcRequestMatcher(introspector, mvcPattern); + opp.postProcess(matcher); + matchers.add(matcher); + } + return matchers; } /** @@ -3908,7 +3927,7 @@ public final class HttpSecurity extends AbstractConfiguredSecurityBuilder * @param antPattern the Ant Pattern to match on (i.e. "/admin/**") * @return the {@link HttpSecurity} for further customizations - * @deprecated use {@link #securityMatcher(String)} instead + * @deprecated use {@link #securityMatcher(String...)} instead * @see AntPathRequestMatcher */ @Deprecated @@ -3929,7 +3948,7 @@ public final class HttpSecurity extends AbstractConfiguredSecurityBuilder * @param mvcPattern the Spring MVC Pattern to match on (i.e. "/admin/**") * @return the {@link HttpSecurity} for further customizations - * @deprecated use {@link #securityMatcher(String)} instead + * @deprecated use {@link #securityMatcher(String...)} instead * @see MvcRequestMatcher */ @Deprecated