Skip to content

Commit

Permalink
Disable default logout page when logout disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
eleftherias authored and Ayush Kohli committed Aug 25, 2021
1 parent 19255ec commit fb7a1ac
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -97,7 +97,10 @@ public void configure(H http) {
if (this.loginPageGeneratingFilter.isEnabled() && authenticationEntryPoint == null) {
this.loginPageGeneratingFilter = postProcess(this.loginPageGeneratingFilter);
http.addFilter(this.loginPageGeneratingFilter);
http.addFilter(this.logoutPageGeneratingFilter);
LogoutConfigurer<H> logoutConfigurer = http.getConfigurer(LogoutConfigurer.class);
if (logoutConfigurer != null) {
http.addFilter(this.logoutPageGeneratingFilter);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2229,7 +2229,10 @@ protected void configure(ServerHttpSecurity http) {
}
if (loginPage != null) {
http.addFilterAt(loginPage, SecurityWebFiltersOrder.LOGIN_PAGE_GENERATING);
http.addFilterAt(new LogoutPageGeneratingWebFilter(), SecurityWebFiltersOrder.LOGOUT_PAGE_GENERATING);
if (http.logout != null) {
http.addFilterAt(new LogoutPageGeneratingWebFilter(),
SecurityWebFiltersOrder.LOGOUT_PAGE_GENERATING);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -46,11 +46,14 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.springframework.security.config.Customizer.withDefaults;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
* Tests for {@link DefaultLoginPageConfigurer}
Expand Down Expand Up @@ -375,6 +378,18 @@ public void configureWhenAuthenticationEntryPointThenNoDefaultLoginPageGeneratin
.isZero();
}

@Test
public void formLoginWhenLogoutEnabledThenCreatesDefaultLogoutPage() throws Exception {
this.spring.register(DefaultLogoutPageConfig.class).autowire();
this.mvc.perform(get("/logout").with(user("user"))).andExpect(status().isOk());
}

@Test
public void formLoginWhenLogoutDisabledThenDefaultLogoutPageDoesNotExist() throws Exception {
this.spring.register(LogoutDisabledConfig.class).autowire();
this.mvc.perform(get("/logout").with(user("user"))).andExpect(status().isNotFound());
}

@EnableWebSecurity
static class DefaultLoginPageConfig extends WebSecurityConfigurerAdapter {

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

}

@EnableWebSecurity
static class DefaultLogoutPageConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.authorizeRequests((authorize) -> authorize
.anyRequest().authenticated()
)
.formLogin(withDefaults());
// @formatter:on
}

}

@EnableWebSecurity
static class LogoutDisabledConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.authorizeRequests((authorize) -> authorize
.anyRequest().authenticated()
)
.formLogin(withDefaults())
.logout((logout) -> logout
.disable()
);
// @formatter:on
}

}

static class ReflectingObjectPostProcessor implements ObjectPostProcessor<Object> {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,7 +26,10 @@
import org.springframework.security.web.server.context.WebSessionServerSecurityContextRepository;
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatchers;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.security.config.Customizer.withDefaults;

/**
Expand Down Expand Up @@ -146,7 +149,7 @@ public void logoutWhenCustomLogoutInLambdaThenCustomLogoutUsed() {
}

@Test
public void logoutWhenDisabledThenPostToLogoutDoesNothing() {
public void logoutWhenDisabledThenDefaultLogoutPageDoesNotExist() {
// @formatter:off
SecurityWebFilterChain securityWebFilter = this.http
.authorizeExchange()
Expand All @@ -156,7 +159,7 @@ public void logoutWhenDisabledThenPostToLogoutDoesNothing() {
.logout().disable()
.build();
WebTestClient webTestClient = WebTestClientBuilder
.bindToWebFilters(securityWebFilter)
.bindToControllerAndWebFilters(HomeController.class, securityWebFilter)
.build();
WebDriver driver = WebTestClientHtmlUnitDriverBuilder
.webTestClientSetup(webTestClient)
Expand All @@ -171,8 +174,8 @@ public void logoutWhenDisabledThenPostToLogoutDoesNothing() {
.submit(FormLoginTests.HomePage.class);
// @formatter:on
homePage.assertAt();
FormLoginTests.DefaultLogoutPage.to(driver).assertAt().logout();
homePage.assertAt();
FormLoginTests.DefaultLogoutPage.to(driver);
assertThat(driver.getPageSource()).isEmpty();
}

@Test
Expand Down Expand Up @@ -208,4 +211,14 @@ public void logoutWhenCustomSecurityContextRepositoryThenLogsOut() {
FormLoginTests.HomePage.to(driver, FormLoginTests.DefaultLoginPage.class).assertAt();
}

@RestController
public static class HomeController {

@GetMapping("/")
public String ok() {
return "ok";
}

}

}

0 comments on commit fb7a1ac

Please sign in to comment.