|
43 | 43 | import org.springframework.mock.web.MockHttpServletResponse;
|
44 | 44 | import org.springframework.mock.web.MockHttpSession;
|
45 | 45 | import org.springframework.security.authentication.AuthenticationManager;
|
| 46 | +import org.springframework.security.authentication.AuthenticationProvider; |
46 | 47 | import org.springframework.security.authentication.AuthenticationServiceException;
|
47 | 48 | import org.springframework.security.config.Customizer;
|
48 | 49 | import org.springframework.security.config.annotation.SecurityContextChangedListenerConfig;
|
|
60 | 61 | import org.springframework.security.saml2.core.Saml2Utils;
|
61 | 62 | import org.springframework.security.saml2.core.TestSaml2X509Credentials;
|
62 | 63 | import org.springframework.security.saml2.provider.service.authentication.AbstractSaml2AuthenticationRequest;
|
| 64 | +import org.springframework.security.saml2.provider.service.authentication.OpenSaml4AuthenticationProvider; |
63 | 65 | import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticatedPrincipal;
|
64 | 66 | import org.springframework.security.saml2.provider.service.authentication.Saml2Authentication;
|
65 | 67 | import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationException;
|
@@ -358,6 +360,15 @@ public void getFaviconWhenDefaultConfigurationThenDoesNotSaveAuthnRequest() thro
|
358 | 360 | .andExpect(redirectedUrl("http://localhost/saml2/authenticate/registration-id"));
|
359 | 361 | }
|
360 | 362 |
|
| 363 | + @Test |
| 364 | + public void saml2LoginWhenCustomAuthenticationProviderThenUses() throws Exception { |
| 365 | + this.spring.register(CustomAuthenticationProviderConfig.class).autowire(); |
| 366 | + AuthenticationProvider provider = this.spring.getContext().getBean(AuthenticationProvider.class); |
| 367 | + this.mvc.perform(post("/login/saml2/sso/registration-id").param("SAMLResponse", SIGNED_RESPONSE)) |
| 368 | + .andExpect(status().isFound()); |
| 369 | + verify(provider).authenticate(any()); |
| 370 | + } |
| 371 | + |
361 | 372 | private void performSaml2Login(String expected) throws IOException, ServletException {
|
362 | 373 | // setup authentication parameters
|
363 | 374 | this.request.setRequestURI("/login/saml2/sso/registration-id");
|
@@ -668,6 +679,29 @@ Saml2AuthenticationTokenConverter authenticationTokenConverter() {
|
668 | 679 |
|
669 | 680 | }
|
670 | 681 |
|
| 682 | + @Configuration |
| 683 | + @EnableWebSecurity |
| 684 | + @EnableWebMvc |
| 685 | + @Import(Saml2LoginConfigBeans.class) |
| 686 | + static class CustomAuthenticationProviderConfig { |
| 687 | + |
| 688 | + private final OpenSaml4AuthenticationProvider provider = spy(new OpenSaml4AuthenticationProvider()); |
| 689 | + |
| 690 | + @Bean |
| 691 | + SecurityFilterChain web(HttpSecurity http) throws Exception { |
| 692 | + http.authorizeHttpRequests((authorize) -> authorize.anyRequest().authenticated()) |
| 693 | + .saml2Login(Customizer.withDefaults()); |
| 694 | + |
| 695 | + return http.build(); |
| 696 | + } |
| 697 | + |
| 698 | + @Bean |
| 699 | + AuthenticationProvider provider() { |
| 700 | + return this.provider; |
| 701 | + } |
| 702 | + |
| 703 | + } |
| 704 | + |
671 | 705 | static class Saml2LoginConfigBeans {
|
672 | 706 |
|
673 | 707 | @Bean
|
|
0 commit comments