Skip to content

Commit 8d58113

Browse files
committed
Merge branch '6.1.x'
Closes gh-13656
2 parents 3ba5cc0 + d2d1f19 commit 8d58113

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
@@ -270,6 +270,9 @@ public void init(B http) throws Exception {
270270
}
271271
}
272272
this.initDefaultLoginFilter(http);
273+
if (this.authenticationManager == null) {
274+
registerDefaultAuthenticationProvider(http);
275+
}
273276
}
274277

275278
/**
@@ -285,10 +288,7 @@ public void configure(B http) throws Exception {
285288
filter.setAuthenticationRequestRepository(getAuthenticationRequestRepository(http));
286289
http.addFilter(postProcess(filter));
287290
super.configure(http);
288-
if (this.authenticationManager == null) {
289-
registerDefaultAuthenticationProvider(http);
290-
}
291-
else {
291+
if (this.authenticationManager != null) {
292292
this.saml2WebSsoAuthenticationFilter.setAuthenticationManager(this.authenticationManager);
293293
}
294294
}
@@ -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
@@ -50,6 +50,7 @@
5050
import org.springframework.mock.web.MockHttpServletResponse;
5151
import org.springframework.mock.web.MockHttpSession;
5252
import org.springframework.security.authentication.AuthenticationManager;
53+
import org.springframework.security.authentication.AuthenticationProvider;
5354
import org.springframework.security.authentication.AuthenticationServiceException;
5455
import org.springframework.security.config.Customizer;
5556
import org.springframework.security.config.annotation.SecurityContextChangedListenerConfig;
@@ -68,6 +69,7 @@
6869
import org.springframework.security.saml2.core.Saml2Utils;
6970
import org.springframework.security.saml2.core.TestSaml2X509Credentials;
7071
import org.springframework.security.saml2.provider.service.authentication.AbstractSaml2AuthenticationRequest;
72+
import org.springframework.security.saml2.provider.service.authentication.OpenSaml4AuthenticationProvider;
7173
import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticatedPrincipal;
7274
import org.springframework.security.saml2.provider.service.authentication.Saml2Authentication;
7375
import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationException;
@@ -390,6 +392,15 @@ public void getFaviconWhenDefaultConfigurationThenDoesNotSaveAuthnRequest() thro
390392
.andExpect(redirectedUrl("http://localhost/saml2/authenticate/registration-id"));
391393
}
392394

395+
@Test
396+
public void saml2LoginWhenCustomAuthenticationProviderThenUses() throws Exception {
397+
this.spring.register(CustomAuthenticationProviderConfig.class).autowire();
398+
AuthenticationProvider provider = this.spring.getContext().getBean(AuthenticationProvider.class);
399+
this.mvc.perform(post("/login/saml2/sso/registration-id").param("SAMLResponse", SIGNED_RESPONSE))
400+
.andExpect(status().isFound());
401+
verify(provider).authenticate(any());
402+
}
403+
393404
private void performSaml2Login(String expected) throws IOException, ServletException {
394405
// setup authentication parameters
395406
this.request.setRequestURI("/login/saml2/sso/registration-id");
@@ -700,6 +711,29 @@ Saml2AuthenticationTokenConverter authenticationTokenConverter() {
700711

701712
}
702713

714+
@Configuration
715+
@EnableWebSecurity
716+
@EnableWebMvc
717+
@Import(Saml2LoginConfigBeans.class)
718+
static class CustomAuthenticationProviderConfig {
719+
720+
private final OpenSaml4AuthenticationProvider provider = spy(new OpenSaml4AuthenticationProvider());
721+
722+
@Bean
723+
SecurityFilterChain web(HttpSecurity http) throws Exception {
724+
http.authorizeHttpRequests((authorize) -> authorize.anyRequest().authenticated())
725+
.saml2Login(Customizer.withDefaults());
726+
727+
return http.build();
728+
}
729+
730+
@Bean
731+
AuthenticationProvider provider() {
732+
return this.provider;
733+
}
734+
735+
}
736+
703737
static class Saml2LoginConfigBeans {
704738

705739
@Bean

0 commit comments

Comments
 (0)