Skip to content

Commit d2d1f19

Browse files
committed
Merge branch '6.0.x' into 6.1.x
Closes gh-13655
2 parents 35d8791 + ca0140c commit d2d1f19

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
@@ -268,6 +268,9 @@ public void init(B http) throws Exception {
268268
}
269269
}
270270
this.initDefaultLoginFilter(http);
271+
if (this.authenticationManager == null) {
272+
registerDefaultAuthenticationProvider(http);
273+
}
271274
}
272275

273276
/**
@@ -283,10 +286,7 @@ public void configure(B http) throws Exception {
283286
filter.setAuthenticationRequestRepository(getAuthenticationRequestRepository(http));
284287
http.addFilter(postProcess(filter));
285288
super.configure(http);
286-
if (this.authenticationManager == null) {
287-
registerDefaultAuthenticationProvider(http);
288-
}
289-
else {
289+
if (this.authenticationManager != null) {
290290
this.saml2WebSsoAuthenticationFilter.setAuthenticationManager(this.authenticationManager);
291291
}
292292
}
@@ -359,7 +359,10 @@ private AuthenticationConverter getAuthenticationConverter(B http) {
359359
}
360360

361361
private void registerDefaultAuthenticationProvider(B http) {
362-
http.authenticationProvider(postProcess(new OpenSaml4AuthenticationProvider()));
362+
OpenSaml4AuthenticationProvider provider = getBeanOrNull(http, OpenSaml4AuthenticationProvider.class);
363+
if (provider == null) {
364+
http.authenticationProvider(postProcess(new OpenSaml4AuthenticationProvider()));
365+
}
363366
}
364367

365368
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
@@ -42,6 +42,7 @@
4242
import org.springframework.mock.web.MockHttpServletResponse;
4343
import org.springframework.mock.web.MockHttpSession;
4444
import org.springframework.security.authentication.AuthenticationManager;
45+
import org.springframework.security.authentication.AuthenticationProvider;
4546
import org.springframework.security.authentication.AuthenticationServiceException;
4647
import org.springframework.security.config.Customizer;
4748
import org.springframework.security.config.annotation.SecurityContextChangedListenerConfig;
@@ -59,6 +60,7 @@
5960
import org.springframework.security.saml2.core.Saml2Utils;
6061
import org.springframework.security.saml2.core.TestSaml2X509Credentials;
6162
import org.springframework.security.saml2.provider.service.authentication.AbstractSaml2AuthenticationRequest;
63+
import org.springframework.security.saml2.provider.service.authentication.OpenSaml4AuthenticationProvider;
6264
import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticatedPrincipal;
6365
import org.springframework.security.saml2.provider.service.authentication.Saml2Authentication;
6466
import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationException;
@@ -353,6 +355,15 @@ public void getFaviconWhenDefaultConfigurationThenDoesNotSaveAuthnRequest() thro
353355
.andExpect(redirectedUrl("http://localhost/saml2/authenticate/registration-id"));
354356
}
355357

358+
@Test
359+
public void saml2LoginWhenCustomAuthenticationProviderThenUses() throws Exception {
360+
this.spring.register(CustomAuthenticationProviderConfig.class).autowire();
361+
AuthenticationProvider provider = this.spring.getContext().getBean(AuthenticationProvider.class);
362+
this.mvc.perform(post("/login/saml2/sso/registration-id").param("SAMLResponse", SIGNED_RESPONSE))
363+
.andExpect(status().isFound());
364+
verify(provider).authenticate(any());
365+
}
366+
356367
private void performSaml2Login(String expected) throws IOException, ServletException {
357368
// setup authentication parameters
358369
this.request.setRequestURI("/login/saml2/sso/registration-id");
@@ -663,6 +674,29 @@ Saml2AuthenticationTokenConverter authenticationTokenConverter() {
663674

664675
}
665676

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

668702
@Bean

0 commit comments

Comments
 (0)