LoginPageGeneratingWebFilter conditionally renders formLogin
Issue: gh-4807
This commit is contained in:
@@ -17,6 +17,8 @@
|
||||
package org.springframework.security.config.web.server;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.openqa.selenium.By;
|
||||
import org.openqa.selenium.NoSuchElementException;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.WebElement;
|
||||
import org.openqa.selenium.support.FindBy;
|
||||
@@ -36,6 +38,8 @@ import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
/**
|
||||
* @author Rob Winch
|
||||
@@ -204,9 +208,10 @@ public class FormLoginTests {
|
||||
|
||||
private LoginForm loginForm;
|
||||
|
||||
private OAuth2Login oauth2Login = new OAuth2Login();
|
||||
|
||||
public DefaultLoginPage(WebDriver webDriver) {
|
||||
this.driver = webDriver;
|
||||
this.loginForm = PageFactory.initElements(webDriver, LoginForm.class);
|
||||
}
|
||||
|
||||
static DefaultLoginPage create(WebDriver driver) {
|
||||
@@ -228,10 +233,23 @@ public class FormLoginTests {
|
||||
return this;
|
||||
}
|
||||
|
||||
public DefaultLoginPage assertLoginFormNotPresent() {
|
||||
assertThatThrownBy(() -> loginForm().username(""))
|
||||
.isInstanceOf(NoSuchElementException.class);
|
||||
return this;
|
||||
}
|
||||
|
||||
public LoginForm loginForm() {
|
||||
if (this.loginForm == null) {
|
||||
this.loginForm = PageFactory.initElements(this.driver, LoginForm.class);
|
||||
}
|
||||
return this.loginForm;
|
||||
}
|
||||
|
||||
public OAuth2Login oauth2Login() {
|
||||
return this.oauth2Login;
|
||||
}
|
||||
|
||||
static DefaultLoginPage to(WebDriver driver) {
|
||||
driver.get("http://localhost/login");
|
||||
return PageFactory.initElements(driver, DefaultLoginPage.class);
|
||||
@@ -263,6 +281,22 @@ public class FormLoginTests {
|
||||
return PageFactory.initElements(this.driver, page);
|
||||
}
|
||||
}
|
||||
|
||||
public class OAuth2Login {
|
||||
public WebElement findClientRegistrationByName(String clientName) {
|
||||
return DefaultLoginPage.this.driver.findElement(By.linkText(clientName));
|
||||
}
|
||||
|
||||
public OAuth2Login assertClientRegistrationByName(String clientName) {
|
||||
assertThatCode(() -> findClientRegistrationByName(clientName))
|
||||
.doesNotThrowAnyException();
|
||||
return this;
|
||||
}
|
||||
|
||||
public DefaultLoginPage and() {
|
||||
return DefaultLoginPage.this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class DefaultLogoutPage {
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright 2002-2018 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 static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.WebElement;
|
||||
import org.openqa.selenium.support.FindBy;
|
||||
import org.openqa.selenium.support.PageFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
|
||||
import org.springframework.security.config.annotation.web.reactive.ServerHttpSecurityConfigurationBuilder;
|
||||
import org.springframework.security.config.oauth2.client.CommonOAuth2Provider;
|
||||
import org.springframework.security.config.test.SpringTestRule;
|
||||
import org.springframework.security.htmlunit.server.WebTestClientHtmlUnitDriverBuilder;
|
||||
import org.springframework.security.oauth2.client.registration.ClientRegistration;
|
||||
import org.springframework.security.oauth2.client.registration.InMemoryReactiveClientRegistrationRepository;
|
||||
import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository;
|
||||
import org.springframework.security.test.web.reactive.server.WebTestClientBuilder;
|
||||
import org.springframework.security.web.server.SecurityWebFilterChain;
|
||||
import org.springframework.security.web.server.WebFilterChainProxy;
|
||||
import org.springframework.security.web.server.authentication.RedirectServerAuthenticationSuccessHandler;
|
||||
import org.springframework.security.web.server.csrf.CsrfToken;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* @author Rob Winch
|
||||
* @since 5.1
|
||||
*/
|
||||
public class OAuth2LoginTests {
|
||||
|
||||
@Rule
|
||||
public final SpringTestRule spring = new SpringTestRule();
|
||||
|
||||
@Autowired
|
||||
private WebFilterChainProxy springSecurity;
|
||||
|
||||
private ClientRegistration github = CommonOAuth2Provider.GITHUB
|
||||
.getBuilder("github")
|
||||
.clientId("client")
|
||||
.clientSecret("secret")
|
||||
.build();
|
||||
|
||||
@Test
|
||||
public void defaultLoginPageWithMultipleClientRegistrationsThenLinks() {
|
||||
this.spring.register(OAuth2LoginWithMulitpleClientRegistrations.class).autowire();
|
||||
|
||||
WebTestClient webTestClient = WebTestClientBuilder
|
||||
.bindToWebFilters(this.springSecurity)
|
||||
.build();
|
||||
|
||||
WebDriver driver = WebTestClientHtmlUnitDriverBuilder
|
||||
.webTestClientSetup(webTestClient)
|
||||
.build();
|
||||
|
||||
FormLoginTests.DefaultLoginPage loginPage = FormLoginTests.HomePage
|
||||
.to(driver, FormLoginTests.DefaultLoginPage.class)
|
||||
.assertAt()
|
||||
.assertLoginFormNotPresent()
|
||||
.oauth2Login()
|
||||
.assertClientRegistrationByName(this.github.getClientName())
|
||||
.and();
|
||||
}
|
||||
|
||||
@EnableWebFluxSecurity
|
||||
static class OAuth2LoginWithMulitpleClientRegistrations {
|
||||
@Bean
|
||||
InMemoryReactiveClientRegistrationRepository clientRegistrationRepository() {
|
||||
ClientRegistration github = CommonOAuth2Provider.GITHUB
|
||||
.getBuilder("github")
|
||||
.clientId("client")
|
||||
.clientSecret("secret")
|
||||
.build();
|
||||
ClientRegistration google = CommonOAuth2Provider.GOOGLE
|
||||
.getBuilder("google")
|
||||
.clientId("client")
|
||||
.clientSecret("secret")
|
||||
.build();
|
||||
return new InMemoryReactiveClientRegistrationRepository(github, google);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user