From 69338ecdfac441f106d3ca42a6fc2125efb7db3c Mon Sep 17 00:00:00 2001 From: Marcus Da Coregio Date: Tue, 2 May 2023 10:12:46 -0300 Subject: [PATCH] Only Observe AuthenticationManager if it is not null Closes gh-13084 --- .../annotation/web/builders/HttpSecurity.java | 2 +- .../HttpSecurityConfigurationTests.java | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) 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 da2bc756f5d..1eb2e279414 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 @@ -2998,7 +2998,7 @@ protected void beforeConfigure() throws Exception { else { ObservationRegistry registry = getObservationRegistry(); AuthenticationManager manager = getAuthenticationRegistry().build(); - if (!registry.isNoop()) { + if (!registry.isNoop() && manager != null) { setSharedObject(AuthenticationManager.class, new ObservationAuthenticationManager(registry, manager)); } else { diff --git a/config/src/test/java/org/springframework/security/config/annotation/web/configuration/HttpSecurityConfigurationTests.java b/config/src/test/java/org/springframework/security/config/annotation/web/configuration/HttpSecurityConfigurationTests.java index 15d9d997971..5a92e4342e5 100644 --- a/config/src/test/java/org/springframework/security/config/annotation/web/configuration/HttpSecurityConfigurationTests.java +++ b/config/src/test/java/org/springframework/security/config/annotation/web/configuration/HttpSecurityConfigurationTests.java @@ -22,6 +22,7 @@ import java.util.concurrent.Callable; import com.google.common.net.HttpHeaders; +import io.micrometer.observation.ObservationRegistry; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.junit.jupiter.api.Test; @@ -39,6 +40,8 @@ import org.springframework.mock.web.MockHttpSession; import org.springframework.security.access.AccessDeniedException; import org.springframework.security.authentication.AuthenticationEventPublisher; +import org.springframework.security.authentication.AuthenticationProvider; +import org.springframework.security.authentication.TestingAuthenticationProvider; import org.springframework.security.authentication.TestingAuthenticationToken; import org.springframework.security.authentication.event.AbstractAuthenticationEvent; import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent; @@ -46,6 +49,7 @@ import org.springframework.security.config.annotation.SecurityContextChangedListenerConfig; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; +import org.springframework.security.config.annotation.web.configurers.AnonymousConfigurer; import org.springframework.security.config.test.SpringTestContext; import org.springframework.security.config.test.SpringTestContextExtension; import org.springframework.security.core.Authentication; @@ -325,6 +329,13 @@ public void getWhenCustomContentNegotiationStrategyThenUses() throws Exception { .resolveMediaTypes(any(NativeWebRequest.class)); } + // gh-13084 + @Test + public void configureWhenNoAuthenticationManagerAndObservationRegistryNotNoOpThenConfigure() throws Exception { + this.spring.register(ObservationConfig.class, NoAuthenticationManagerConfig.class).autowire(); + this.mockMvc.perform(get("/")); + } + @RestController static class NameController { @@ -532,6 +543,38 @@ static ContentNegotiationStrategy cns() { } + @Configuration + @EnableWebSecurity + static class NoAuthenticationManagerConfig { + + @Bean + SecurityFilterChain apiSecurity(HttpSecurity http) throws Exception { + http.anonymous(AnonymousConfigurer::disable); + return http.build(); + } + + @Bean + AuthenticationProvider authenticationProvider1() { + return new TestingAuthenticationProvider(); + } + + @Bean + AuthenticationProvider authenticationProvider2() { + return new TestingAuthenticationProvider(); + } + + } + + @Configuration + static class ObservationConfig { + + @Bean + ObservationRegistry observationRegistry() { + return ObservationRegistry.create(); + } + + } + static class DefaultConfigurer extends AbstractHttpConfigurer { boolean init;