Skip to content

Commit ca0140c

Browse files
committed
saml2Login Honors AuthenticationProvider bean
Closes gh-13654
1 parent 17e9fec commit ca0140c

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configurers/saml2/Saml2LoginConfigurer.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ public void init(B http) throws Exception {
269269
}
270270
}
271271
this.initDefaultLoginFilter(http);
272+
if (this.authenticationManager == null) {
273+
registerDefaultAuthenticationProvider(http);
274+
}
272275
}
273276

274277
/**
@@ -284,10 +287,7 @@ public void configure(B http) throws Exception {
284287
filter.setAuthenticationRequestRepository(getAuthenticationRequestRepository(http));
285288
http.addFilter(postProcess(filter));
286289
super.configure(http);
287-
if (this.authenticationManager == null) {
288-
registerDefaultAuthenticationProvider(http);
289-
}
290-
else {
290+
if (this.authenticationManager != null) {
291291
this.saml2WebSsoAuthenticationFilter.setAuthenticationManager(this.authenticationManager);
292292
}
293293
}
@@ -361,7 +361,10 @@ private AuthenticationConverter getAuthenticationConverter(B http) {
361361
}
362362

363363
private void registerDefaultAuthenticationProvider(B http) {
364-
http.authenticationProvider(postProcess(new OpenSaml4AuthenticationProvider()));
364+
OpenSaml4AuthenticationProvider provider = getBeanOrNull(http, OpenSaml4AuthenticationProvider.class);
365+
if (provider == null) {
366+
http.authenticationProvider(postProcess(new OpenSaml4AuthenticationProvider()));
367+
}
365368
}
366369

367370
private void registerDefaultCsrfOverride(B http) {

config/src/test/java/org/springframework/security/config/annotation/web/configurers/saml2/Saml2LoginConfigurerTests.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.mock.web.MockHttpServletResponse;
4444
import org.springframework.mock.web.MockHttpSession;
4545
import org.springframework.security.authentication.AuthenticationManager;
46+
import org.springframework.security.authentication.AuthenticationProvider;
4647
import org.springframework.security.authentication.AuthenticationServiceException;
4748
import org.springframework.security.config.Customizer;
4849
import org.springframework.security.config.annotation.SecurityContextChangedListenerConfig;
@@ -60,6 +61,7 @@
6061
import org.springframework.security.saml2.core.Saml2Utils;
6162
import org.springframework.security.saml2.core.TestSaml2X509Credentials;
6263
import org.springframework.security.saml2.provider.service.authentication.AbstractSaml2AuthenticationRequest;
64+
import org.springframework.security.saml2.provider.service.authentication.OpenSaml4AuthenticationProvider;
6365
import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticatedPrincipal;
6466
import org.springframework.security.saml2.provider.service.authentication.Saml2Authentication;
6567
import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationException;
@@ -358,6 +360,15 @@ public void getFaviconWhenDefaultConfigurationThenDoesNotSaveAuthnRequest() thro
358360
.andExpect(redirectedUrl("http://localhost/saml2/authenticate/registration-id"));
359361
}
360362

363+
@Test
364+
public void saml2LoginWhenCustomAuthenticationProviderThenUses() throws Exception {
365+
this.spring.register(CustomAuthenticationProviderConfig.class).autowire();
366+
AuthenticationProvider provider = this.spring.getContext().getBean(AuthenticationProvider.class);
367+
this.mvc.perform(post("/login/saml2/sso/registration-id").param("SAMLResponse", SIGNED_RESPONSE))
368+
.andExpect(status().isFound());
369+
verify(provider).authenticate(any());
370+
}
371+
361372
private void performSaml2Login(String expected) throws IOException, ServletException {
362373
// setup authentication parameters
363374
this.request.setRequestURI("/login/saml2/sso/registration-id");
@@ -668,6 +679,29 @@ Saml2AuthenticationTokenConverter authenticationTokenConverter() {
668679

669680
}
670681

682+
@Configuration
683+
@EnableWebSecurity
684+
@EnableWebMvc
685+
@Import(Saml2LoginConfigBeans.class)
686+
static class CustomAuthenticationProviderConfig {
687+
688+
private final OpenSaml4AuthenticationProvider provider = spy(new OpenSaml4AuthenticationProvider());
689+
690+
@Bean
691+
SecurityFilterChain web(HttpSecurity http) throws Exception {
692+
http.authorizeHttpRequests((authorize) -> authorize.anyRequest().authenticated())
693+
.saml2Login(Customizer.withDefaults());
694+
695+
return http.build();
696+
}
697+
698+
@Bean
699+
AuthenticationProvider provider() {
700+
return this.provider;
701+
}
702+
703+
}
704+
671705
static class Saml2LoginConfigBeans {
672706

673707
@Bean

0 commit comments

Comments
 (0)