Skip to content

Commit cb4bb46

Browse files
committed
Disable default logout page when logout disabled
Closes gh-9475
1 parent 5fd81ee commit cb4bb46

File tree

4 files changed

+78
-9
lines changed

4 files changed

+78
-9
lines changed

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -97,7 +97,10 @@ public void configure(H http) {
9797
if (this.loginPageGeneratingFilter.isEnabled() && authenticationEntryPoint == null) {
9898
this.loginPageGeneratingFilter = postProcess(this.loginPageGeneratingFilter);
9999
http.addFilter(this.loginPageGeneratingFilter);
100-
http.addFilter(this.logoutPageGeneratingFilter);
100+
LogoutConfigurer<H> logoutConfigurer = http.getConfigurer(LogoutConfigurer.class);
101+
if (logoutConfigurer != null) {
102+
http.addFilter(this.logoutPageGeneratingFilter);
103+
}
101104
}
102105
}
103106

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -2223,7 +2223,10 @@ protected void configure(ServerHttpSecurity http) {
22232223
}
22242224
if (loginPage != null) {
22252225
http.addFilterAt(loginPage, SecurityWebFiltersOrder.LOGIN_PAGE_GENERATING);
2226-
http.addFilterAt(new LogoutPageGeneratingWebFilter(), SecurityWebFiltersOrder.LOGOUT_PAGE_GENERATING);
2226+
if (http.logout != null) {
2227+
http.addFilterAt(new LogoutPageGeneratingWebFilter(),
2228+
SecurityWebFiltersOrder.LOGOUT_PAGE_GENERATING);
2229+
}
22272230
}
22282231
}
22292232

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

+51-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,11 +46,14 @@
4646
import static org.mockito.ArgumentMatchers.any;
4747
import static org.mockito.Mockito.spy;
4848
import static org.mockito.Mockito.verify;
49+
import static org.springframework.security.config.Customizer.withDefaults;
4950
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
51+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
5052
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
5153
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
5254
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
5355
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
56+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
5457

5558
/**
5659
* Tests for {@link DefaultLoginPageConfigurer}
@@ -375,6 +378,18 @@ public void configureWhenAuthenticationEntryPointThenNoDefaultLoginPageGeneratin
375378
.isZero();
376379
}
377380

381+
@Test
382+
public void formLoginWhenLogoutEnabledThenCreatesDefaultLogoutPage() throws Exception {
383+
this.spring.register(DefaultLogoutPageConfig.class).autowire();
384+
this.mvc.perform(get("/logout").with(user("user"))).andExpect(status().isOk());
385+
}
386+
387+
@Test
388+
public void formLoginWhenLogoutDisabledThenDefaultLogoutPageDoesNotExist() throws Exception {
389+
this.spring.register(LogoutDisabledConfig.class).autowire();
390+
this.mvc.perform(get("/logout").with(user("user"))).andExpect(status().isNotFound());
391+
}
392+
378393
@EnableWebSecurity
379394
static class DefaultLoginPageConfig extends WebSecurityConfigurerAdapter {
380395

@@ -533,6 +548,41 @@ static ObjectPostProcessor<Object> objectPostProcessor() {
533548

534549
}
535550

551+
@EnableWebSecurity
552+
static class DefaultLogoutPageConfig extends WebSecurityConfigurerAdapter {
553+
554+
@Override
555+
protected void configure(HttpSecurity http) throws Exception {
556+
// @formatter:off
557+
http
558+
.authorizeRequests((authorize) -> authorize
559+
.anyRequest().authenticated()
560+
)
561+
.formLogin(withDefaults());
562+
// @formatter:on
563+
}
564+
565+
}
566+
567+
@EnableWebSecurity
568+
static class LogoutDisabledConfig extends WebSecurityConfigurerAdapter {
569+
570+
@Override
571+
protected void configure(HttpSecurity http) throws Exception {
572+
// @formatter:off
573+
http
574+
.authorizeRequests((authorize) -> authorize
575+
.anyRequest().authenticated()
576+
)
577+
.formLogin(withDefaults())
578+
.logout((logout) -> logout
579+
.disable()
580+
);
581+
// @formatter:on
582+
}
583+
584+
}
585+
536586
static class ReflectingObjectPostProcessor implements ObjectPostProcessor<Object> {
537587

538588
@Override

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

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,10 @@
2626
import org.springframework.security.web.server.context.WebSessionServerSecurityContextRepository;
2727
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatchers;
2828
import org.springframework.test.web.reactive.server.WebTestClient;
29+
import org.springframework.web.bind.annotation.GetMapping;
30+
import org.springframework.web.bind.annotation.RestController;
2931

32+
import static org.assertj.core.api.Assertions.assertThat;
3033
import static org.springframework.security.config.Customizer.withDefaults;
3134

3235
/**
@@ -146,7 +149,7 @@ public void logoutWhenCustomLogoutInLambdaThenCustomLogoutUsed() {
146149
}
147150

148151
@Test
149-
public void logoutWhenDisabledThenPostToLogoutDoesNothing() {
152+
public void logoutWhenDisabledThenDefaultLogoutPageDoesNotExist() {
150153
// @formatter:off
151154
SecurityWebFilterChain securityWebFilter = this.http
152155
.authorizeExchange()
@@ -156,7 +159,7 @@ public void logoutWhenDisabledThenPostToLogoutDoesNothing() {
156159
.logout().disable()
157160
.build();
158161
WebTestClient webTestClient = WebTestClientBuilder
159-
.bindToWebFilters(securityWebFilter)
162+
.bindToControllerAndWebFilters(HomeController.class, securityWebFilter)
160163
.build();
161164
WebDriver driver = WebTestClientHtmlUnitDriverBuilder
162165
.webTestClientSetup(webTestClient)
@@ -171,8 +174,8 @@ public void logoutWhenDisabledThenPostToLogoutDoesNothing() {
171174
.submit(FormLoginTests.HomePage.class);
172175
// @formatter:on
173176
homePage.assertAt();
174-
FormLoginTests.DefaultLogoutPage.to(driver).assertAt().logout();
175-
homePage.assertAt();
177+
FormLoginTests.DefaultLogoutPage.to(driver);
178+
assertThat(driver.getPageSource()).isEmpty();
176179
}
177180

178181
@Test
@@ -208,4 +211,14 @@ public void logoutWhenCustomSecurityContextRepositoryThenLogsOut() {
208211
FormLoginTests.HomePage.to(driver, FormLoginTests.DefaultLoginPage.class).assertAt();
209212
}
210213

214+
@RestController
215+
public static class HomeController {
216+
217+
@GetMapping("/")
218+
public String ok() {
219+
return "ok";
220+
}
221+
222+
}
223+
211224
}

0 commit comments

Comments
 (0)