|
42 | 42 | import org.springframework.mock.web.MockHttpServletResponse;
|
43 | 43 | import org.springframework.mock.web.MockHttpSession;
|
44 | 44 | import org.springframework.security.authentication.AuthenticationManager;
|
| 45 | +import org.springframework.security.authentication.AuthenticationProvider; |
45 | 46 | import org.springframework.security.authentication.AuthenticationServiceException;
|
46 | 47 | import org.springframework.security.config.Customizer;
|
47 | 48 | import org.springframework.security.config.annotation.SecurityContextChangedListenerConfig;
|
|
59 | 60 | import org.springframework.security.saml2.core.Saml2Utils;
|
60 | 61 | import org.springframework.security.saml2.core.TestSaml2X509Credentials;
|
61 | 62 | import org.springframework.security.saml2.provider.service.authentication.AbstractSaml2AuthenticationRequest;
|
| 63 | +import org.springframework.security.saml2.provider.service.authentication.OpenSaml4AuthenticationProvider; |
62 | 64 | import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticatedPrincipal;
|
63 | 65 | import org.springframework.security.saml2.provider.service.authentication.Saml2Authentication;
|
64 | 66 | import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationException;
|
@@ -353,6 +355,15 @@ public void getFaviconWhenDefaultConfigurationThenDoesNotSaveAuthnRequest() thro
|
353 | 355 | .andExpect(redirectedUrl("http://localhost/saml2/authenticate/registration-id"));
|
354 | 356 | }
|
355 | 357 |
|
| 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 | + |
356 | 367 | private void performSaml2Login(String expected) throws IOException, ServletException {
|
357 | 368 | // setup authentication parameters
|
358 | 369 | this.request.setRequestURI("/login/saml2/sso/registration-id");
|
@@ -663,6 +674,29 @@ Saml2AuthenticationTokenConverter authenticationTokenConverter() {
|
663 | 674 |
|
664 | 675 | }
|
665 | 676 |
|
| 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 | + |
666 | 700 | static class Saml2LoginConfigBeans {
|
667 | 701 |
|
668 | 702 | @Bean
|
|
0 commit comments