Spring Session Couchbase provides a solution for Spring Session in a Couchbase data store.
-
Accessing a session from any environment (i.e. web, messaging infrastructure, etc)
-
In a web environment
-
Support for clustering in a vendor neutral way
-
Pluggable strategy for determining the session id
-
Easily keep the HttpSession alive when a WebSocket is active
-
Simply add to your pom.xml the following dependency pom.xml
<dependency>
<groupId>io.github.couchbaselabs</groupId>
<artifactId>spring-session-data-couchbase</artifactId>
<version>1.1</version>
</dependency>
After adding the required dependencies, we can create our Spring configuration.
The Spring configuration is responsible for creating a Servlet Filter that replaces the HttpSession
implementation with an implementation backed by Spring Session.
All you have to do is to add the following Spring Configuration:
@SpringBootApplication
@EnableCouchbaseHttpSession
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
-
The
@EnableCouchbaseHttpSession
annotation creates a Spring Bean with the name ofspringSessionRepositoryFilter
that implements Filter. The filter is what is in charge of replacing theHttpSession
implementation to be backed by Spring Session. In this instance Spring Session is backed by Couchbase.
The @EnableCouchbaseHttpSession
has 4 properties:
-
maxInactiveIntervalInSeconds (default 1800 seconds) - How long the session will live before expiring
-
typeName (default "_type" ) - The name of the attribute that is going to be used as the type of the document
-
typeValue (default "sessions" ) - The value of the type attribute that is going to be used to differentiate this document from others.
-
keepStringAsLiteral (default false) - Any attribute in the session which is a String will be saved as a document attribute instead of being serialized in a binary format. This is really useful if you need to query the user’s session via N1QL (Ex: create a dashboard of what users have in their sessions). Check out this example to see it working
Spring Boot automatically creates a Couchbase connection and connects Spring Session to a Couchbase Server. You can configure this connection by defining a @Configuration
class which extends org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration
or via application.properties :
spring.couchbase.bootstrap-hosts=localhost spring.couchbase.bucket.name=sessionstore spring.couchbase.bucket.password=password
Our Spring Configuration created a Spring Bean named springSessionRepositoryFilter
that implements Filter
.
The springSessionRepositoryFilter
bean is responsible for replacing the HttpSession
with a custom implementation that is backed by Spring Session.
In order for our Filter
to do its magic, Spring needs to load our Config
class.
Last we need to ensure that our Servlet Container (i.e. Tomcat) uses our springSessionRepositoryFilter
for every request.
Fortunately, Spring Boot takes care of both of these steps for us.
The https://github.com/couchbaselabs/session-store-java repo contains a fully functional example, please check the documentation in the sample to find out how to run it.
You can find the documentation, issue management, support, samples, and guides for using Spring Session Couchbase at http://projects.spring.io/spring-session-data-couchbase/
Spring Session is Open Source software released under the Apache 2.0 license.
This is a community project mainly supported by me. If you have any questions feel free to ping me at @deniswsrosa