Skip to content

Commit 0d24e2b

Browse files
committed
Fix WebFlux logout disabling
Fixes: gh-7682
1 parent b00999d commit 0d24e2b

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

config/src/main/java/org/springframework/security/config/web/server/ServerHttpSecurity.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2775,7 +2775,9 @@ public ServerHttpSecurity disable() {
27752775
protected void configure(ServerHttpSecurity http) {
27762776
if (this.csrfTokenRepository != null) {
27772777
this.filter.setCsrfTokenRepository(this.csrfTokenRepository);
2778-
http.logout().addLogoutHandler(new CsrfServerLogoutHandler(this.csrfTokenRepository));
2778+
if (ServerHttpSecurity.this.logout != null) {
2779+
ServerHttpSecurity.this.logout.addLogoutHandler(new CsrfServerLogoutHandler(this.csrfTokenRepository));
2780+
}
27792781
}
27802782
http.addFilterAt(this.filter, SecurityWebFiltersOrder.CSRF);
27812783
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,4 +458,25 @@ public void logoutWhenXMLHttpRequestThenReturnsStatusNoContent() throws Exceptio
458458
@EnableWebSecurity
459459
static class BasicSecurityConfig extends WebSecurityConfigurerAdapter {
460460
}
461+
462+
@Test
463+
public void logoutWhenDisabledThenLogoutUrlNotFound() throws Exception {
464+
this.spring.register(LogoutDisabledConfig.class).autowire();
465+
466+
this.mvc.perform(post("/logout")
467+
.with(csrf()))
468+
.andExpect(status().isNotFound());
469+
}
470+
471+
@EnableWebSecurity
472+
static class LogoutDisabledConfig extends WebSecurityConfigurerAdapter {
473+
@Override
474+
protected void configure(HttpSecurity http) throws Exception {
475+
// @formatter:off
476+
http
477+
.logout()
478+
.disable();
479+
// @formatter:on
480+
}
481+
}
461482
}

config/src/test/java/org/springframework/security/config/web/server/LogoutSpecTests.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,40 @@ public void logoutWhenCustomLogoutInLambdaThenCustomLogoutUsed() {
164164
.assertAt()
165165
.assertLogout();
166166
}
167+
168+
@Test
169+
public void logoutWhenDisabledThenPostToLogoutDoesNothing() {
170+
SecurityWebFilterChain securityWebFilter = this.http
171+
.authorizeExchange()
172+
.anyExchange().authenticated()
173+
.and()
174+
.formLogin().and()
175+
.logout().disable()
176+
.build();
177+
178+
WebTestClient webTestClient = WebTestClientBuilder
179+
.bindToWebFilters(securityWebFilter)
180+
.build();
181+
182+
WebDriver driver = WebTestClientHtmlUnitDriverBuilder
183+
.webTestClientSetup(webTestClient)
184+
.build();
185+
186+
FormLoginTests.DefaultLoginPage loginPage = FormLoginTests.HomePage.to(driver, FormLoginTests.DefaultLoginPage.class)
187+
.assertAt();
188+
189+
FormLoginTests.HomePage homePage = loginPage.loginForm()
190+
.username("user")
191+
.password("password")
192+
.submit(FormLoginTests.HomePage.class);
193+
194+
homePage.assertAt();
195+
196+
FormLoginTests.DefaultLogoutPage.to(driver)
197+
.assertAt()
198+
.logout();
199+
200+
homePage
201+
.assertAt();
202+
}
167203
}

0 commit comments

Comments
 (0)