|
50 | 50 | import org.springframework.mock.web.MockHttpServletResponse; |
51 | 51 | import org.springframework.mock.web.MockHttpSession; |
52 | 52 | import org.springframework.security.authentication.AuthenticationManager; |
| 53 | +import org.springframework.security.authentication.AuthenticationProvider; |
53 | 54 | import org.springframework.security.authentication.AuthenticationServiceException; |
54 | 55 | import org.springframework.security.config.Customizer; |
55 | 56 | import org.springframework.security.config.annotation.SecurityContextChangedListenerConfig; |
|
68 | 69 | import org.springframework.security.saml2.core.Saml2Utils; |
69 | 70 | import org.springframework.security.saml2.core.TestSaml2X509Credentials; |
70 | 71 | import org.springframework.security.saml2.provider.service.authentication.AbstractSaml2AuthenticationRequest; |
| 72 | +import org.springframework.security.saml2.provider.service.authentication.OpenSaml4AuthenticationProvider; |
71 | 73 | import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticatedPrincipal; |
72 | 74 | import org.springframework.security.saml2.provider.service.authentication.Saml2Authentication; |
73 | 75 | import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationException; |
@@ -390,6 +392,15 @@ public void getFaviconWhenDefaultConfigurationThenDoesNotSaveAuthnRequest() thro |
390 | 392 | .andExpect(redirectedUrl("http://localhost/saml2/authenticate/registration-id")); |
391 | 393 | } |
392 | 394 |
|
| 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 | + |
393 | 404 | private void performSaml2Login(String expected) throws IOException, ServletException { |
394 | 405 | // setup authentication parameters |
395 | 406 | this.request.setRequestURI("/login/saml2/sso/registration-id"); |
@@ -700,6 +711,29 @@ Saml2AuthenticationTokenConverter authenticationTokenConverter() { |
700 | 711 |
|
701 | 712 | } |
702 | 713 |
|
| 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 | + |
703 | 737 | static class Saml2LoginConfigBeans { |
704 | 738 |
|
705 | 739 | @Bean |
|
0 commit comments