|
49 | 49 | import org.springframework.security.authentication.AuthenticationProvider;
|
50 | 50 | import org.springframework.security.authentication.AuthenticationServiceException;
|
51 | 51 | import org.springframework.security.authentication.ProviderManager;
|
| 52 | +import org.springframework.security.config.Customizer; |
52 | 53 | import org.springframework.security.config.annotation.ObjectPostProcessor;
|
53 | 54 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
54 | 55 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
|
80 | 81 | import org.springframework.security.saml2.provider.service.servlet.Saml2AuthenticationRequestRepository;
|
81 | 82 | import org.springframework.security.saml2.provider.service.servlet.filter.Saml2WebSsoAuthenticationFilter;
|
82 | 83 | import org.springframework.security.saml2.provider.service.web.Saml2AuthenticationRequestContextResolver;
|
| 84 | +import org.springframework.security.saml2.provider.service.web.Saml2AuthenticationTokenConverter; |
83 | 85 | import org.springframework.security.web.FilterChainProxy;
|
84 | 86 | import org.springframework.security.web.SecurityFilterChain;
|
85 | 87 | import org.springframework.security.web.authentication.AuthenticationConverter;
|
@@ -223,6 +225,26 @@ public void authenticateWhenCustomAuthenticationConverterThenUses() throws Excep
|
223 | 225 | verify(CustomAuthenticationConverter.authenticationConverter).convert(any(HttpServletRequest.class));
|
224 | 226 | }
|
225 | 227 |
|
| 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 | + |
226 | 248 | @Test
|
227 | 249 | public void authenticateWithInvalidDeflatedSAMLResponseThenFailureHandlerUses() throws Exception {
|
228 | 250 | this.spring.register(CustomAuthenticationFailureHandler.class).autowire();
|
@@ -447,6 +469,27 @@ protected void configure(HttpSecurity http) throws Exception {
|
447 | 469 |
|
448 | 470 | }
|
449 | 471 |
|
| 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 | + |
450 | 493 | @EnableWebSecurity
|
451 | 494 | @Import(Saml2LoginConfigBeans.class)
|
452 | 495 | static class CustomAuthenticationRequestRepository {
|
|
0 commit comments