Spring Session MongoDB provides an API and implementations for managing a user’s session information on MongoDB.

1. Introduction

For an introduction to Spring Session as a whole, visit Spring Session itself.

2. Samples and Guides (Start Here)

If you are looking to get started with Spring Session, the best place to start is our Sample Applications.

Table 1. Sample Applications using Spring Boot
Description Guide

Demonstrates how to use Spring Session to replace the HttpSession with Mongo.

HttpSession with Mongo Guide

2.1. HttpSession with Mongo

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.

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.

All you have to do is to add the following Spring Configuration:

@EnableMongoHttpSession (1)
public class HttpSessionConfig {

	@Bean
	public JdkMongoSessionConverter jdkMongoSessionConverter() {
		return new JdkMongoSessionConverter(Duration.ofMinutes(30)); (2)
	}
}
1 The @EnableMongoHttpSession annotation creates a Spring Bean with the name of springSessionRepositoryFilter that implements Filter. The filter is what is in charge of replacing the HttpSession implementation to be backed by Spring Session. In this instance Spring Session is backed by Mongo.

2.1.1. Session serialization mechanisms

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();
}
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.

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:

@Configuration
@EnableMongoHttpSession
static class Config extends BaseConfig {

	@Bean
	public AbstractMongoSessionConverter mongoSessionConverter() {
		return new JacksonMongoSessionConverter(Collections.singletonList(new GeoModule()));
	}
}
JdkMongoSessionConverter

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.

3. API Documentation

You can browse the complete Javadoc online. The key APIs are described below:

4. Spring Session Community

We are glad to consider you a part of our community. Please find additional information below.

4.1. Support

You can get help by asking questions on StackOverflow with the tag spring-session. Similarly we encourage helping others by answering questions on StackOverflow.

4.2. Source Code

Our source code can be found on github at https://github.com/spring-projects/spring-session-data-mongodb/

4.3. Issue Tracking

4.4. Contributing

We appreciate Pull Requests.

4.5. License

Spring Session is Open Source software released under the Apache 2.0 license.

5. Minimum Requirements

The minimum requirements for Spring Session MongoDB are:

  • Java 8

  • If you are running in a Servlet Container (not required), Servlet 2.5+

  • If you are using other Spring libraries (not required), the minimum required version is Spring 5.0