This guide describes how to use Spring Session to transparently leverage Redis to back a web application’s HttpSession
with XML-based configuration.
Note
|
You can find the completed guide in the httpsession-xml sample application. |
Before you use Spring Session, you must update your dependencies. If you use Maven, you must add the following dependencies:
<dependencies>
<!-- ... -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>{spring-session-version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>{lettuce-core-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>{spring-core-version}</version>
</dependency>
</dependencies>
After adding the required dependencies, we can create our Spring configuration.
The Spring configuration is responsible for creating a servlet filter that replaces the HttpSession
implementation with an implementation backed by Spring Session.
To do so, add the following Spring Configuration:
link:{samples-dir}spring-session-sample-xml-redis/src/main/webapp/WEB-INF/spring/session.xml[role=include]
-
We use the combination of
<context:annotation-config/>
andRedisHttpSessionConfiguration
because Spring Session does not yet provide XML Namespace support (see gh-104). This creates a Spring Bean with the name ofspringSessionRepositoryFilter
that implementsFilter
. The filter is in charge of replacing theHttpSession
implementation to be backed by Spring Session. In this instance, Spring Session is backed by Redis. -
We create a
RedisConnectionFactory
that connects Spring Session to the Redis Server. We configure the connection to connect to localhost on the default port (6379) For more information on configuring Spring Data Redis, see the {docs-url}/spring-data/data-redis/docs/{spring-data-redis-version}/reference/html/[reference documentation].
Our Spring Configuration created a Spring Bean named springSessionRepositoryFilter
that implements Filter
.
The springSessionRepositoryFilter
bean is responsible for replacing the HttpSession
with a custom implementation that is backed by Spring Session.
In order for our Filter
to do its magic, we need to instruct Spring to load our session.xml
configuration.
We can do so with the following configuration:
link:{samples-dir}spring-session-sample-xml-redis/src/main/webapp/WEB-INF/web.xml[role=include]
link:{samples-dir}spring-session-sample-xml-redis/src/main/webapp/WEB-INF/web.xml[role=include]
The {docs-url}/spring/docs/{spring-core-version}/spring-framework-reference/core.html#context-create[ContextLoaderListener
] reads the contextConfigLocation and picks up our session.xml configuration.
Last, we need to ensure that our Servlet Container (that is, Tomcat) uses our springSessionRepositoryFilter
for every request.
The following snippet performs this last step for us:
link:{samples-dir}spring-session-sample-xml-redis/src/main/webapp/WEB-INF/web.xml[role=include]
The {docs-url}/spring-framework/docs/{spring-core-version}/javadoc-api/org/springframework/web/filter/DelegatingFilterProxy.html[DelegatingFilterProxy
] looks up a Bean by the name of springSessionRepositoryFilter
and cast it to a Filter
.
For every request that DelegatingFilterProxy
is invoked, the springSessionRepositoryFilter
is invoked.
This section describes how to work with the httpsession-xml
sample application.
You can run the sample by obtaining the {download-url}[source code] and invoking the following command:
Note
|
For the sample to work, you must install Redis 2.8+ on localhost and run it with the default port (6379).
Alternatively, you can update the RedisConnectionFactory to point to a Redis server.
Another option is to use Docker to run Redis on localhost. See Docker Redis repository for detailed instructions.
|
$ ./gradlew :spring-session-sample-xml-redis:tomcatRun
You should now be able to access the application at http://localhost:8080/
Now you can try using the application. Fill out the form with the following information:
-
Attribute Name: username
-
Attribute Value: rob
Now click the Set Attribute button. You should now see the values displayed in the table.
We interact with the standard HttpSession
in the SessionServlet
shown in the following listing:
link:{samples-dir}spring-session-sample-xml-redis/src/main/java/sample/SessionServlet.java[role=include]
Instead of using Tomcat’s HttpSession
, we persist the values in Redis.
Spring Session creates a cookie named SESSION in your browser.
That cookie contains the ID of your session.
You can view the cookies (with Chrome or Firefox).
You can remove the session using redis-cli. For example, on a Linux based system you can type the following:
$ redis-cli keys '*' | xargs redis-cli del
Tip
|
The Redis documentation has instructions for installing redis-cli. |
Alternatively, you can also delete the explicit key. To do so, enter the following into your terminal, being sure to replace 7e8383a4-082c-4ffe-a4bc-c40fd3363c5e
with the value of your SESSION cookie:
$ redis-cli del spring:session:sessions:7e8383a4-082c-4ffe-a4bc-c40fd3363c5e
Now you can visit the application at http://localhost:8080/ and see that the attribute we added is no longer displayed.