Skip to content

Commit baf5eba

Browse files
committed
Add logoutRequestMatcher to Saml2LogoutConfigurer for custom matching
Closes: gh-10821 Signed-off-by: Andrey Litvitski <andrey1010102008@gmail.com>
1 parent b6ed037 commit baf5eba

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configurers/saml2/Saml2LogoutConfigurer.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
*
106106
* @author Josh Cummings
107107
* @author Ngoc Nhan
108+
* @author Andrey Litvitski
108109
* @since 5.6
109110
* @see Saml2LogoutConfigurer
110111
*/
@@ -127,6 +128,8 @@ public final class Saml2LogoutConfigurer<H extends HttpSecurityBuilder<H>>
127128

128129
private LogoutResponseConfigurer logoutResponseConfigurer;
129130

131+
private RequestMatcher logoutRequestMatcher;
132+
130133
/**
131134
* Creates a new instance
132135
* @see HttpSecurity#logout(Customizer)
@@ -195,6 +198,16 @@ public Saml2LogoutConfigurer<H> logoutResponse(
195198
return this;
196199
}
197200

201+
/**
202+
* Sets a custom {@link RequestMatcher} to use for SAML logout requests.
203+
* @param logoutRequestMatcher the matcher to use
204+
* @return the {@link Saml2LogoutConfigurer} for further customization
205+
*/
206+
public Saml2LogoutConfigurer<H> logoutRequestMatcher(RequestMatcher logoutRequestMatcher) {
207+
this.logoutRequestMatcher = logoutRequestMatcher;
208+
return this;
209+
}
210+
198211
/**
199212
* {@inheritDoc}
200213
*/
@@ -271,6 +284,9 @@ private Saml2RelyingPartyInitiatedLogoutFilter createRelyingPartyLogoutFilter(
271284
}
272285

273286
private RequestMatcher createLogoutMatcher() {
287+
if (this.logoutRequestMatcher != null) {
288+
return this.logoutRequestMatcher;
289+
}
274290
RequestMatcher logout = getRequestMatcherBuilder().matcher(HttpMethod.POST, this.logoutUrl);
275291
RequestMatcher saml2 = new Saml2RequestMatcher(getSecurityContextHolderStrategy());
276292
return new AndRequestMatcher(logout, saml2);

config/src/test/java/org/springframework/security/config/annotation/web/configurers/saml2/Saml2LogoutConfigurerTests.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,18 @@ public void saml2LogoutWhenLogoutFilterPostProcessedThenUses() {
543543

544544
}
545545

546+
// gh-10821
547+
@Test
548+
public void saml2LogoutWhenCustomLogoutRequestMatcherThenUsed() throws Exception {
549+
this.spring.register(Saml2LogoutCustomMatcherConfig.class).autowire();
550+
MvcResult result = this.mvc.perform(post("/saml/custom-logout").with(authentication(this.user)).with(csrf()))
551+
.andExpect(status().isFound())
552+
.andReturn();
553+
assertThat(result.getResponse().getHeader("Location"))
554+
.startsWith("https://ap.example.org/logout/saml2/request");
555+
verify(getBean(LogoutHandler.class)).logout(any(), any(), any());
556+
}
557+
546558
private <T> T getBean(Class<T> clazz) {
547559
return this.spring.getContext().getBean(clazz);
548560
}
@@ -577,6 +589,34 @@ LogoutHandler logoutHandler() {
577589

578590
}
579591

592+
@Configuration
593+
@EnableWebSecurity
594+
@Import(Saml2LoginConfigBeans.class)
595+
static class Saml2LogoutCustomMatcherConfig {
596+
597+
LogoutHandler mockLogoutHandler = mock(LogoutHandler.class);
598+
599+
@Bean
600+
SecurityFilterChain web(HttpSecurity http) throws Exception {
601+
// @formatter:off
602+
http
603+
.authorizeHttpRequests((authorize) -> authorize.anyRequest().authenticated())
604+
.logout((logout) -> logout.addLogoutHandler(this.mockLogoutHandler))
605+
.saml2Login(withDefaults())
606+
.saml2Logout((saml2) -> saml2
607+
.logoutRequestMatcher(pathPattern(HttpMethod.POST, "/saml/custom-logout"))
608+
);
609+
return http.build();
610+
// @formatter:on
611+
}
612+
613+
@Bean
614+
LogoutHandler logoutHandler() {
615+
return this.mockLogoutHandler;
616+
}
617+
618+
}
619+
580620
@Configuration
581621
@EnableWebSecurity
582622
@Import(Saml2LoginConfigBeans.class)

0 commit comments

Comments
 (0)