Java Hazelcast
Java Hazelcast
with Hazelcast
Tommy Ludwig, Rob Winch
Version 2.3.0.RELEASE
Table of Contents
Updating Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Spring Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Servlet Container Initialization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Hazelcast Spring Security Sample Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Running the Sample Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Exploring the Security Sample Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
How Does It Work? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Interacting with the Data Store. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
This guide describes how to use Spring Session along with Spring Security when
you use Hazelcast as your data store. It assumes that you have already applied
Spring Security to your application.
You cand find the completed guide in the Hazelcast Spring Security sample
NOTE
application.
Index
1
Updating Dependencies
Before you use Spring Session, you must update your dependencies. If you use Maven, you must
add the following dependencies:
pom.xml
<dependencies>
<!-- ... -->
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>3.12.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
</dependencies>
2
Spring Configuration
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. To do so, add the following
Spring Configuration:
@EnableHazelcastHttpSession ①
@Configuration
public class HazelcastHttpSessionConfig {
@Bean
public HazelcastInstance hazelcastInstance() {
Config config = new Config();
MapAttributeConfig attributeConfig = new MapAttributeConfig()
.setName(HazelcastIndexedSessionRepository.PRINCIPAL_NAME_ATTRIBUTE)
.setExtractor(PrincipalNameExtractor.class.getName());
config.getMapConfig(HazelcastIndexedSessionRepository.DEFAULT_SESSION_MAP_NAME) ②
.addMapAttributeConfig(attributeConfig).addMapIndexConfig(
new
MapIndexConfig(HazelcastIndexedSessionRepository.PRINCIPAL_NAME_ATTRIBUTE,
false));
return Hazelcast.newHazelcastInstance(config); ③
}
3
Servlet Container Initialization
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 SessionConfig class. Since our
application is already loading Spring configuration by using our SecurityInitializer class, we can
add our SessionConfig class to it. The following listing shows how to do so:
src/main/java/sample/SecurityInitializer.java
public SecurityInitializer() {
super(SecurityConfig.class, SessionConfig.class);
}
Last, we need to ensure that our Servlet Container (that is, Tomcat) uses our
springSessionRepositoryFilter for every request. It is extremely important that Spring Session’s
springSessionRepositoryFilter is invoked before Spring Security’s springSecurityFilterChain. Doing
so ensures that the HttpSession that Spring Security uses is backed by Spring Session. Fortunately,
Spring Session provides a utility class named AbstractHttpSessionApplicationInitializer that
makes this doing so easy. The following example shows how to do so:
src/main/java/sample/Initializer.java
The name of our class (Initializer) does not matter. What is important is that we
NOTE
extend AbstractHttpSessionApplicationInitializer.
4
Hazelcast Spring Security Sample
Application
This section describes how to work with the Hazelcast Spring Security sample application.
$ ./gradlew :spring-session-sample-javaconfig-hazelcast:tomcatRun
• Username user
• Password password
Now click the Login button. You should now see a message indicating that your are logged in with
the user entered previously. The user’s information is stored in Hazelcast rather than Tomcat’s
HttpSession implementation.
When a new HttpSession is created, Spring Session creates a cookie named SESSION in your browser.
That cookie contains the ID of your session. You can view the cookies (with Chrome or Firefox).
5
Using the Console
For example, to remove the session by using the management center console after connecting to
your Hazelcast node, run the following commands:
default> ns spring:session:sessions
spring:session:sessions> m.clear
Alternatively, you can also delete the explicit key. Enter the following into the console, being sure to
replace 7e8383a4-082c-4ffe-a4bc-c40fd3363c5e with the value of your SESSION cookie:
Now visit the application at http://localhost:8080/ and observe that we are no longer authenticated.
As described in the section of the documentation that cover other clients, there is a REST API
provided by the Hazelcast node(s).
For example, you could delete an individual key as follows (being sure to replace 7e8383a4-082c-
4ffe-a4bc-c40fd3363c5e with the value of your SESSION cookie):
$ curl -v -X DELETE
http://localhost:xxxxx/hazelcast/rest/maps/spring:session:sessions/7e8383a4-082c-
4ffe-a4bc-c40fd3363c5e
The port number of the Hazelcast node is printed to the console on startup. Replace
TIP
xxxxx with the port number.
Now you can see that you are no longer authenticated with this session.