Improve Spring Session sample

This commit improves the Spring Session sample by providing multiple
build profiles that make it possible to easily try out different session
stores. By default, JDBC session store backed by an in-memory embedded H2
database is used.

See gh-10351
This commit is contained in:
Vedran Pavic
2017-09-19 21:28:12 +02:00
committed by Stephane Nicoll
parent 17a349b691
commit 8561929164
10 changed files with 173 additions and 84 deletions

View File

@@ -0,0 +1,48 @@
= Spring Boot Spring Session Sample
This sample demonstrates the Spring Session auto-configuration support. Spring Session
supports multiple session store types, including:
* `Redis`
* `JDBC`
* `Hazelcast`
== Using a different session store
Initially, the project uses JDBC session store backed by an in-memory embedded H2
database. You can try out your favorite session store as explained below.
=== Redis
Add `org.springframework.session:spring-session-data-redis` and
`spring-boot-starter-data-redis` dependencies to the project and make sure it is
configured properly (by default, a Redis instance with the default settings is expected
on your local box).
TIP: Run sample application using Redis session store using
`$mvn spring-boot:run -Predis`.
=== JDBC
Add `org.springframework.session:spring-session-jdbc`,
`org.springframework.boot:spring-boot-starter-jdbc` and `com.h2database:h2` dependencies
to the project. An in-memory embedded H2 database is automatically configured.
TIP: Run sample application using JDBC session store with
`$mvn spring-boot:run -Pjdbc`.
Note that this profile is active by default.
=== Hazelcast
Add `org.springframework.session:spring-session-hazelcast` and `com.hazelcast:hazelcast`
dependencies to the project to enable support for Hazelcast. Since there is a default
`hazelcast.xml` configuration file at the root of the classpath, it is used to
automatically configure the underlying `HazelcastInstance`.
TIP: Run sample application using Hazelcast session store with
`$mvn spring-boot:run -Phazelcast`.

View File

@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<!-- Your own application should inherit from spring-boot-starter-parent -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-samples</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-sample-session</artifactId>
<name>Spring Boot Session Sample</name>
<description>Spring Boot Session Sample</description>
<url>http://projects.spring.io/spring-boot/</url>
<organization>
<name>Pivotal Software, Inc.</name>
<url>http://www.spring.io</url>
</organization>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
</properties>
<dependencies>
<!-- Compile -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>redis</id>
<dependencies>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
</profile>
<profile>
<id>jdbc</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
</profile>
<profile>
<id>hazelcast</id>
<dependencies>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-hazelcast</artifactId>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>
</project>

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2012-2017 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 sample.session.redis;
import java.util.UUID;
import javax.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloRestController {
@GetMapping("/")
String uid(HttpSession session) {
UUID uid = (UUID) session.getAttribute("uid");
if (uid == null) {
uid = UUID.randomUUID();
}
session.setAttribute("uid", uid);
return uid.toString();
}
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2012-2017 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 sample.session.redis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SampleSessionApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleSessionApplication.class);
}
}

View File

@@ -0,0 +1 @@
server.session.timeout=5

View File

@@ -0,0 +1,20 @@
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-3.8.xsd"
xmlns="http://www.hazelcast.com/schema/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<map name="spring:session:sessions">
<attributes>
<attribute extractor="org.springframework.session.hazelcast.PrincipalNameExtractor">principalName</attribute>
</attributes>
<indexes>
<index>principalName</index>
</indexes>
</map>
<network>
<join>
<multicast enabled="false"/>
</join>
</network>
</hazelcast>

View File

@@ -0,0 +1,70 @@
/*
* Copyright 2012-2017 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 sample.session.redis;
import java.net.URI;
import org.junit.Test;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link SampleSessionApplication}.
*
* @author Andy Wilkinson
*/
public class SampleSessionApplicationTests {
@Test
public void sessionExpiry() throws Exception {
ConfigurableApplicationContext context = new SpringApplicationBuilder()
.sources(SampleSessionApplication.class)
.properties("server.port:0")
.initializers(new ServerPortInfoApplicationContextInitializer())
.run();
String port = context.getEnvironment().getProperty("local.server.port");
URI uri = URI.create("http://localhost:" + port + "/");
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.getForEntity(uri, String.class);
String uuid1 = response.getBody();
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Cookie", response.getHeaders().getFirst("Set-Cookie"));
RequestEntity<Void> request = new RequestEntity<>(requestHeaders, HttpMethod.GET,
uri);
String uuid2 = restTemplate.exchange(request, String.class).getBody();
assertThat(uuid1).isEqualTo(uuid2);
Thread.sleep(5000);
String uuid3 = restTemplate.exchange(request, String.class).getBody();
assertThat(uuid2).isNotEqualTo(uuid3);
}
}