You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
5
-
In order to configure your application, you must choose what type of application you have:
6
-
7
-
- <<spring-boot-configuration,I have a Spring Boot application>>
8
-
- <<java-configuration,I have a non Spring Boot application>>
9
-
10
-
[[spring-boot-configuration]]
11
-
== Spring Boot Configuration
12
-
13
-
=== Adding the Dependencies
14
-
15
-
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.
109
-
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`.
120
-
The filter is in charge of replacing the `HttpSession` implementation to be backed by Spring Session.
121
-
In this instance, Spring Session is backed by Redis.
122
-
<2> We create a `RedisConnectionFactory` that connects Spring Session to the Redis Server using the `LettuceConnectionFactory`.
123
-
By default, it connects to `localhost` on the default port (6379).
124
-
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].
125
-
126
-
=== Initializing the Configuration into the Java Servlet Container
127
-
128
-
Our <<java-configuration,Spring Java Configuration>> created a Spring Bean named `springSessionRepositoryFilter` that implements `Filter`.
129
-
The `springSessionRepositoryFilter` bean is responsible for replacing the `HttpSession` with a custom implementation that is backed by Spring Session.
130
-
131
-
In order for our `Filter` to work, Spring needs to load our `Config` class.
132
-
Last, we need to ensure that our Servlet Container uses our `springSessionRepositoryFilter` for every request.
133
-
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`.
145
-
146
-
<1> The first step is to extend `AbstractHttpSessionApplicationInitializer`.
147
-
Doing so ensures that the Spring Bean by the name of `springSessionRepositoryFilter` is registered with our Servlet Container for every request.
148
-
<2> `AbstractHttpSessionApplicationInitializer` also provides a mechanism to ensure Spring loads our `Config`.
149
-
150
-
== Further Customizations
1
+
[[redis-configurations]]
2
+
= Redis Configurations
151
3
152
4
Now that you have your application configured, you might want to start customizing things:
153
5
154
6
- 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
155
-
- I want to customize the Redis configuration by <<creating-spring-configuration,exposing my own beans>>.
156
7
- I want <<choosing-between-regular-and-indexed,help in choosing>> `RedisSessionRepository` or `RedisIndexedSessionRepository`.
157
8
- I want to <<serializing-session-using-json,serialize the session using JSON>>.
158
9
- I want to <<using-a-different-namespace,specify a different namespace>>.
159
10
- I want to <<listening-session-events,know when a session is created, deleted, destroyed or expires>>.
160
11
- I want to <<finding-all-user-sessions, find all sessions of a specific user>>
161
12
162
13
[[serializing-session-using-json]]
163
-
=== Serializing the Session using JSON
14
+
== Serializing the Session using JSON
164
15
165
16
By default, Spring Session uses Java Serialization to serialize the session attributes.
17
+
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.
166
18
You can provide a `RedisSerializer` bean to customize how the session is serialized into Redis.
167
19
Spring Data Redis provides the `GenericJackson2JsonRedisSerializer` that serializes and deserializes objects using Jackson's `ObjectMapper`.
168
20
@@ -188,12 +40,12 @@ public RedisSerializer<Object> springSessionDefaultRedisSerializer(ObjectMapper
188
40
====
189
41
190
42
[[using-a-different-namespace]]
191
-
=== Specifying a Different Namespace
43
+
== Specifying a Different Namespace
192
44
193
45
It is not uncommon to have multiple applications that use the same Redis instance.
194
46
For that reason, Spring Session uses a `namespace` (defaults to `spring:session`) to keep the session data separated if needed.
195
47
196
-
==== Using Spring Boot Properties
48
+
=== Using Spring Boot Properties
197
49
198
50
You can specify it by setting the `spring.session.redis.namespace` property.
199
51
@@ -214,7 +66,7 @@ spring:
214
66
----
215
67
====
216
68
217
-
==== Using the Annotation's Attributes
69
+
=== Using the Annotation's Attributes
218
70
219
71
You can specify the `namespace` by setting the `redisNamespace` property in the `@EnableRedisHttpSession`, `@EnableRedisIndexedHttpSession`, or `@EnableRedisWebSession` annotations:
220
72
@@ -251,7 +103,7 @@ public class SessionConfig {
251
103
====
252
104
253
105
[[choosing-between-regular-and-indexed]]
254
-
=== Choosing Between `RedisSessionRepository` and `RedisIndexedSessionRepository`
106
+
== Choosing Between `RedisSessionRepository` and `RedisIndexedSessionRepository`
255
107
256
108
When working with Spring Session Redis, you will likely have to choose between the `RedisSessionRepository` and the `RedisIndexedSessionRepository`.
257
109
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
270
122
These indexes allow for efficient querying of sessions based on specific criteria, enhancing performance and enabling advanced session management features.
271
123
In addition to that, `RedisIndexedSessionRepository` also supports session expiration and deletion.
272
124
273
-
==== Configuring the `RedisSessionRepository`
125
+
=== Configuring the `RedisSessionRepository`
274
126
275
-
===== Using Spring Boot Properties
127
+
==== Using Spring Boot Properties
276
128
277
129
If you are using Spring Boot, the `RedisSessionRepository` is the default implementation.
278
130
However, if you want to be explicit about it, you can set the following property in your application:
@@ -294,7 +146,7 @@ spring:
294
146
----
295
147
====
296
148
297
-
===== Using Annotations
149
+
==== Using Annotations
298
150
299
151
You can configure the `RedisSessionRepository` by using the `@EnableRedisHttpSession` annotation:
300
152
@@ -310,9 +162,9 @@ public class SessionConfig {
310
162
====
311
163
312
164
[[configuring-redisindexedsessionrepository]]
313
-
==== Configuring the `RedisIndexedSessionRepository`
165
+
=== Configuring the `RedisIndexedSessionRepository`
314
166
315
-
===== Using Spring Boot Properties
167
+
==== Using Spring Boot Properties
316
168
317
169
You can configure the `RedisIndexedSessionRepository` by setting the following properties in your application:
318
170
@@ -333,7 +185,7 @@ spring:
333
185
----
334
186
====
335
187
336
-
===== Using Annotations
188
+
==== Using Annotations
337
189
338
190
You can configure the `RedisIndexedSessionRepository` by using the `@EnableRedisIndexedHttpSession` annotation:
339
191
@@ -349,7 +201,7 @@ public class SessionConfig {
349
201
====
350
202
351
203
[[listening-session-events]]
352
-
=== Listening to Session Events
204
+
== Listening to Session Events
353
205
354
206
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.
355
207
In order to be able to do that, you must be using the <<configuring-redisindexedsessionrepository,indexed repository>>.
@@ -389,7 +241,7 @@ public class SessionEventListener {
389
241
====
390
242
391
243
[[finding-all-user-sessions]]
392
-
=== Finding All Sessions of a Specific User
244
+
== Finding All Sessions of a Specific User
393
245
394
246
By retrieving all sessions of a specific user, you can track the user's active sessions across devices or browsers.
395
247
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.
0 commit comments