Spring Session MongoDB provides an API and implementations for managing a user’s session information on MongoDB.
For an introduction to Spring Session as a whole, visit Spring Session itself.
If you are looking to get started with Spring Session, the best place to start is our Sample Applications.
Description | Guide |
---|---|
Demonstrates how to use Spring Session to replace the |
Using Spring Session with HttpSession
is enabled by adding a Servlet Filter before anything that uses the HttpSession
.
This section describes how to use Mongo to back HttpSession
using Java based configuration.
Note
|
The HttpSession Mongo Sample provides a working sample on how to integrate Spring Session and HttpSession using Java configuration.
You can read the basic steps for integration below, but you are encouraged to follow along with the detailed HttpSession Guide when integrating with your own application.
|
To be able to persist session objects in MongoDB we need to provide the serialization/deserialization mechanism.
By default, Spring Session MongoDB will use JdkMongoSessionConverter
.
However, you may switch to JacksonMongoSessionConverter
by merely adding the following code to your Boot app:
@Bean
JacksonMongoSessionConverter mongoSessionConverter() {
return new JacksonMongoSessionConverter();
}
This mechanism uses Jackson to serialize session objects to/from JSON.
By creating the following bean:
@Bean
JacksonMongoSessionConverter mongoSessionConverter() {
return new JacksonMongoSessionConverter();
}
…you are able to switch from the default (JDK-based serialization) to using Jackson.
Important
|
If you are integrating with Spring Security (by storing your sessions in MongoDB), this configuration will register the proper whitelisted components so Spring Security works properly. |
If you would like to provide custom Jackson modules you can do it by explicitly registering modules as shown below:
link:{code-dir}/src/test/java/org/springframework/session/data/mongo/integration/MongoRepositoryJacksonITest.java[role=include]
JdkMongoSessionConverter
uses standard Java serialization to persist session attributes map to MongoDB in a binary form.
However, standard session elements like id, access time, etc are still written as a plain Mongo objects and can be read and queried without additional effort.
JdkMongoSessionConverter
is used if no explicit AbstractMongoSessionConverter
Bean has been defined.
There is also a constructor taking Serializer
and Deserializer
objects, allowing you to pass custom implementations, which is especially important when you want to use non-default classloader.
You can browse the complete Javadoc online. The key APIs are described below:
We are glad to consider you a part of our community. Please find additional information below.
You can get help by asking questions on StackOverflow with the tag spring-session. Similarly we encourage helping others by answering questions on StackOverflow.
Our source code can be found on github at https://github.com/spring-projects/spring-session-data-mongodb/
We track issues in github issues at https://github.com/spring-projects/spring-session-data-mongodb/issues
We appreciate Pull Requests.
Spring Session is Open Source software released under the Apache 2.0 license.