Skip to content

Commit 2e12753

Browse files
Merge branch '3.1.x'
2 parents dbd9567 + 50fa05a commit 2e12753

File tree

8 files changed

+22
-208
lines changed

8 files changed

+22
-208
lines changed

spring-session-docs/modules/ROOT/nav.adoc

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
* xref:whats-new.adoc[What's New]
2-
* xref:getting-started.adoc[Getting Started]
3-
** xref:getting-started/using-redis.adoc[Using Redis]
4-
** xref:getting-started/using-jdbc.adoc[Using JDBC]
5-
** xref:getting-started/using-mongodb.adoc[Using MongoDB]
6-
** xref:getting-started/using-hazelcast.adoc[Using Hazelcast]
7-
** xref:getting-started/using-custom-session-repository.adoc[Using Your Own Session Repository]
82
* xref:samples.adoc[Samples & Guides (Start Here)]
93
** Boot Samples
104
*** HttpSession
@@ -21,6 +15,8 @@
2115
*** xref:guides/boot-webflux-custom-cookie.adoc[Custom Cookie]
2216
** Java Configuration
2317
** XML Configuration
18+
* xref:configurations.adoc[Configurations]
19+
** xref:configuration/redis.adoc[Redis]
2420
* xref:http-session.adoc[HttpSession Integration]
2521
* xref:web-socket.adoc[WebSocket Integration]
2622
* xref:web-session.adoc[WebSession Integration]

spring-session-docs/modules/ROOT/pages/getting-started/using-redis.adoc renamed to spring-session-docs/modules/ROOT/pages/configuration/redis.adoc

Lines changed: 16 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -1,168 +1,20 @@
1-
[[using-redis]]
2-
= Using Spring Session with Redis
3-
4-
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:
16-
17-
====
18-
.pom.xml
19-
[source,xml,role="primary"]
20-
[subs="verbatim,attributes"]
21-
----
22-
<dependencies>
23-
<dependency>
24-
<groupId>org.springframework.session</groupId>
25-
<artifactId>spring-session-data-redis</artifactId>
26-
</dependency>
27-
</dependencies>
28-
----
29-
30-
.build.gradle
31-
[source,groovy,role="secondary"]
32-
----
33-
implementation("org.springframework.session:spring-session-data-redis")
34-
----
35-
====
36-
37-
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:
38-
39-
====
40-
.pom.xml
41-
[source,xml,role="primary"]
42-
[subs="verbatim,attributes"]
43-
----
44-
<dependencies>
45-
<dependency>
46-
<groupId>org.springframework.boot</groupId>
47-
<artifactId>spring-boot-starter-data-redis</artifactId>
48-
</dependency>
49-
</dependencies>
50-
----
51-
52-
.build.gradle
53-
[source,groovy,role="secondary"]
54-
----
55-
implementation("org.springframework.boot:spring-boot-starter-data-redis")
56-
----
57-
====
58-
59-
Since we are using Spring Boot, it already {spring-boot-ref-docs}/web.html#web.spring-session[provides auto-configuration for the Redis support].
60-
61-
[NOTE]
62-
====
63-
You can take control over Spring Session’s configuration using `@Enable*HttpSession` (servlet) or `@Enable*WebSession` (reactive).
64-
This will cause the auto-configuration to back off.
65-
Spring Session can then be configured using the annotation’s attributes rather than the configuration properties.
66-
====
67-
68-
The basic setup is done, your application should be using Spring Session backed by Redis.
69-
If you need, you can refer to {gh-samples-url}spring-session-sample-boot-redis[a sample Spring Boot application with Spring Session backed by Redis].
70-
71-
[[java-configuration]]
72-
== Spring Java Configuration
73-
74-
=== Adding the Dependencies
75-
76-
First, we need to add the `spring-session-data-redis` and the `lettuce-core` dependency:
77-
78-
====
79-
.pom.xml
80-
[source,xml,role="primary"]
81-
[subs="verbatim,attributes"]
82-
----
83-
<dependencies>
84-
<dependency>
85-
<groupId>org.springframework.session</groupId>
86-
<artifactId>spring-session-data-redis</artifactId>
87-
<version>{spring-session-version}</version>
88-
</dependency>
89-
<dependency>
90-
<groupId>io.lettuce</groupId>
91-
<artifactId>lettuce-core</artifactId>
92-
<version>{lettuce-core-version}</version>
93-
</dependency>
94-
</dependencies>
95-
----
96-
97-
.build.gradle
98-
[source,groovy,role="secondary"]
99-
----
100-
implementation("org.springframework.session:spring-session-data-redis:{spring-session-version}")
101-
implementation("io.lettuce:lettuce-core:{lettuce-core-version}")
102-
----
103-
====
104-
105-
[[creating-spring-configuration]]
106-
=== Creating the Spring Configuration
107-
108-
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.
110-
To do so, add the following Spring Configuration:
111-
112-
====
113-
[source,java]
114-
----
115-
include::{samples-dir}spring-session-sample-javaconfig-redis/src/main/java/sample/Config.java[tags=class]
116-
----
117-
====
118-
119-
<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.
134-
The following shows an example:
135-
136-
====
137-
.src/main/java/sample/Initializer.java
138-
[source,java]
139-
----
140-
include::{samples-dir}spring-session-sample-javaconfig-redis/src/main/java/sample/Initializer.java[tags=class]
141-
----
142-
====
143-
144-
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
1513

1524
Now that you have your application configured, you might want to start customizing things:
1535

1546
- 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>>.
1567
- I want <<choosing-between-regular-and-indexed,help in choosing>> `RedisSessionRepository` or `RedisIndexedSessionRepository`.
1578
- I want to <<serializing-session-using-json,serialize the session using JSON>>.
1589
- I want to <<using-a-different-namespace,specify a different namespace>>.
15910
- I want to <<listening-session-events,know when a session is created, deleted, destroyed or expires>>.
16011
- I want to <<finding-all-user-sessions, find all sessions of a specific user>>
16112

16213
[[serializing-session-using-json]]
163-
=== Serializing the Session using JSON
14+
== Serializing the Session using JSON
16415

16516
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.
16618
You can provide a `RedisSerializer` bean to customize how the session is serialized into Redis.
16719
Spring Data Redis provides the `GenericJackson2JsonRedisSerializer` that serializes and deserializes objects using Jackson's `ObjectMapper`.
16820

@@ -188,12 +40,12 @@ public RedisSerializer<Object> springSessionDefaultRedisSerializer(ObjectMapper
18840
====
18941

19042
[[using-a-different-namespace]]
191-
=== Specifying a Different Namespace
43+
== Specifying a Different Namespace
19244

19345
It is not uncommon to have multiple applications that use the same Redis instance.
19446
For that reason, Spring Session uses a `namespace` (defaults to `spring:session`) to keep the session data separated if needed.
19547

196-
==== Using Spring Boot Properties
48+
=== Using Spring Boot Properties
19749

19850
You can specify it by setting the `spring.session.redis.namespace` property.
19951

@@ -214,7 +66,7 @@ spring:
21466
----
21567
====
21668

217-
==== Using the Annotation's Attributes
69+
=== Using the Annotation's Attributes
21870

21971
You can specify the `namespace` by setting the `redisNamespace` property in the `@EnableRedisHttpSession`, `@EnableRedisIndexedHttpSession`, or `@EnableRedisWebSession` annotations:
22072

@@ -251,7 +103,7 @@ public class SessionConfig {
251103
====
252104

253105
[[choosing-between-regular-and-indexed]]
254-
=== Choosing Between `RedisSessionRepository` and `RedisIndexedSessionRepository`
106+
== Choosing Between `RedisSessionRepository` and `RedisIndexedSessionRepository`
255107

256108
When working with Spring Session Redis, you will likely have to choose between the `RedisSessionRepository` and the `RedisIndexedSessionRepository`.
257109
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
270122
These indexes allow for efficient querying of sessions based on specific criteria, enhancing performance and enabling advanced session management features.
271123
In addition to that, `RedisIndexedSessionRepository` also supports session expiration and deletion.
272124

273-
==== Configuring the `RedisSessionRepository`
125+
=== Configuring the `RedisSessionRepository`
274126

275-
===== Using Spring Boot Properties
127+
==== Using Spring Boot Properties
276128

277129
If you are using Spring Boot, the `RedisSessionRepository` is the default implementation.
278130
However, if you want to be explicit about it, you can set the following property in your application:
@@ -294,7 +146,7 @@ spring:
294146
----
295147
====
296148

297-
===== Using Annotations
149+
==== Using Annotations
298150

299151
You can configure the `RedisSessionRepository` by using the `@EnableRedisHttpSession` annotation:
300152

@@ -310,9 +162,9 @@ public class SessionConfig {
310162
====
311163

312164
[[configuring-redisindexedsessionrepository]]
313-
==== Configuring the `RedisIndexedSessionRepository`
165+
=== Configuring the `RedisIndexedSessionRepository`
314166

315-
===== Using Spring Boot Properties
167+
==== Using Spring Boot Properties
316168

317169
You can configure the `RedisIndexedSessionRepository` by setting the following properties in your application:
318170

@@ -333,7 +185,7 @@ spring:
333185
----
334186
====
335187

336-
===== Using Annotations
188+
==== Using Annotations
337189

338190
You can configure the `RedisIndexedSessionRepository` by using the `@EnableRedisIndexedHttpSession` annotation:
339191

@@ -349,7 +201,7 @@ public class SessionConfig {
349201
====
350202

351203
[[listening-session-events]]
352-
=== Listening to Session Events
204+
== Listening to Session Events
353205

354206
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.
355207
In order to be able to do that, you must be using the <<configuring-redisindexedsessionrepository,indexed repository>>.
@@ -389,7 +241,7 @@ public class SessionEventListener {
389241
====
390242

391243
[[finding-all-user-sessions]]
392-
=== Finding All Sessions of a Specific User
244+
== Finding All Sessions of a Specific User
393245

394246
By retrieving all sessions of a specific user, you can track the user's active sessions across devices or browsers.
395247
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.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[[configuration]]
2+
= Configuration
3+
4+
This section provides guidance on how to further configure Spring Session for each of its supported datastores.

spring-session-docs/modules/ROOT/pages/getting-started.adoc

Lines changed: 0 additions & 22 deletions
This file was deleted.

spring-session-docs/modules/ROOT/pages/getting-started/using-custom-session-repository.adoc

Lines changed: 0 additions & 4 deletions
This file was deleted.

spring-session-docs/modules/ROOT/pages/getting-started/using-hazelcast.adoc

Lines changed: 0 additions & 4 deletions
This file was deleted.

spring-session-docs/modules/ROOT/pages/getting-started/using-jdbc.adoc

Lines changed: 0 additions & 4 deletions
This file was deleted.

spring-session-docs/modules/ROOT/pages/getting-started/using-mongodb.adoc

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)