Spring-Session Grails Plugin (Part 2)

jeetmp3

Jitendra Singh Bisht

Posted on July 24, 2019

Spring-Session Grails Plugin (Part 2)

Fork me on GitHub

In my previous blog I’ve explained spring-session grails plugin along with Redis datastore. We’ve also covered JSON serialization that will be almost same when you use mongo datastore. In this blog post I’ll explain how you can use MongoDB as your session store.

To change datastore you need to add a property in your Config.groovy.

springsession.sessionStore=SessionStore.MONGO

This will set MongoDB as your datastore. By default it’ll try to connect mongo running on localhost port 27017. Let’s check out the some config properties with their default values.

Note: Some of the common properties explained in previous block. Those will work same for mongo datastore.

springsession.mongo.hostName="localhost" // MongoDB host server. Default is localhost.
springsession.mongo.port=27017 // MongoDB port. Default is 27017.
springsession.mongo.database="spring-session" // MongoDB database to store sessions. Default is spring-session.
springsession.mongo.username="" // MongoDB username. Default is "".
springsession.mongo.password="" // MongoDB password. Default is "".
springsession.mongo.collectionName="sessions" // Mongo collection name to store session data. Default is "sessions".
springsession.mongo.replicaSet=[[:] ] // MongoDB replica set if any. It includes list of maps [[hostName: 'localhost', port: 27017] ] Default is [[:]].
springsession.mongo.jackson.modules=[] // Jackson module class if any. Default is empty list

Note: MongoDB is a NoSQL and schemaless database. So you don’t need to create database and collections for the session. It’ll auto created when session will created.

By default it uses Java serialization. To use JSON Serialization please visit to first blog of this series. First 2 steps will be same but in 3rd step Register my module class with spring-session plugin you will have to use mongo specific jackson.modules property.

springsession.mongo.jackson.modules = ['demo.SimpleModule']

That’s it in this post guys. In my next blog, I’ll explain how to use JDBC datastore with spring-session grails plugin.

💖 💪 🙅 🚩
jeetmp3
Jitendra Singh Bisht

Posted on July 24, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related