Add ":servlet:java-configuration:hello-security"
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* 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 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;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
|
||||
|
||||
/**
|
||||
* Test the Hello World application.
|
||||
*
|
||||
* @author Michael Simons
|
||||
*/
|
||||
public class HelloWorldTests {
|
||||
|
||||
private WebDriver driver;
|
||||
|
||||
private int port;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
this.port = Integer.parseInt(System.getProperty("app.httpPort"));
|
||||
this.driver = new HtmlUnitDriver();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
this.driver.quit();
|
||||
}
|
||||
|
||||
@Test
|
||||
void accessHomePageWithUnauthenticatedUserSendsToLoginPage() {
|
||||
final LoginPage loginPage = HomePage.to(this.driver, this.port);
|
||||
loginPage.assertAt();
|
||||
}
|
||||
|
||||
@Test
|
||||
void authenticatedUserIsSentToOriginalPage() {
|
||||
// @formatter:off
|
||||
final HomePage homePage = HomePage.to(this.driver, this.port)
|
||||
.loginForm()
|
||||
.username("user")
|
||||
.password("password")
|
||||
.submit();
|
||||
// @formatter:on
|
||||
homePage.assertAt().andTheUserNameIsDisplayed();
|
||||
}
|
||||
|
||||
@Test
|
||||
void authenticatedUserLogsOut() {
|
||||
// @formatter:off
|
||||
LoginPage loginPage = HomePage.to(this.driver, this.port)
|
||||
.loginForm()
|
||||
.username("user")
|
||||
.password("password")
|
||||
.submit()
|
||||
.logout();
|
||||
// @formatter:on
|
||||
loginPage.assertAt();
|
||||
|
||||
loginPage = HomePage.to(this.driver, this.port);
|
||||
loginPage.assertAt();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* 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.pages;
|
||||
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.WebElement;
|
||||
import org.openqa.selenium.support.FindBy;
|
||||
import org.openqa.selenium.support.PageFactory;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* The home page.
|
||||
*
|
||||
* @author Michael Simons
|
||||
*/
|
||||
public class HomePage {
|
||||
|
||||
private final WebDriver webDriver;
|
||||
|
||||
@FindBy(css = "p")
|
||||
private WebElement message;
|
||||
|
||||
@FindBy(css = "input[type=submit]")
|
||||
private WebElement logoutButton;
|
||||
|
||||
public static LoginPage to(WebDriver driver, int port) {
|
||||
driver.get("http://localhost:" + port + "/");
|
||||
return PageFactory.initElements(driver, LoginPage.class);
|
||||
}
|
||||
|
||||
public HomePage(WebDriver webDriver) {
|
||||
this.webDriver = webDriver;
|
||||
}
|
||||
|
||||
public Content assertAt() {
|
||||
assertThat(this.webDriver.getTitle()).isEqualTo("Hello Security");
|
||||
return PageFactory.initElements(this.webDriver, Content.class);
|
||||
}
|
||||
|
||||
public LoginPage logout() {
|
||||
this.logoutButton.submit();
|
||||
return PageFactory.initElements(this.webDriver, LoginPage.class);
|
||||
}
|
||||
|
||||
public static class Content {
|
||||
|
||||
@FindBy(css = "p")
|
||||
private WebElement message;
|
||||
|
||||
public Content andTheUserNameIsDisplayed() {
|
||||
assertThat(this.message.getText()).isEqualTo("Hello user");
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* 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.pages;
|
||||
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.WebElement;
|
||||
import org.openqa.selenium.support.FindBy;
|
||||
import org.openqa.selenium.support.PageFactory;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* The log in page.
|
||||
*
|
||||
* @author Michael Simons
|
||||
*/
|
||||
public class LoginPage {
|
||||
|
||||
private final WebDriver webDriver;
|
||||
|
||||
private final LoginForm loginForm;
|
||||
|
||||
public LoginPage(WebDriver webDriver) {
|
||||
this.webDriver = webDriver;
|
||||
this.loginForm = PageFactory.initElements(this.webDriver, LoginForm.class);
|
||||
}
|
||||
|
||||
public LoginPage assertAt() {
|
||||
assertThat(this.webDriver.getTitle()).isEqualTo("Please sign in");
|
||||
return this;
|
||||
}
|
||||
|
||||
public LoginForm loginForm() {
|
||||
return this.loginForm;
|
||||
}
|
||||
|
||||
public static class LoginForm {
|
||||
|
||||
private WebDriver webDriver;
|
||||
|
||||
private WebElement username;
|
||||
|
||||
private WebElement password;
|
||||
|
||||
@FindBy(css = "button[type=submit]")
|
||||
private WebElement submit;
|
||||
|
||||
public LoginForm(WebDriver webDriver) {
|
||||
this.webDriver = webDriver;
|
||||
}
|
||||
|
||||
public LoginForm username(String username) {
|
||||
this.username.sendKeys(username);
|
||||
return this;
|
||||
}
|
||||
|
||||
public LoginForm password(String password) {
|
||||
this.password.sendKeys(password);
|
||||
return this;
|
||||
}
|
||||
|
||||
public HomePage submit() {
|
||||
this.submit.click();
|
||||
return PageFactory.initElements(this.webDriver, HomePage.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2002-2016 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.context.annotation.Bean;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
||||
|
||||
@EnableWebSecurity
|
||||
public class SecurityConfiguration {
|
||||
|
||||
// @formatter:off
|
||||
@Bean
|
||||
public UserDetailsService userDetailsService() {
|
||||
UserDetails user = User.withDefaultPasswordEncoder()
|
||||
.username("user")
|
||||
.password("password")
|
||||
.roles("USER")
|
||||
.build();
|
||||
return new InMemoryUserDetailsManager(user);
|
||||
}
|
||||
// @formatter:on
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.security.web.context.AbstractSecurityWebApplicationInitializer;
|
||||
|
||||
/**
|
||||
* No customizations of {@link AbstractSecurityWebApplicationInitializer} are necessary.
|
||||
*
|
||||
* @author Rob Winch
|
||||
*/
|
||||
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
|
||||
|
||||
public SecurityWebApplicationInitializer() {
|
||||
super(SecurityConfiguration.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="WARN">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
@@ -0,0 +1,2 @@
|
||||
Manifest-Version: 1.0
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
|
||||
xmlns:c="http://java.sun.com/jsp/jstl/core" version="2.0">
|
||||
|
||||
<jsp:directive.page contentType="text/html" pageEncoding="UTF-8" />
|
||||
<jsp:output omit-xml-declaration="true" />
|
||||
<jsp:output doctype-root-element="HTML"
|
||||
doctype-system="about:legacy-compat" />
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Hello Security</title>
|
||||
<c:url var="faviconUrl" value="/resources/img/favicon.ico"/>
|
||||
<link rel="icon" type="image/x-icon" href="${faviconUrl}"/>
|
||||
<c:url var="bootstrapUrl" value="/resources/css/bootstrap.css"/>
|
||||
<link href="${bootstrapUrl}" rel="stylesheet"></link>
|
||||
<c:url var="bootstrapResponsiveUrl" value="/resources/css/bootstrap-responsive.css"/>
|
||||
<link href="${bootstrapResponsiveUrl}" rel="stylesheet"></link>
|
||||
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
|
||||
<!--[if lt IE 9]>
|
||||
<script src="https://html5shim.googlecode.com/svn/trunk/html5.js"></script>
|
||||
<![endif]-->
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>This is secured!</h1>
|
||||
<p>
|
||||
Hello <b><c:out value="${pageContext.request.remoteUser}"/></b>
|
||||
</p>
|
||||
<c:url var="logoutUrl" value="/logout"/>
|
||||
<form class="form-inline" action="${logoutUrl}" method="post">
|
||||
<input type="submit" value="Log out" />
|
||||
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
</jsp:root>
|
||||
1092
servlet/java-configuration/hello-security/src/main/webapp/resources/css/bootstrap-responsive.css
vendored
Normal file
1092
servlet/java-configuration/hello-security/src/main/webapp/resources/css/bootstrap-responsive.css
vendored
Normal file
File diff suppressed because it is too large
Load Diff
6039
servlet/java-configuration/hello-security/src/main/webapp/resources/css/bootstrap.css
vendored
Normal file
6039
servlet/java-configuration/hello-security/src/main/webapp/resources/css/bootstrap.css
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Binary file not shown.
|
After Width: | Height: | Size: 1.1 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 1.1 KiB |
Reference in New Issue
Block a user