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 theHttpSessionin 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 theHttpSessionalive when receiving WebSocket messagesWebSession- allows replacing the Spring WebFlux'sWebSessionin 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.
We recommend you visit the Hazelcast Documentation site and Hazelcast Code Samples for additional code samples.
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.
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);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.gitInstall jars into your local Maven cache:
./gradlew installCompile and test; build all jars:
./gradlew buildYou can find more information about using Spring Session in the Hazelcast documentation.
Hazelcast Spring Session is Open Source software released under the Apache 2.0 license.
When migrating from Spring Session's Hazelcast module, which was under Spring Team ownership until version 4.0, you need to make some adjustments:
- GroupId is changed to
com.hazelcastand artifactId is nowhazelcast-spring-session. - All Hazelcast-specific classes were moved from
org.springframework.session.hazelcasttocom.hazelcast.spring.session.