|
37 | 37 | import org.springframework.mock.web.MockHttpServletResponse;
|
38 | 38 | import org.springframework.mock.web.MockHttpSession;
|
39 | 39 | import org.springframework.security.authentication.TestingAuthenticationToken;
|
| 40 | +import org.springframework.security.config.annotation.ObjectPostProcessor; |
40 | 41 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
41 | 42 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
42 | 43 | import org.springframework.security.config.test.SpringTestContext;
|
|
59 | 60 | import org.springframework.security.saml2.provider.service.registration.Saml2MessageBinding;
|
60 | 61 | import org.springframework.security.saml2.provider.service.registration.TestRelyingPartyRegistrations;
|
61 | 62 | import org.springframework.security.saml2.provider.service.web.authentication.logout.HttpSessionLogoutRequestRepository;
|
| 63 | +import org.springframework.security.saml2.provider.service.web.authentication.logout.Saml2LogoutRequestFilter; |
62 | 64 | import org.springframework.security.saml2.provider.service.web.authentication.logout.Saml2LogoutRequestRepository;
|
63 | 65 | import org.springframework.security.saml2.provider.service.web.authentication.logout.Saml2LogoutRequestResolver;
|
| 66 | +import org.springframework.security.saml2.provider.service.web.authentication.logout.Saml2LogoutResponseFilter; |
64 | 67 | import org.springframework.security.saml2.provider.service.web.authentication.logout.Saml2LogoutResponseResolver;
|
65 | 68 | import org.springframework.security.web.SecurityFilterChain;
|
| 69 | +import org.springframework.security.web.authentication.logout.LogoutFilter; |
66 | 70 | import org.springframework.security.web.authentication.logout.LogoutHandler;
|
67 | 71 | import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
|
| 72 | +import org.springframework.security.web.util.matcher.AntPathRequestMatcher; |
68 | 73 | import org.springframework.test.web.servlet.MockMvc;
|
69 | 74 | import org.springframework.test.web.servlet.MvcResult;
|
70 | 75 |
|
|
75 | 80 | import static org.mockito.BDDMockito.mock;
|
76 | 81 | import static org.mockito.BDDMockito.verify;
|
77 | 82 | import static org.mockito.BDDMockito.verifyNoInteractions;
|
| 83 | +import static org.mockito.Mockito.atLeastOnce; |
| 84 | +import static org.mockito.Mockito.spy; |
78 | 85 | import static org.springframework.security.config.Customizer.withDefaults;
|
79 | 86 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication;
|
80 | 87 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
|
@@ -346,6 +353,47 @@ public void saml2LogoutResponseWhenCustomLogoutResponseHandlerThenUses() throws
|
346 | 353 | verify(getBean(Saml2LogoutResponseValidator.class)).validate(any());
|
347 | 354 | }
|
348 | 355 |
|
| 356 | + @Test |
| 357 | + public void saml2LogoutWhenLogoutGetThenLogsOutAndSendsLogoutRequest() throws Exception { |
| 358 | + this.spring.register(Saml2LogoutWithHttpGet.class).autowire(); |
| 359 | + MvcResult result = this.mvc.perform(get("/logout").with(authentication(this.user))) |
| 360 | + .andExpect(status().isFound()).andReturn(); |
| 361 | + String location = result.getResponse().getHeader("Location"); |
| 362 | + LogoutHandler logoutHandler = this.spring.getContext().getBean(LogoutHandler.class); |
| 363 | + assertThat(location).startsWith("https://ap.example.org/logout/saml2/request"); |
| 364 | + verify(logoutHandler).logout(any(), any(), any()); |
| 365 | + } |
| 366 | + |
| 367 | + @Test |
| 368 | + public void saml2LogoutWhenSaml2LogoutRequestFilterPostProcessedThenUses() { |
| 369 | + |
| 370 | + Saml2DefaultsWithObjectPostProcessorConfig.objectPostProcessor = spy(ReflectingObjectPostProcessor.class); |
| 371 | + this.spring.register(Saml2DefaultsWithObjectPostProcessorConfig.class).autowire(); |
| 372 | + verify(Saml2DefaultsWithObjectPostProcessorConfig.objectPostProcessor) |
| 373 | + .postProcess(any(Saml2LogoutRequestFilter.class)); |
| 374 | + |
| 375 | + } |
| 376 | + |
| 377 | + @Test |
| 378 | + public void saml2LogoutWhenSaml2LogoutResponseFilterPostProcessedThenUses() { |
| 379 | + |
| 380 | + Saml2DefaultsWithObjectPostProcessorConfig.objectPostProcessor = spy(ReflectingObjectPostProcessor.class); |
| 381 | + this.spring.register(Saml2DefaultsWithObjectPostProcessorConfig.class).autowire(); |
| 382 | + verify(Saml2DefaultsWithObjectPostProcessorConfig.objectPostProcessor) |
| 383 | + .postProcess(any(Saml2LogoutResponseFilter.class)); |
| 384 | + |
| 385 | + } |
| 386 | + |
| 387 | + @Test |
| 388 | + public void saml2LogoutWhenLogoutFilterPostProcessedThenUses() { |
| 389 | + |
| 390 | + Saml2DefaultsWithObjectPostProcessorConfig.objectPostProcessor = spy(ReflectingObjectPostProcessor.class); |
| 391 | + this.spring.register(Saml2DefaultsWithObjectPostProcessorConfig.class).autowire(); |
| 392 | + verify(Saml2DefaultsWithObjectPostProcessorConfig.objectPostProcessor, atLeastOnce()) |
| 393 | + .postProcess(any(LogoutFilter.class)); |
| 394 | + |
| 395 | + } |
| 396 | + |
349 | 397 | private <T> T getBean(Class<T> clazz) {
|
350 | 398 | return this.spring.getContext().getBean(clazz);
|
351 | 399 | }
|
@@ -401,6 +449,61 @@ LogoutSuccessHandler logoutSuccessHandler() {
|
401 | 449 |
|
402 | 450 | }
|
403 | 451 |
|
| 452 | + @EnableWebSecurity |
| 453 | + @Import(Saml2LoginConfigBeans.class) |
| 454 | + static class Saml2LogoutWithHttpGet { |
| 455 | + |
| 456 | + LogoutHandler mockLogoutHandler = mock(LogoutHandler.class); |
| 457 | + |
| 458 | + @Bean |
| 459 | + SecurityFilterChain web(HttpSecurity http) throws Exception { |
| 460 | + // @formatter:off |
| 461 | + http |
| 462 | + .authorizeRequests((authorize) -> authorize.anyRequest().authenticated()) |
| 463 | + .logout((logout) -> logout.addLogoutHandler(this.mockLogoutHandler)) |
| 464 | + .saml2Login(withDefaults()) |
| 465 | + .saml2Logout((saml2) -> saml2.addObjectPostProcessor(new ObjectPostProcessor<LogoutFilter>() { |
| 466 | + @Override |
| 467 | + public <O extends LogoutFilter> O postProcess(O filter) { |
| 468 | + filter.setLogoutRequestMatcher(new AntPathRequestMatcher("/logout", "GET")); |
| 469 | + return filter; |
| 470 | + } |
| 471 | + })); |
| 472 | + return http.build(); |
| 473 | + // @formatter:on |
| 474 | + } |
| 475 | + |
| 476 | + @Bean |
| 477 | + LogoutHandler logoutHandler() { |
| 478 | + return this.mockLogoutHandler; |
| 479 | + } |
| 480 | + |
| 481 | + } |
| 482 | + |
| 483 | + @EnableWebSecurity |
| 484 | + @Import(Saml2LoginConfigBeans.class) |
| 485 | + static class Saml2DefaultsWithObjectPostProcessorConfig { |
| 486 | + |
| 487 | + static ObjectPostProcessor<Object> objectPostProcessor; |
| 488 | + |
| 489 | + @Bean |
| 490 | + SecurityFilterChain web(HttpSecurity http) throws Exception { |
| 491 | + // @formatter:off |
| 492 | + http |
| 493 | + .authorizeRequests((authorize) -> authorize.anyRequest().authenticated()) |
| 494 | + .saml2Login(withDefaults()) |
| 495 | + .saml2Logout(withDefaults()); |
| 496 | + return http.build(); |
| 497 | + // @formatter:on |
| 498 | + } |
| 499 | + |
| 500 | + @Bean |
| 501 | + static ObjectPostProcessor<Object> objectPostProcessor() { |
| 502 | + return objectPostProcessor; |
| 503 | + } |
| 504 | + |
| 505 | + } |
| 506 | + |
404 | 507 | @EnableWebSecurity
|
405 | 508 | @Import(Saml2LoginConfigBeans.class)
|
406 | 509 | static class Saml2LogoutComponentsConfig {
|
@@ -490,4 +593,13 @@ private Consumer<Collection<Saml2X509Credential>> credential(Saml2X509Credential
|
490 | 593 |
|
491 | 594 | }
|
492 | 595 |
|
| 596 | + static class ReflectingObjectPostProcessor implements ObjectPostProcessor<Object> { |
| 597 | + |
| 598 | + @Override |
| 599 | + public <O> O postProcess(O object) { |
| 600 | + return object; |
| 601 | + } |
| 602 | + |
| 603 | + } |
| 604 | + |
493 | 605 | }
|
0 commit comments