Merge pull request #10351 from vpavic:improve-session-sample
* pr/10351: Polish "Improve Spring Session sample" Improve Spring Session sample
This commit is contained in:
commit
326290b2c9
|
|
@ -158,8 +158,8 @@ The following sample applications are provided:
|
|||
| link:spring-boot-sample-servlet[spring-boot-sample-servlet]
|
||||
| Web application with a "raw" `Servlet` returning plain text content
|
||||
|
||||
| link:spring-boot-sample-session-redis[spring-boot-sample-session-redis]
|
||||
| Web Application that uses Spring Session to store session data in Redis
|
||||
| link:spring-boot-sample-session[spring-boot-sample-session]
|
||||
| Web Application that uses Spring Session to manage session data
|
||||
|
||||
| link:spring-boot-sample-simple[spring-boot-sample-simple]
|
||||
| Simple command line application
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@
|
|||
<module>spring-boot-sample-secure-oauth2-actuator</module>
|
||||
<module>spring-boot-sample-secure-oauth2-resource</module>
|
||||
<module>spring-boot-sample-servlet</module>
|
||||
<module>spring-boot-sample-session-redis</module>
|
||||
<module>spring-boot-sample-session</module>
|
||||
<module>spring-boot-sample-simple</module>
|
||||
<module>spring-boot-sample-test</module>
|
||||
<module>spring-boot-sample-test-nomockito</module>
|
||||
|
|
|
|||
|
|
@ -1,50 +0,0 @@
|
|||
<?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-redis</artifactId>
|
||||
<name>Spring Boot Session Redis Sample</name>
|
||||
<description>Spring Boot Session Redis 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>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.session</groupId>
|
||||
<artifactId>spring-session-data-redis</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>
|
||||
</project>
|
||||
|
|
@ -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`.
|
||||
|
|
@ -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>
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
* 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.
|
||||
|
|
@ -14,7 +14,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample.session.redis;
|
||||
package sample.session;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
* 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.
|
||||
|
|
@ -14,16 +14,16 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample.session.redis;
|
||||
package sample.session;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SampleSessionRedisApplication {
|
||||
public class SampleSessionApplication {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication.run(SampleSessionRedisApplication.class);
|
||||
SpringApplication.run(SampleSessionApplication.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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>
|
||||
|
|
@ -14,7 +14,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample.session.redis;
|
||||
package sample.session;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
|
|
@ -23,7 +23,6 @@ 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.data.redis.RedisConnectionFailureException;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.RequestEntity;
|
||||
|
|
@ -33,31 +32,20 @@ import org.springframework.web.client.RestTemplate;
|
|||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link SampleSessionRedisApplication}.
|
||||
* Tests for {@link SampleSessionApplication}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class SampleSessionRedisApplicationTests {
|
||||
public class SampleSessionApplicationTests {
|
||||
|
||||
@Test
|
||||
public void sessionExpiry() throws Exception {
|
||||
|
||||
String port = null;
|
||||
|
||||
try {
|
||||
ConfigurableApplicationContext context = new SpringApplicationBuilder()
|
||||
.sources(SampleSessionRedisApplication.class)
|
||||
.properties("server.port:0")
|
||||
.initializers(new ServerPortInfoApplicationContextInitializer())
|
||||
.run();
|
||||
port = context.getEnvironment().getProperty("local.server.port");
|
||||
}
|
||||
catch (RuntimeException ex) {
|
||||
if (!redisServerRunning(ex)) {
|
||||
return;
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
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();
|
||||
|
|
@ -79,11 +67,4 @@ public class SampleSessionRedisApplicationTests {
|
|||
assertThat(uuid2).isNotEqualTo(uuid3);
|
||||
}
|
||||
|
||||
private boolean redisServerRunning(Throwable ex) {
|
||||
if (ex instanceof RedisConnectionFailureException) {
|
||||
return false;
|
||||
}
|
||||
return (ex.getCause() == null || redisServerRunning(ex.getCause()));
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue