Spring Boot Session + MongoDB

Farzin Pashaee
Technology Times

--

The term “Spring Boot Session” describes the procedure for preserving stateful data about an application user during numerous request-response exchanges. Sessions are frequently used in online applications to store user data like authentication details, the contents of shopping carts, or any other data that has to persist over repeated requests. You can read the following article to learn more about the spring boot session.

The magic of the Spring Boot made everything so simple and easy. You can integrate your session management with different technologies by simply adding the related dependency. In this article, we will see how we can use MongoDB to store session data.

Getting Start

To start, we need to have the following dependencies in your pom.xml file:

<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-mongodb</artifactId>
</dependency>

In addition to the session core dependency, you need to add the spring-session-data-mongodb. Now that the dependencies are ready, we can prepare the configuration for the MongoDB server in your application.properties file to have everything sorted out.

spring.data.mongodb.database=sessionDb
spring.data.mongodb.port=27017
spring.data.mongodb.host=localhost
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=admin
spring.data.mongodb.password=password

Now, let's create our first session. To do so, we can inject the HttpSession object inside the controller or use the HttpServletRequestand get the session object from there.

@Slf4j
@Controller
public class TestController {

@RequestMapping("mongodb-session")
public String getSession(HttpSession session){
if ( session.getAttribute("counter") == null ){
session.setAttribute("counter" , 1 );
log.info( "New user");
} else {
log.info( "visit count : " + session.getAttribute("counter") );
session.setAttribute("counter" , (int) session.getAttribute("counter") + 1 );
}
return "mongodb-session.html";
}

}

After running the app and creating your first session, you will find out that a new document will be created for each user session on your MongoDB database. like this example:

But what is really under the hood and what is really happening when we are using session data mongo? In fact, the majority of this magic is being done by the SessionRepositoryFilter. If you track down the HTTP request and see where actually the session object is created you will notice multiple things:

The HttpServletRequest is wrapped by the SessionRepositoryFilter, which also overrides the methods for obtaining a HttpSession. SessionRepositoryFilter will check the validity of the token first and also:

  • Will check if any cookie is present and will load the session data from the store
  • Will convert the HttpSession into a MongoSession
  • will update session data in the store

There are a lot of different dependencies available that you can use based on the store that you are using or are more comfortable with. In this article, we discussed how you can use MongoDB to store session data. similar practices can be used for other technologies like Redis, Hazelcast, and…

I hope this article helped you, and please support me by applauding 👏 for the story. If you don’t know how it works, it’s just like this:

Or buy me a coffee here!

--

--