diff --git a/spring-session-docs/src/docs/asciidoc/index.adoc b/spring-session-docs/src/docs/asciidoc/index.adoc index 59d472fa..7b2658fa 100644 --- a/spring-session-docs/src/docs/asciidoc/index.adoc +++ b/spring-session-docs/src/docs/asciidoc/index.adoc @@ -74,6 +74,10 @@ To get started with Spring Session, the best place to start is our Sample Applic | Demonstrates how to use Spring Session to replace the `HttpSession` with Redis using JSON serialization. | +| {gh-samples-url}spring-session-sample-boot-redis-simple[HttpSession with simple Redis `SessionRepository`] +| Demonstrates how to use Spring Session to replace the `HttpSession` with Redis using `SimpleRedisOperationsSessionRepository`. +| + |=== .Sample Applications that use Spring Java-based configuration diff --git a/spring-session-samples/spring-session-sample-boot-redis-simple/spring-session-sample-boot-redis-simple.gradle b/spring-session-samples/spring-session-sample-boot-redis-simple/spring-session-sample-boot-redis-simple.gradle new file mode 100644 index 00000000..dafb79e4 --- /dev/null +++ b/spring-session-samples/spring-session-sample-boot-redis-simple/spring-session-sample-boot-redis-simple.gradle @@ -0,0 +1,21 @@ +apply plugin: 'io.spring.convention.spring-sample-boot' + +dependencies { + compile project(':spring-session-data-redis') + compile 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect' + compile 'org.springframework.boot:spring-boot-starter-data-redis' + compile 'org.springframework.boot:spring-boot-starter-security' + compile 'org.springframework.boot:spring-boot-starter-thymeleaf' + compile 'org.springframework.boot:spring-boot-starter-web' + compile 'org.springframework.boot:spring-boot-devtools' + compile 'org.webjars:bootstrap' + compile 'org.webjars:html5shiv' + compile 'org.webjars:webjars-locator-core' + + testCompile 'org.junit.jupiter:junit-jupiter-api' + testRuntime 'org.junit.jupiter:junit-jupiter-engine' + testCompile 'org.springframework.boot:spring-boot-starter-test' + + integrationTestCompile seleniumDependencies + integrationTestCompile 'org.testcontainers:testcontainers' +} diff --git a/spring-session-samples/spring-session-sample-boot-redis-simple/src/integration-test/java/sample/BootTests.java b/spring-session-samples/spring-session-sample-boot-redis-simple/src/integration-test/java/sample/BootTests.java new file mode 100644 index 00000000..b52edceb --- /dev/null +++ b/spring-session-samples/spring-session-sample-boot-redis-simple/src/integration-test/java/sample/BootTests.java @@ -0,0 +1,103 @@ +/* + * Copyright 2014-2019 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 sample; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.openqa.selenium.WebDriver; +import org.testcontainers.containers.GenericContainer; +import sample.pages.HomePage; +import sample.pages.LoginPage; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDriverBuilder; + +@ExtendWith(SpringExtension.class) +@AutoConfigureMockMvc +@SpringBootTest(webEnvironment = WebEnvironment.MOCK) +class BootTests { + + private static final String DOCKER_IMAGE = "redis:5.0.5"; + + @Autowired + private MockMvc mockMvc; + + private WebDriver driver; + + @BeforeEach + void setup() { + this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).build(); + } + + @AfterEach + void tearDown() { + this.driver.quit(); + } + + @Test + void home() { + LoginPage login = HomePage.go(this.driver); + login.assertAt(); + } + + @Test + void login() { + LoginPage login = HomePage.go(this.driver); + HomePage home = login.form().login(HomePage.class); + home.assertAt(); + home.containCookie("SESSION"); + home.doesNotContainCookie("JSESSIONID"); + } + + @Test + void logout() { + LoginPage login = HomePage.go(this.driver); + HomePage home = login.form().login(HomePage.class); + home.logout(); + login.assertAt(); + } + + @TestConfiguration + static class Config { + + @Bean + public GenericContainer redisContainer() { + GenericContainer redisContainer = new GenericContainer(DOCKER_IMAGE) + .withExposedPorts(6379); + redisContainer.start(); + return redisContainer; + } + + @Bean + public LettuceConnectionFactory redisConnectionFactory() { + return new LettuceConnectionFactory(redisContainer().getContainerIpAddress(), + redisContainer().getFirstMappedPort()); + } + + } + +} diff --git a/spring-session-samples/spring-session-sample-boot-redis-simple/src/integration-test/java/sample/pages/BasePage.java b/spring-session-samples/spring-session-sample-boot-redis-simple/src/integration-test/java/sample/pages/BasePage.java new file mode 100644 index 00000000..9159c078 --- /dev/null +++ b/spring-session-samples/spring-session-sample-boot-redis-simple/src/integration-test/java/sample/pages/BasePage.java @@ -0,0 +1,38 @@ +/* + * Copyright 2014-2019 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 sample.pages; + +import org.openqa.selenium.WebDriver; + +public class BasePage { + + private WebDriver driver; + + public BasePage(WebDriver driver) { + this.driver = driver; + } + + public WebDriver getDriver() { + return this.driver; + } + + public static void get(WebDriver driver, String get) { + String baseUrl = "http://localhost"; + driver.get(baseUrl + get); + } + +} diff --git a/spring-session-samples/spring-session-sample-boot-redis-simple/src/integration-test/java/sample/pages/HomePage.java b/spring-session-samples/spring-session-sample-boot-redis-simple/src/integration-test/java/sample/pages/HomePage.java new file mode 100644 index 00000000..2e0c28e0 --- /dev/null +++ b/spring-session-samples/spring-session-sample-boot-redis-simple/src/integration-test/java/sample/pages/HomePage.java @@ -0,0 +1,62 @@ +/* + * Copyright 2014-2019 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 sample.pages; + +import java.util.Set; + +import org.openqa.selenium.By; +import org.openqa.selenium.Cookie; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.PageFactory; + +import static org.assertj.core.api.Assertions.assertThat; + +public class HomePage extends BasePage { + + public HomePage(WebDriver driver) { + super(driver); + } + + public static LoginPage go(WebDriver driver) { + get(driver, "/"); + return PageFactory.initElements(driver, LoginPage.class); + } + + public void assertAt() { + assertThat(getDriver().getTitle()) + .isEqualTo("Spring Session Sample - Secured Content"); + } + + public void containCookie(String cookieName) { + Set cookies = getDriver().manage().getCookies(); + assertThat(cookies).extracting("name").contains(cookieName); + } + + public void doesNotContainCookie(String cookieName) { + Set cookies = getDriver().manage().getCookies(); + assertThat(cookies).extracting("name").doesNotContain(cookieName); + } + + public HomePage logout() { + WebElement logout = getDriver() + .findElement(By.cssSelector("input[type=\"submit\"]")); + logout.click(); + return PageFactory.initElements(getDriver(), HomePage.class); + } + +} diff --git a/spring-session-samples/spring-session-sample-boot-redis-simple/src/integration-test/java/sample/pages/LoginPage.java b/spring-session-samples/spring-session-sample-boot-redis-simple/src/integration-test/java/sample/pages/LoginPage.java new file mode 100644 index 00000000..99fd0fae --- /dev/null +++ b/spring-session-samples/spring-session-sample-boot-redis-simple/src/integration-test/java/sample/pages/LoginPage.java @@ -0,0 +1,65 @@ +/* + * Copyright 2014-2019 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 sample.pages; + +import org.openqa.selenium.SearchContext; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; +import org.openqa.selenium.support.pagefactory.DefaultElementLocatorFactory; + +import static org.assertj.core.api.Assertions.assertThat; + +public class LoginPage extends BasePage { + + public LoginPage(WebDriver driver) { + super(driver); + } + + public void assertAt() { + assertThat(getDriver().getTitle()).isEqualTo("Please sign in"); + } + + public Form form() { + return new Form(getDriver()); + } + + public class Form { + + @FindBy(name = "username") + private WebElement username; + + @FindBy(name = "password") + private WebElement password; + + @FindBy(tagName = "button") + private WebElement button; + + public Form(SearchContext context) { + PageFactory.initElements(new DefaultElementLocatorFactory(context), this); + } + + public T login(Class page) { + this.username.sendKeys("user"); + this.password.sendKeys("password"); + this.button.click(); + return PageFactory.initElements(getDriver(), page); + } + } + +} diff --git a/spring-session-samples/spring-session-sample-boot-redis-simple/src/integration-test/resources/testcontainers.properties b/spring-session-samples/spring-session-sample-boot-redis-simple/src/integration-test/resources/testcontainers.properties new file mode 100644 index 00000000..e3e83419 --- /dev/null +++ b/spring-session-samples/spring-session-sample-boot-redis-simple/src/integration-test/resources/testcontainers.properties @@ -0,0 +1 @@ +ryuk.container.timeout=120 diff --git a/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/java/sample/Application.java b/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/java/sample/Application.java new file mode 100644 index 00000000..e9c3db94 --- /dev/null +++ b/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/java/sample/Application.java @@ -0,0 +1,29 @@ +/* + * Copyright 2014-2019 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 sample; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/java/sample/config/SecurityConfig.java b/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/java/sample/config/SecurityConfig.java new file mode 100644 index 00000000..3d6b979e --- /dev/null +++ b/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/java/sample/config/SecurityConfig.java @@ -0,0 +1,40 @@ +/* + * Copyright 2014-2019 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 sample.config; + +import org.springframework.boot.autoconfigure.security.servlet.PathRequest; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +@Configuration +public class SecurityConfig extends WebSecurityConfigurerAdapter { + + // @formatter:off + @Override + protected void configure(HttpSecurity http) throws Exception { + http + .authorizeRequests() + .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll() + .anyRequest().authenticated() + .and() + .formLogin() + .permitAll(); + } + // @formatter:on + +} diff --git a/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/java/sample/config/SessionConfig.java b/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/java/sample/config/SessionConfig.java new file mode 100644 index 00000000..c06f9558 --- /dev/null +++ b/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/java/sample/config/SessionConfig.java @@ -0,0 +1,52 @@ +/* + * Copyright 2014-2019 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 sample.config; + +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.context.annotation.Bean; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisOperations; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.StringRedisSerializer; +import org.springframework.session.config.annotation.web.http.EnableSpringHttpSession; +import org.springframework.session.data.redis.SimpleRedisOperationsSessionRepository; + +@EnableSpringHttpSession +public class SessionConfig { + + private final RedisConnectionFactory redisConnectionFactory; + + public SessionConfig(ObjectProvider redisConnectionFactory) { + this.redisConnectionFactory = redisConnectionFactory.getIfAvailable(); + } + + @Bean + public RedisOperations sessionRedisOperations() { + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setConnectionFactory(this.redisConnectionFactory); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setHashKeySerializer(new StringRedisSerializer()); + return redisTemplate; + } + + @Bean + public SimpleRedisOperationsSessionRepository sessionRepository( + RedisOperations sessionRedisOperations) { + return new SimpleRedisOperationsSessionRepository(sessionRedisOperations); + } + +} diff --git a/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/java/sample/config/WebMvcConfig.java b/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/java/sample/config/WebMvcConfig.java new file mode 100644 index 00000000..1c1a2925 --- /dev/null +++ b/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/java/sample/config/WebMvcConfig.java @@ -0,0 +1,31 @@ +/* + * Copyright 2014-2019 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 sample.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class WebMvcConfig implements WebMvcConfigurer { + + @Override + public void addViewControllers(ViewControllerRegistry registry) { + registry.addViewController("/").setViewName("index"); + } + +} diff --git a/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/resources/application.properties b/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/resources/application.properties new file mode 100644 index 00000000..1b5271b5 --- /dev/null +++ b/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.security.user.password=password diff --git a/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/resources/static/favicon.ico b/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/resources/static/favicon.ico new file mode 100644 index 00000000..bfb99740 Binary files /dev/null and b/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/resources/static/favicon.ico differ diff --git a/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/resources/static/images/logo.png b/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/resources/static/images/logo.png new file mode 100644 index 00000000..39323088 Binary files /dev/null and b/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/resources/static/images/logo.png differ diff --git a/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/resources/templates/index.html b/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/resources/templates/index.html new file mode 100644 index 00000000..769c73c2 --- /dev/null +++ b/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/resources/templates/index.html @@ -0,0 +1,11 @@ + + + Secured Content + + +
+

Secured Page

+

This page is secured using Spring Boot, Spring Session, and Spring Security.

+
+ + diff --git a/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/resources/templates/layout.html b/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/resources/templates/layout.html new file mode 100644 index 00000000..6c4dbdc6 --- /dev/null +++ b/spring-session-samples/spring-session-sample-boot-redis-simple/src/main/resources/templates/layout.html @@ -0,0 +1,128 @@ + + + + Spring Session Sample + + + + + + + + + + + +
+ + +
+
+ Some Success message +
+
+ Fake content +
+
+ +
+
+ + + +