Spring Session uses https://docs.spring.io/spring-data/data-redis/docs/{spring-data-redis-version}/reference/html/[Spring Data Redis] to support managing the session information in Redis.
In order to configure your application, you must choose what type of application you have:
- <<spring-boot-configuration,I have a Spring Boot application>>
- <<java-configuration,I have a non Spring Boot application>>
[[spring-boot-configuration]]
== Spring Boot Configuration
=== Adding the Dependencies
First, you need to add the `spring-session-data-redis` dependency:
As <<using-redis,mentioned above>>, we also need to add the Spring Data Redis dependency to our application, for that we can use the `spring-boot-starter-data-redis` dependency:
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.
<1> The `@EnableRedisHttpSession` annotation creates a Spring Bean with the name of `springSessionRepositoryFilter` that implements `Filter`.
The filter is in charge of replacing the `HttpSession` implementation to be backed by Spring Session.
In this instance, Spring Session is backed by Redis.
<2> We create a `RedisConnectionFactory` that connects Spring Session to the Redis Server using the `LettuceConnectionFactory`.
By default, it connects to `localhost` on the default port (6379).
For more information on configuring Spring Data Redis, see the https://docs.spring.io/spring-data/data-redis/docs/{spring-data-redis-version}/reference/html/#redis:connectors[reference documentation].
=== Initializing the Configuration into the Java Servlet Container
Our <<java-configuration,Spring Java 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 work, Spring needs to load our `Config` class.
Last, we need to ensure that our Servlet Container uses our `springSessionRepositoryFilter` for every request.
Fortunately, Spring Session provides a utility class named `AbstractHttpSessionApplicationInitializer` to help with both of these steps.
NOTE: The name of our class (`Initializer`) does not matter. What is important is that we extend `AbstractHttpSessionApplicationInitializer`.
<1> The first step is to extend `AbstractHttpSessionApplicationInitializer`.
Doing so ensures that the Spring Bean by the name of `springSessionRepositoryFilter` is registered with our Servlet Container for every request.
<2> `AbstractHttpSessionApplicationInitializer` also provides a mechanism to ensure Spring loads our `Config`.
== Further Customizations
[[redis-configurations]]
= Redis Configurations
Now that you have your application configured, you might want to start customizing things:
- I want to {spring-boot-ref-docs}/application-properties.html#application-properties.data.spring.data.redis.host[customize the Redis configuration] using Spring Boot properties
- I want to customize the Redis configuration by <<creating-spring-configuration,exposing my own beans>>.
- I want <<choosing-between-regular-and-indexed,help in choosing>> `RedisSessionRepository` or `RedisIndexedSessionRepository`.
- I want to <<serializing-session-using-json,serialize the session using JSON>>.
- I want to <<using-a-different-namespace,specify a different namespace>>.
@@ -160,9 +11,10 @@ Now that you have your application configured, you might want to start customizi
- I want to <<finding-all-user-sessions, find all sessions of a specific user>>
[[serializing-session-using-json]]
=== Serializing the Session using JSON
== Serializing the Session using JSON
By default, Spring Session uses Java Serialization to serialize the session attributes.
Sometimes it might be problematic, especially when you have multiple applications that use the same Redis instance but have different versions of the same class.
You can provide a `RedisSerializer` bean to customize how the session is serialized into Redis.
Spring Data Redis provides the `GenericJackson2JsonRedisSerializer` that serializes and deserializes objects using Jackson's `ObjectMapper`.
@@ -188,12 +40,12 @@ public RedisSerializer<Object> springSessionDefaultRedisSerializer(ObjectMapper
====
[[using-a-different-namespace]]
=== Specifying a Different Namespace
== Specifying a Different Namespace
It is not uncommon to have multiple applications that use the same Redis instance.
For that reason, Spring Session uses a `namespace` (defaults to `spring:session`) to keep the session data separated if needed.
==== Using Spring Boot Properties
=== Using Spring Boot Properties
You can specify it by setting the `spring.session.redis.namespace` property.
@@ -214,7 +66,7 @@ spring:
----
====
==== Using the Annotation's Attributes
=== Using the Annotation's Attributes
You can specify the `namespace` by setting the `redisNamespace` property in the `@EnableRedisHttpSession`, `@EnableRedisIndexedHttpSession`, or `@EnableRedisWebSession` annotations:
@@ -251,7 +103,7 @@ public class SessionConfig {
====
[[choosing-between-regular-and-indexed]]
=== Choosing Between `RedisSessionRepository` and `RedisIndexedSessionRepository`
== Choosing Between `RedisSessionRepository` and `RedisIndexedSessionRepository`
When working with Spring Session Redis, you will likely have to choose between the `RedisSessionRepository` and the `RedisIndexedSessionRepository`.
Both are implementations of the `SessionRepository` interface that store session data in Redis.
@@ -270,9 +122,9 @@ For example, it may create indexes based on session attributes like user ID or l
These indexes allow for efficient querying of sessions based on specific criteria, enhancing performance and enabling advanced session management features.
In addition to that, `RedisIndexedSessionRepository` also supports session expiration and deletion.
==== Configuring the `RedisSessionRepository`
=== Configuring the `RedisSessionRepository`
===== Using Spring Boot Properties
==== Using Spring Boot Properties
If you are using Spring Boot, the `RedisSessionRepository` is the default implementation.
However, if you want to be explicit about it, you can set the following property in your application:
@@ -294,7 +146,7 @@ spring:
----
====
===== Using Annotations
==== Using Annotations
You can configure the `RedisSessionRepository` by using the `@EnableRedisHttpSession` annotation:
@@ -310,9 +162,9 @@ public class SessionConfig {
====
[[configuring-redisindexedsessionrepository]]
==== Configuring the `RedisIndexedSessionRepository`
=== Configuring the `RedisIndexedSessionRepository`
===== Using Spring Boot Properties
==== Using Spring Boot Properties
You can configure the `RedisIndexedSessionRepository` by setting the following properties in your application:
@@ -333,7 +185,7 @@ spring:
----
====
===== Using Annotations
==== Using Annotations
You can configure the `RedisIndexedSessionRepository` by using the `@EnableRedisIndexedHttpSession` annotation:
@@ -349,7 +201,7 @@ public class SessionConfig {
====
[[listening-session-events]]
=== Listening to Session Events
== Listening to Session Events
Often times it is valuable to react to session events, for example, you might want to do some kind of processing depending on the session lifecycle.
In order to be able to do that, you must be using the <<configuring-redisindexedsessionrepository,indexed repository>>.
@@ -389,7 +241,7 @@ public class SessionEventListener {
====
[[finding-all-user-sessions]]
=== Finding All Sessions of a Specific User
== Finding All Sessions of a Specific User
By retrieving all sessions of a specific user, you can track the user's active sessions across devices or browsers.
For example, you can use this information session management purposes, such as allowing the user to invalidate or logout from specific sessions or performing actions based on the user's session activity.
This section is still a work in progress, please refer to this xref:guides/boot-mongo.adoc[other section].
Reference in New Issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.