Introduce SessionIdGenerationStrategy

Closes gh-11
This commit is contained in:
Marcus Da Coregio
2023-03-07 17:55:18 -03:00
committed by Marcus Hert Da Coregio
parent 2e1275333a
commit d547b33962
39 changed files with 1074 additions and 30 deletions

View File

@@ -17,6 +17,7 @@
** XML Configuration
* xref:configurations.adoc[Configurations]
** xref:configuration/redis.adoc[Redis]
** xref:configuration/common.adoc[Common Configurations]
* xref:http-session.adoc[HttpSession Integration]
* xref:web-socket.adoc[WebSocket Integration]
* xref:web-session.adoc[WebSession Integration]

View File

@@ -0,0 +1,57 @@
[[common-configurations]]
= Common Configurations
This section contains common configurations that applies to all or most Spring Session modules.
It contains configuration examples for the following use cases:
- I need to <<changing-how-session-ids-are-generated,change the way that Session IDs are generated>>
[[changing-how-session-ids-are-generated]]
== Changing How Session IDs Are Generated
By default, Spring Session uses `UuidSessionIdGenerationStrategy` which, in turn, uses a `java.util.UUID` to generate a session id.
There might be scenarios where it may be better to include other characters to increase entropy, or you may want to use a different algorithm to generate the session id.
To change this, you can provide a custom `SessionIdGenerationStrategy` bean:
.Changing How Session IDs Are Generated
[tabs]
======
Java::
+
[source,java,role="primary"]
----
@Bean
public SessionIdGenerationStrategy sessionIdGenerationStrategy() {
return new MySessionIdGenerationStrategy();
}
class MySessionIdGenerationStrategy implements SessionIdGenerationStrategy {
@Override
public String generate() {
// ...
}
}
----
======
After exposing your `SessionIdGenerationStrategy` bean, Spring Session will use it to generate session ids.
If you are manually configuring your `SessionRepository` bean (instead of using `@EnableRedisHttpSession`, for example), you can set the `SessionIdGenerationStrategy` directly on the `SessionRepository` implementation:
.Setting `SessionIdGenerationStrategy` directly into `SessionRepository` implementation
[tabs]
======
Java::
+
[source,java,role="primary"]
----
@Bean
public RedisSessionRepository redisSessionRepository(RedisOperations redisOperations) {
RedisSessionRepository repository = new RedisSessionRepository(redisOperations)
repository.setSessionIdGenerationStrategy(new MySessionIdGenerationStrategy());
return repository;
}
----
======

View File

@@ -1 +1,3 @@
= What's New
- xref:configuration/common.adoc#changing-how-session-ids-are-generated[docs] - https://github.com/spring-projects/spring-session/issues/11[gh-11] - Introduce `SessionIdGenerationStrategy` to allow custom session id generation