mrnejc / mongo-session-store

0 stars 0 forks source link

MongoDB Session Store

About

Small (java only) implementation of SessionStore that uses MongoDB backend for storing web client sessions.

Code is based on LocalSessionStore implementation in vertx-web.

Usage

I'm using JitPack.io for package distribution. Add repository and dependency to your pom.xml file:

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
         <groupId>com.themonkee</groupId>
         <artifactId>mongo-session-store</artifactId>
         <version><!-- pick the version from tags --></version>
    </dependency>
</dependencies>

Now to your code. To use the session store first create MongoClient

vertx = Vertx.vertx();
mongoClient = MongoClient.createNonShared(vertx,
                                          new JsonObject().put("host", "localhost")
                                                          .put("port", 27017)
                                                          .put("db_name", "test));

and then pass the connection to create session store, if you want to change the name of session collection (default is "sessions") set it via configuration JsonObject and setup SessionHandler to use this store in handler

MongoSessionStore.create(vertx, mongoClient, new JsonObject.put("collection", "my_sessions" ).setHandler(r->{
  if(r.succeeded())
    sessionHandler = SessionHandler.create(r.result());
  else
    <... handle error ...>
});

Dependencies

This module is build against vertx-web and vertx-mongo-client. Make sure you add them to your pom.xml:

<dependency>
  <groupId>io.vertx</groupId>
  <artifactId>vertx-web</artifactId>
  <version>3.4.2</version>
</dependency>

<dependency>
  <groupId>io.vertx</groupId>
  <artifactId>vertx-mongo-client</artifactId>
  <version>3.4.3</version>
</dependency>

License

Since vertx-web is provided under dual Eclipse Public License v1.0 and Apache License v2.0 this code is released under same conditions.