Skip to content

Commit 0364518

Browse files
Update Saml2LoginConfigurer to pick up Saml2AuthenticationTokenConverter bean
Closes gh-10268
1 parent 1e76b11 commit 0364518

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,17 @@ private void setAuthenticationRequestRepository(B http,
268268
}
269269

270270
private AuthenticationConverter getAuthenticationConverter(B http) {
271-
if (this.authenticationConverter == null) {
271+
if (this.authenticationConverter != null) {
272+
return this.authenticationConverter;
273+
}
274+
AuthenticationConverter authenticationConverterBean = getBeanOrNull(http,
275+
Saml2AuthenticationTokenConverter.class);
276+
if (authenticationConverterBean == null) {
272277
return new Saml2AuthenticationTokenConverter(
273278
(RelyingPartyRegistrationResolver) new DefaultRelyingPartyRegistrationResolver(
274279
this.relyingPartyRegistrationRepository));
275280
}
276-
return this.authenticationConverter;
281+
return authenticationConverterBean;
277282
}
278283

279284
private String version() {

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

+43
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.springframework.security.authentication.AuthenticationProvider;
5050
import org.springframework.security.authentication.AuthenticationServiceException;
5151
import org.springframework.security.authentication.ProviderManager;
52+
import org.springframework.security.config.Customizer;
5253
import org.springframework.security.config.annotation.ObjectPostProcessor;
5354
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
5455
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@@ -80,6 +81,7 @@
8081
import org.springframework.security.saml2.provider.service.servlet.Saml2AuthenticationRequestRepository;
8182
import org.springframework.security.saml2.provider.service.servlet.filter.Saml2WebSsoAuthenticationFilter;
8283
import org.springframework.security.saml2.provider.service.web.Saml2AuthenticationRequestContextResolver;
84+
import org.springframework.security.saml2.provider.service.web.Saml2AuthenticationTokenConverter;
8385
import org.springframework.security.web.FilterChainProxy;
8486
import org.springframework.security.web.SecurityFilterChain;
8587
import org.springframework.security.web.authentication.AuthenticationConverter;
@@ -223,6 +225,26 @@ public void authenticateWhenCustomAuthenticationConverterThenUses() throws Excep
223225
verify(CustomAuthenticationConverter.authenticationConverter).convert(any(HttpServletRequest.class));
224226
}
225227

228+
@Test
229+
public void authenticateWhenCustomAuthenticationConverterBeanThenUses() throws Exception {
230+
this.spring.register(CustomAuthenticationConverterBean.class).autowire();
231+
Saml2AuthenticationTokenConverter authenticationConverter = this.spring.getContext()
232+
.getBean(Saml2AuthenticationTokenConverter.class);
233+
RelyingPartyRegistration relyingPartyRegistration = TestRelyingPartyRegistrations.noCredentials()
234+
.assertingPartyDetails((party) -> party.verificationX509Credentials(
235+
(c) -> c.add(TestSaml2X509Credentials.relyingPartyVerifyingCredential())))
236+
.build();
237+
String response = new String(Saml2Utils.samlDecode(SIGNED_RESPONSE));
238+
given(authenticationConverter.convert(any(HttpServletRequest.class)))
239+
.willReturn(new Saml2AuthenticationToken(relyingPartyRegistration, response));
240+
// @formatter:off
241+
MockHttpServletRequestBuilder request = post("/login/saml2/sso/" + relyingPartyRegistration.getRegistrationId())
242+
.param("SAMLResponse", SIGNED_RESPONSE);
243+
// @formatter:on
244+
this.mvc.perform(request).andExpect(redirectedUrl("/"));
245+
verify(authenticationConverter).convert(any(HttpServletRequest.class));
246+
}
247+
226248
@Test
227249
public void authenticateWithInvalidDeflatedSAMLResponseThenFailureHandlerUses() throws Exception {
228250
this.spring.register(CustomAuthenticationFailureHandler.class).autowire();
@@ -447,6 +469,27 @@ protected void configure(HttpSecurity http) throws Exception {
447469

448470
}
449471

472+
@EnableWebSecurity
473+
@Import(Saml2LoginConfigBeans.class)
474+
static class CustomAuthenticationConverterBean {
475+
476+
private final Saml2AuthenticationTokenConverter authenticationConverter = mock(
477+
Saml2AuthenticationTokenConverter.class);
478+
479+
@Bean
480+
SecurityFilterChain app(HttpSecurity http) throws Exception {
481+
http.authorizeHttpRequests((authz) -> authz.anyRequest().authenticated())
482+
.saml2Login(Customizer.withDefaults());
483+
return http.build();
484+
}
485+
486+
@Bean
487+
Saml2AuthenticationTokenConverter authenticationConverter() {
488+
return this.authenticationConverter;
489+
}
490+
491+
}
492+
450493
@EnableWebSecurity
451494
@Import(Saml2LoginConfigBeans.class)
452495
static class CustomAuthenticationRequestRepository {

0 commit comments

Comments
 (0)