Skip to content

hazelcast/hazelcast-spring-session

Repository files navigation

Hazelcast Spring Session

Spring Session provides an API and implementations for managing a user's session information, while also making it trivial to support clustered sessions without being tied to an application container specific solution. It also provides transparent integration with:

  • HttpSession - allows replacing the HttpSession in an application container (i.e. Tomcat) neutral way, with support for providing session IDs in headers to work with RESTful APIs.
  • WebSocket - provides the ability to keep the HttpSession alive when receiving WebSocket messages
  • WebSession - allows replacing the Spring WebFlux's WebSession in an application container neutral way.

Hazelcast Spring Session uses Hazelcast Platform to store user session information in a cluster. The SessionRepository uses Hazelcast's IMap to store the session information, giving users AP characteristics.

Getting started

We recommend you visit the Hazelcast Documentation site and Hazelcast Code Samples for additional code samples.

Basic usage

To start using hazelcast-spring-session, you need to add following dependency:

<dependency>
    <groupId>com.hazelcast</groupId>
    <artifactId>hazelcast-spring-session</artifactId>
    <version>4.0.0-SNAPSHOT</version> <!-- note: SNAPSHOT is used here only before first major release -->
</dependency>

or using Gradle:

// note: SNAPSHOT is used here only before first major release
implementation("com.hazelcast:hazelcast-spring-session:4.0.0-SNAPSHOT")

Then you need to add @EnableHazelcastHttpSession annotation to your @Configuration class.

Note that this module's classes must be present on the classpath of all members of the Hazelcast cluster. In a client-server architecture you can either put your project's main artifact in the lib directory of the Hazelcast distribution or use a feature like User Code Namespaces to upload the code.

Configuring a Hazelcast instance

To use the project, your Hazelcast instances must be configured correctly. An example configuration is below:

import com.hazelcast.spring.session.HazelcastIndexedSessionRepository;
import com.hazelcast.spring.session.PrincipalNameExtractor;
import com.hazelcast.spring.session.HazelcastSessionSerializer;
import com.hazelcast.spring.session.HazelcastSessionSerializer;
import org.springframework.session.MapSession;
import com.hazelcast.config.Config;
import com.hazelcast.config.AttributeConfig;
import com.hazelcast.config.SerializerConfig;
// 
Config config = new Config();
// add attribute configuration, so principal name can be retrieved more efficiently
AttributeConfig attributeConfig = new AttributeConfig()
        .setName(HazelcastIndexedSessionRepository.PRINCIPAL_NAME_ATTRIBUTE)
        .setExtractorClassName(PrincipalNameExtractor.class.getName());

// configuration for the IMap storing the user session data
config.getMapConfig(HazelcastIndexedSessionRepository.DEFAULT_SESSION_MAP_NAME)
    .addAttributeConfig(attributeConfig)
    // we are adding an index on principal name for faster querying
    .addIndexConfig(
            new IndexConfig(IndexType.HASH, HazelcastIndexedSessionRepository.PRINCIPAL_NAME_ATTRIBUTE));
// serialization configuration - session objects must be sent between members
SerializerConfig serializerConfig = new SerializerConfig();
serializerConfig.setImplementation(new HazelcastSessionSerializer()).setTypeClass(MapSession.class);
config.getSerializationConfig().addSerializerConfig(serializerConfig);

Building from source

Hazelcast Spring Session uses a Gradle-based build system.

In the instructions below, ./gradlew is invoked from the root of the source tree and serves as a cross-platform, self-contained bootstrap mechanism for the build.

Check out sources:

git clone [email protected]:hazelcast/hazelcast-spring-session.git

Install jars into your local Maven cache:

./gradlew install

Compile and test; build all jars:

./gradlew build

Documentation

You can find more information about using Spring Session in the Hazelcast documentation.

License

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

Migration from Spring Session Hazelcast 3.x

When migrating from Spring Session's Hazelcast module, which was under Spring Team ownership until version 4.0, you need to make some adjustments:

  1. GroupId is changed to com.hazelcast and artifactId is now hazelcast-spring-session.
  2. All Hazelcast-specific classes were moved from org.springframework.session.hazelcast to com.hazelcast.spring.session.

About

Hazelcast-based Spring Session implementation

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 16