Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.keycloak.adapters.tomcat7;
  
  
  import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
Manages relationship to users and sessions so that forced admin logout can be implemented

Author(s):
Davide Ungari
Version:
$Revision: 1 $
 
     private static final Logger log = Logger.getLogger(""+CatalinaUserSessionManagement.class);
 
     public static class UserSessions {
         protected String user;
         protected long loggedIn = System.currentTimeMillis();
         protected Map<StringString>  keycloakSessionToHttpSession = new HashMap<StringString>();
         protected Map<StringString>  httpSessionToKeycloakSession = new HashMap<StringString>();
         protected Map<StringSessionsessions = new HashMap<StringSession>();
         public long getLoggedIn() {
             return ;
         }
     }
 
     public synchronized int getActiveSessions() {
         return .size();
     }

    

Parameters:
username
Returns:
null if user not logged in
 
     @Override
     public synchronized Long getUserLoginTime(String username) {
         UserSessions sessions = .get(username);
         if (sessions == nullreturn null;
         return sessions.getLoggedIn();
     }
 
     @Override
     public synchronized Set<StringgetActiveUsers() {
         HashSet<Stringset = new HashSet<String>();
         set.addAll(.keySet());
         return set;
     }
 
 
     public synchronized void login(Session sessionString usernameString keycloakSessionId) {
         String sessionId = session.getId();
 
         UserSessions sessions = .get(username);
         if (sessions == null) {
             sessions = new UserSessions();
             sessions.user = username;
             .put(usernamesessions);
         }
         .put(keycloakSessionIdsessions);
         sessions.httpSessionToKeycloakSession.put(sessionIdkeycloakSessionId);
         sessions.keycloakSessionToHttpSession.put(keycloakSessionIdsessionId);
         sessions.sessions.put(sessionIdsession);
         session.addSessionListener(this);
     }
 
     @Override
     public void logoutAll() {
         for (String user : .keySet()) logoutUser(user);
     }
 
     @Override
     public void logoutUser(String user) {
         UserSessions sessions = null;
         sessions = .remove(user);
         if (sessions == null) {
             return;
         }
         for (Map.Entry<StringStringentry : sessions.httpSessionToKeycloakSession.entrySet()) {
             String sessionId = entry.getKey();
             String keycloakSessionId = entry.getValue();
             Session session = sessions.sessions.get(sessionId);
             session.setPrincipal(null);
             session.setAuthType(null);
             session.getSession().invalidate();
             .remove(keycloakSessionId);
         }
     }
    public synchronized void logoutKeycloakSession(String keycloakSessionId) {
        UserSessions sessions = .remove(keycloakSessionId);
        if (sessions == null) {
            return;
        }
        String sessionId = sessions.keycloakSessionToHttpSession.remove(keycloakSessionId);
        if (sessionId == null) {
        }
        sessions.httpSessionToKeycloakSession.remove(sessionId);
        Session session = sessions.sessions.remove(sessionId);
        session.setPrincipal(null);
        session.setAuthType(null);
        session.getSession().invalidate();
        if (sessions.keycloakSessionToHttpSession.size() == 0) {
            .remove(sessions.user);
        }
    }
    public void sessionEvent(SessionEvent event) {
        // We only care about session destroyed events
        if (!..equals(event.getType())
                && (!..equals(event.getType())))
            return;
        // Look up the single session id associated with this session (if any)
        Session session = event.getSession();
        GenericPrincipal principal = (GenericPrincipalsession.getPrincipal();
        if (principal == nullreturn;
        session.setPrincipal(null);
        session.setAuthType(null);
        String username = principal.getUserPrincipal().getName();
        UserSessions userSessions = .get(username);
        if (userSessions == null) {
            return;
        }
        String sessionid = session.getId();
        synchronized (this) {
            String keycloakSessionId = userSessions.httpSessionToKeycloakSession.remove(sessionid);
            if (keycloakSessionId != null) {
                userSessions.keycloakSessionToHttpSession.remove(keycloakSessionId);
                .remove(keycloakSessionId);
            }
            userSessions.sessions.remove(sessionid);
            if (userSessions.httpSessionToKeycloakSession.size() == 0) {
                .remove(username);
            }
        }
    }
New to GrepCode? Check out our FAQ X