3 changed files with 210 additions and 40 deletions
@ -0,0 +1,124 @@
@@ -0,0 +1,124 @@
|
||||
/* |
||||
* Copyright 2002-2017 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.security.config.web.server; |
||||
|
||||
import org.junit.Test; |
||||
import org.openqa.selenium.WebDriver; |
||||
import org.springframework.security.authentication.ReactiveAuthenticationManager; |
||||
import org.springframework.security.authentication.UserDetailsRepositoryAuthenticationManager; |
||||
import org.springframework.security.core.userdetails.MapUserDetailsRepository; |
||||
import org.springframework.security.core.userdetails.User; |
||||
import org.springframework.security.core.userdetails.UserDetails; |
||||
import org.springframework.security.htmlunit.server.WebTestClientHtmlUnitDriverBuilder; |
||||
import org.springframework.security.web.server.SecurityWebFilterChain; |
||||
import org.springframework.test.web.reactive.server.WebTestClient; |
||||
import org.springframework.security.test.web.reactive.server.WebTestClientBuilder; |
||||
|
||||
/** |
||||
* @author Shazin Sadakath |
||||
* @since 5.0 |
||||
*/ |
||||
public class LogoutBuilderTests { |
||||
|
||||
private UserDetails user = User.withUsername("user").password("password").roles("USER").build(); |
||||
private HttpSecurity http = HttpSecurity.http(); |
||||
|
||||
ReactiveAuthenticationManager manager = new UserDetailsRepositoryAuthenticationManager(new MapUserDetailsRepository(this.user)); |
||||
|
||||
@Test |
||||
public void defaultLogout() { |
||||
SecurityWebFilterChain securityWebFilter = this.http |
||||
.authenticationManager(this.manager) |
||||
.authorizeExchange() |
||||
.anyExchange().authenticated() |
||||
.and() |
||||
.formLogin().and() |
||||
.build(); |
||||
|
||||
WebTestClient webTestClient = WebTestClientBuilder |
||||
.bindToWebFilters(securityWebFilter) |
||||
.build(); |
||||
|
||||
WebDriver driver = WebTestClientHtmlUnitDriverBuilder |
||||
.webTestClientSetup(webTestClient) |
||||
.build(); |
||||
|
||||
FormLoginTests.DefaultLoginPage loginPage = FormLoginTests.HomePage.to(driver, FormLoginTests.DefaultLoginPage.class) |
||||
.assertAt(); |
||||
|
||||
loginPage = loginPage.loginForm() |
||||
.username("user") |
||||
.password("invalid") |
||||
.submit(FormLoginTests.DefaultLoginPage.class) |
||||
.assertError(); |
||||
|
||||
FormLoginTests.HomePage homePage = loginPage.loginForm() |
||||
.username("user") |
||||
.password("password") |
||||
.submit(FormLoginTests.HomePage.class); |
||||
|
||||
homePage.assertAt(); |
||||
|
||||
driver.get("http://localhost/logout"); |
||||
|
||||
FormLoginTests.DefaultLoginPage.create(driver) |
||||
.assertAt() |
||||
.assertLogout(); |
||||
} |
||||
|
||||
@Test |
||||
public void customLogout() { |
||||
SecurityWebFilterChain securityWebFilter = this.http |
||||
.authenticationManager(this.manager) |
||||
.authorizeExchange() |
||||
.anyExchange().authenticated() |
||||
.and() |
||||
.formLogin().and() |
||||
.logout().logoutUrl("/custom-logout").and() |
||||
.build(); |
||||
|
||||
WebTestClient webTestClient = WebTestClientBuilder |
||||
.bindToWebFilters(securityWebFilter) |
||||
.build(); |
||||
|
||||
WebDriver driver = WebTestClientHtmlUnitDriverBuilder |
||||
.webTestClientSetup(webTestClient) |
||||
.build(); |
||||
|
||||
FormLoginTests.DefaultLoginPage loginPage = FormLoginTests.HomePage.to(driver, FormLoginTests.DefaultLoginPage.class) |
||||
.assertAt(); |
||||
|
||||
loginPage = loginPage.loginForm() |
||||
.username("user") |
||||
.password("invalid") |
||||
.submit(FormLoginTests.DefaultLoginPage.class) |
||||
.assertError(); |
||||
|
||||
FormLoginTests.HomePage homePage = loginPage.loginForm() |
||||
.username("user") |
||||
.password("password") |
||||
.submit(FormLoginTests.HomePage.class); |
||||
|
||||
homePage.assertAt(); |
||||
|
||||
driver.get("http://localhost/custom-logout"); |
||||
|
||||
FormLoginTests.DefaultLoginPage.create(driver) |
||||
.assertAt() |
||||
.assertLogout(); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue