Hazelcast Session Replication in Shared Session Environment

23 views
Skip to first unread message

Steve

unread,
Aug 26, 2021, 10:41:09 AM8/26/21
to Hazelcast
Hi
Wildfly allows the sharing of HttpSession across  wars contained in a single ear.
See Here 
The issue is HazelCast Session replication does not work in this situation..
Can anyone think of another way to share the HttpSession in this situation. I'm thinking something like maybe disabling the Wildfly configuration and somehow looking up the session in the Hazelcast IMAP? Any response would be greatly appreciated.
Thanks
-Steve

Steve

unread,
Aug 30, 2021, 12:21:40 PM8/30/21
to Hazelcast
I found a solution
Heres a bit of code with my solution

First I created a ServletContextListener

public void contextInitialized(ServletContextEvent event) {
System.out.println("########### SETTING Cookie Path to / for context " + event.getServletContext().getContextPath());
event.getServletContext().getSessionCookieConfig().setPath("/");
event.getServletContext().getSessionCookieConfig().setHttpOnly(true);
}
This allowed all my wars to share a session
Then I used the code below to pull out the piece of the Session I was interested in sharing

HttpServletRequest request = attributes.getRequest();
Cookie cookie = findCookie(request.getCookies(), "hazelcast.sessionId");
if (cookie != null) {
   String sessionId = cookie.getValue();
       if (hazelcastInstance != null) {
         IMap sessionMap = hazelcastInstance.getMap("session-map");
        if (sessionMap.containsKey(sessionId)) {
          SessionState state = (SessionState) sessionMap.get(sessionId);
          Map<String, Data> dataMap = state.getAttributes();
          if (dataMap.containsKey(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY)) {
               SerializationServiceSupport ss = (SerializationServiceSupport) hazelcastInstance;
               sc = ss.getSerializationService().toObject(
               dataMap.get(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY));

Nicolas Frankel

unread,
Oct 9, 2021, 11:49:06 PM10/9/21
to Hazelcast
You don't need to do it by yourself, we provide a module to do that. You can use it:
Reply all
Reply to author
Forward
0 new messages