Skip to content

Commit 782167b

Browse files
committed
Add SimpleRedisOperationsSessionRepository sample
Resolves: #1408
1 parent 17005c5 commit 782167b

File tree

16 files changed

+586
-0
lines changed

16 files changed

+586
-0
lines changed

spring-session-docs/src/docs/asciidoc/index.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ To get started with Spring Session, the best place to start is our Sample Applic
7474
| Demonstrates how to use Spring Session to replace the `HttpSession` with Redis using JSON serialization.
7575
|
7676

77+
| {gh-samples-url}spring-session-sample-boot-redis-simple[HttpSession with simple Redis `SessionRepository`]
78+
| Demonstrates how to use Spring Session to replace the `HttpSession` with Redis using `SimpleRedisOperationsSessionRepository`.
79+
|
80+
7781
|===
7882

7983
.Sample Applications that use Spring Java-based configuration
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apply plugin: 'io.spring.convention.spring-sample-boot'
2+
3+
dependencies {
4+
compile project(':spring-session-data-redis')
5+
compile 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'
6+
compile 'org.springframework.boot:spring-boot-starter-data-redis'
7+
compile 'org.springframework.boot:spring-boot-starter-security'
8+
compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
9+
compile 'org.springframework.boot:spring-boot-starter-web'
10+
compile 'org.springframework.boot:spring-boot-devtools'
11+
compile 'org.webjars:bootstrap'
12+
compile 'org.webjars:html5shiv'
13+
compile 'org.webjars:webjars-locator-core'
14+
15+
testCompile 'org.junit.jupiter:junit-jupiter-api'
16+
testRuntime 'org.junit.jupiter:junit-jupiter-engine'
17+
testCompile 'org.springframework.boot:spring-boot-starter-test'
18+
19+
integrationTestCompile seleniumDependencies
20+
integrationTestCompile 'org.testcontainers:testcontainers'
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2014-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample;
18+
19+
import org.junit.jupiter.api.AfterEach;
20+
import org.junit.jupiter.api.BeforeEach;
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.extension.ExtendWith;
23+
import org.openqa.selenium.WebDriver;
24+
import org.testcontainers.containers.GenericContainer;
25+
import sample.pages.HomePage;
26+
import sample.pages.LoginPage;
27+
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
30+
import org.springframework.boot.test.context.SpringBootTest;
31+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
32+
import org.springframework.boot.test.context.TestConfiguration;
33+
import org.springframework.context.annotation.Bean;
34+
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
35+
import org.springframework.test.context.junit.jupiter.SpringExtension;
36+
import org.springframework.test.web.servlet.MockMvc;
37+
import org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDriverBuilder;
38+
39+
@ExtendWith(SpringExtension.class)
40+
@AutoConfigureMockMvc
41+
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
42+
class BootTests {
43+
44+
private static final String DOCKER_IMAGE = "redis:5.0.5";
45+
46+
@Autowired
47+
private MockMvc mockMvc;
48+
49+
private WebDriver driver;
50+
51+
@BeforeEach
52+
void setup() {
53+
this.driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(this.mockMvc).build();
54+
}
55+
56+
@AfterEach
57+
void tearDown() {
58+
this.driver.quit();
59+
}
60+
61+
@Test
62+
void home() {
63+
LoginPage login = HomePage.go(this.driver);
64+
login.assertAt();
65+
}
66+
67+
@Test
68+
void login() {
69+
LoginPage login = HomePage.go(this.driver);
70+
HomePage home = login.form().login(HomePage.class);
71+
home.assertAt();
72+
home.containCookie("SESSION");
73+
home.doesNotContainCookie("JSESSIONID");
74+
}
75+
76+
@Test
77+
void logout() {
78+
LoginPage login = HomePage.go(this.driver);
79+
HomePage home = login.form().login(HomePage.class);
80+
home.logout();
81+
login.assertAt();
82+
}
83+
84+
@TestConfiguration
85+
static class Config {
86+
87+
@Bean
88+
public GenericContainer redisContainer() {
89+
GenericContainer redisContainer = new GenericContainer(DOCKER_IMAGE)
90+
.withExposedPorts(6379);
91+
redisContainer.start();
92+
return redisContainer;
93+
}
94+
95+
@Bean
96+
public LettuceConnectionFactory redisConnectionFactory() {
97+
return new LettuceConnectionFactory(redisContainer().getContainerIpAddress(),
98+
redisContainer().getFirstMappedPort());
99+
}
100+
101+
}
102+
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2014-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.pages;
18+
19+
import org.openqa.selenium.WebDriver;
20+
21+
public class BasePage {
22+
23+
private WebDriver driver;
24+
25+
public BasePage(WebDriver driver) {
26+
this.driver = driver;
27+
}
28+
29+
public WebDriver getDriver() {
30+
return this.driver;
31+
}
32+
33+
public static void get(WebDriver driver, String get) {
34+
String baseUrl = "http://localhost";
35+
driver.get(baseUrl + get);
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2014-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.pages;
18+
19+
import java.util.Set;
20+
21+
import org.openqa.selenium.By;
22+
import org.openqa.selenium.Cookie;
23+
import org.openqa.selenium.WebDriver;
24+
import org.openqa.selenium.WebElement;
25+
import org.openqa.selenium.support.PageFactory;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
public class HomePage extends BasePage {
30+
31+
public HomePage(WebDriver driver) {
32+
super(driver);
33+
}
34+
35+
public static LoginPage go(WebDriver driver) {
36+
get(driver, "/");
37+
return PageFactory.initElements(driver, LoginPage.class);
38+
}
39+
40+
public void assertAt() {
41+
assertThat(getDriver().getTitle())
42+
.isEqualTo("Spring Session Sample - Secured Content");
43+
}
44+
45+
public void containCookie(String cookieName) {
46+
Set<Cookie> cookies = getDriver().manage().getCookies();
47+
assertThat(cookies).extracting("name").contains(cookieName);
48+
}
49+
50+
public void doesNotContainCookie(String cookieName) {
51+
Set<Cookie> cookies = getDriver().manage().getCookies();
52+
assertThat(cookies).extracting("name").doesNotContain(cookieName);
53+
}
54+
55+
public HomePage logout() {
56+
WebElement logout = getDriver()
57+
.findElement(By.cssSelector("input[type=\"submit\"]"));
58+
logout.click();
59+
return PageFactory.initElements(getDriver(), HomePage.class);
60+
}
61+
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2014-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.pages;
18+
19+
import org.openqa.selenium.SearchContext;
20+
import org.openqa.selenium.WebDriver;
21+
import org.openqa.selenium.WebElement;
22+
import org.openqa.selenium.support.FindBy;
23+
import org.openqa.selenium.support.PageFactory;
24+
import org.openqa.selenium.support.pagefactory.DefaultElementLocatorFactory;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
public class LoginPage extends BasePage {
29+
30+
public LoginPage(WebDriver driver) {
31+
super(driver);
32+
}
33+
34+
public void assertAt() {
35+
assertThat(getDriver().getTitle()).isEqualTo("Please sign in");
36+
}
37+
38+
public Form form() {
39+
return new Form(getDriver());
40+
}
41+
42+
public class Form {
43+
44+
@FindBy(name = "username")
45+
private WebElement username;
46+
47+
@FindBy(name = "password")
48+
private WebElement password;
49+
50+
@FindBy(tagName = "button")
51+
private WebElement button;
52+
53+
public Form(SearchContext context) {
54+
PageFactory.initElements(new DefaultElementLocatorFactory(context), this);
55+
}
56+
57+
public <T> T login(Class<T> page) {
58+
this.username.sendKeys("user");
59+
this.password.sendKeys("password");
60+
this.button.click();
61+
return PageFactory.initElements(getDriver(), page);
62+
}
63+
}
64+
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ryuk.container.timeout=120
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2014-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample;
18+
19+
import org.springframework.boot.SpringApplication;
20+
import org.springframework.boot.autoconfigure.SpringBootApplication;
21+
22+
@SpringBootApplication
23+
public class Application {
24+
25+
public static void main(String[] args) {
26+
SpringApplication.run(Application.class, args);
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2014-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.config;
18+
19+
import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
20+
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
22+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
23+
24+
@Configuration
25+
public class SecurityConfig extends WebSecurityConfigurerAdapter {
26+
27+
// @formatter:off
28+
@Override
29+
protected void configure(HttpSecurity http) throws Exception {
30+
http
31+
.authorizeRequests()
32+
.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
33+
.anyRequest().authenticated()
34+
.and()
35+
.formLogin()
36+
.permitAll();
37+
}
38+
// @formatter:on
39+
40+
}

0 commit comments

Comments
 (0)