Fix form login

Previously it wasn't using the custom log in page.
This commit is contained in:
Rob Winch 2020-10-28 10:49:46 -05:00
parent 97910bde82
commit 1c04cfc209
6 changed files with 59 additions and 23 deletions

View File

@ -15,8 +15,8 @@
*/
package example;
import example.pages.CustomLoginPage;
import example.pages.HomePage;
import example.pages.LoginPage;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -28,7 +28,7 @@ import org.openqa.selenium.htmlunit.HtmlUnitDriver;
*
* @author Michael Simons
*/
public class HelloWorldTests {
public class FormLoginTests {
private WebDriver driver;
@ -47,7 +47,7 @@ public class HelloWorldTests {
@Test
void accessHomePageWithUnauthenticatedUserSendsToLoginPage() {
final LoginPage loginPage = HomePage.to(this.driver, this.port);
final CustomLoginPage loginPage = HomePage.to(this.driver, this.port);
loginPage.assertAt();
}
@ -66,7 +66,7 @@ public class HelloWorldTests {
@Test
void authenticatedUserLogsOut() {
// @formatter:off
LoginPage loginPage = HomePage.to(this.driver, this.port)
CustomLoginPage loginPage = HomePage.to(this.driver, this.port)
.loginForm()
.username("user")
.password("password")

View File

@ -27,19 +27,19 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Michael Simons
*/
public class LoginPage {
public class CustomLoginPage {
private final WebDriver webDriver;
private final LoginForm loginForm;
public LoginPage(WebDriver webDriver) {
public CustomLoginPage(WebDriver webDriver) {
this.webDriver = webDriver;
this.loginForm = PageFactory.initElements(this.webDriver, LoginForm.class);
}
public LoginPage assertAt() {
assertThat(this.webDriver.getTitle()).isEqualTo("Please sign in");
public CustomLoginPage assertAt() {
assertThat(this.webDriver.getTitle()).isEqualTo("Custom Log In Page");
return this;
}

View File

@ -37,9 +37,9 @@ public class HomePage {
@FindBy(css = "input[type=submit]")
private WebElement logoutButton;
public static LoginPage to(WebDriver driver, int port) {
public static CustomLoginPage to(WebDriver driver, int port) {
driver.get("http://localhost:" + port + "/");
return PageFactory.initElements(driver, LoginPage.class);
return PageFactory.initElements(driver, CustomLoginPage.class);
}
public HomePage(WebDriver webDriver) {
@ -51,9 +51,9 @@ public class HomePage {
return this;
}
public LoginPage logout() {
public CustomLoginPage logout() {
this.logoutButton.submit();
return PageFactory.initElements(this.webDriver, LoginPage.class);
return PageFactory.initElements(this.webDriver, CustomLoginPage.class);
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright 2020 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
*
* https://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 example;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
/**
* Controller for the custom log in page.
*
* @author Rob WInch
*/
@Controller
public class LoginController {
@GetMapping("/login")
String login() {
return "login";
}
}

View File

@ -24,8 +24,6 @@ import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import static org.springframework.security.config.Customizer.withDefaults;
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@ -33,11 +31,13 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
// @formatter:off
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests((requests) -> requests
.anyRequest().authenticated()
)
.httpBasic(withDefaults())
.formLogin(withDefaults());
.authorizeRequests((requests) -> requests
.anyRequest().authenticated()
)
.formLogin((form) -> form
.loginPage("/login")
.permitAll()
);
}
// @formatter:on

View File

@ -1,9 +1,10 @@
<html xmlns:th="https://www.thymeleaf.org">
<head th:include="layout :: head(title=~{::title},links=~{})">
<title>Please Login</title>
<head>
<title>Custom Log In Page</title>
</head>
<body th:include="layout :: body" th:with="content=~{::content}">
<div th:fragment="content">
<body>
<h1>Custom Log In Page</h1>
<div>
<form name="f" th:action="@{/login}" method="post">
<fieldset>
<legend>Please Login</legend>