title: How can I get all active sessions with Apache Shiro ehcache-terracotta?
body: <p>In my maven application, I want to provide that if a user log in a new account, another account that already open will be log out. I am using Apache shiro, ehcache and terracotta for this.
  I want to get all active sessions to check whether that user is logined previously. I can take users and their session id from all servers using Hazelcast(this works), but i can not take session (because it is not serializable)</p>
  
  <p>I use clustered servers in project. Servers have same ip address but their ports are different.
  x.x.x.x:7002 -->server1,
  x.x.x.x:7003 -->server2
  how can i define that ports in ehcache configuration file?
  how can i take all active sessions from that ports with terracotta?</p>
  
  <p>Please help</p>
  
  <p>ehcache.xml:</p>
  
  <pre><code>&lt;ehcache&gt;
  &lt;terracottaConfig url="x.x.x.x:9510"/&gt;
  
  &lt;diskStore path="java.io.tmpdir/shiro-ehcache"/&gt;
  &lt;defaultCache
      maxElementsInMemory="10000"
      eternal="false"
      timeToIdleSeconds="120"
      timeToLiveSeconds="120"
      overflowToDisk="false"
      diskPersistent="false"
      diskExpiryThreadIntervalSeconds="120"&gt;
      &lt;!-- &lt;terracotta/&gt;--&gt;
  &lt;/defaultCache&gt;
  &lt;cache name="myactiveSessionCache"
         maxElementsInMemory="10000"
         eternal="true"
         timeToLiveSeconds="0"
         timeToIdleSeconds="0"
         diskPersistent="false"
         overflowToDisk="false"
         diskExpiryThreadIntervalSeconds="600"&gt;
      &lt;!-- &lt;terracotta/&gt;--&gt;
  &lt;/cache&gt;
  &lt;!-- Add more cache entries as desired, for example,
  Realm authc/authz caching: --&gt;
  &lt;/ehcache&gt;
  </code></pre>
  
  <p>shiro.ini:</p>
  
  <pre><code>sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager
  sessionDAO = org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO
  sessionManager.sessionDAO = $sessionDAO
  cacheManager = org.apache.shiro.cache.ehcache.EhCacheManager
  cacheManager .cacheManagerConfigFile = classpath:ehcache.xml
  securityManager.cacheManager = $cacheManager 
  securityManager.sessionManager = $sessionManager
  </code></pre>
  
  <p>Login.java:</p>
  
  <pre><code>User newuser = ..//get from db with username and password
  UsernamePasswordToken token = new UsernamePasswordToken(username, password);
  Subject currentUser = new Subject.Builder().buildSubject();
  token.setRememberMe(true);
  currentUser = getSubject();
  currentUser.login(token);
  
  //at here, i take all active users and their's session id with Hazelcast. 
  //and check that is 'newuser' in all active users list?
  if (!activeuserList.containsKey(newuser)) {
      activeuserList.put(newuser, currentUser.getSession().getId());
  } else {
      Serializable s = activeuserList.get(newuser);
      isSessionActive(s);
  }
  }
  
  ...
  //check -- is session active?
  public void isSessionActive(Serializable s) {
  DefaultWebSecurityManager securityManager = (DefaultWebSecurityManager)   SecurityUtils.getSecurityManager();
  DefaultWebSessionManager sessionManager = (DefaultWebSessionManager) securityManager.getSessionManager();
  EnterpriseCacheSessionDAO sessionDao = (EnterpriseCacheSessionDAO) sessionManager.getSessionDAO();
  Cache&lt;Serializable, Session&gt; activeSessionsCache = sessionDao.getActiveSessionsCache();
  if (activeSessionsCache.keys().contains(s)) {
          //previous session is closed.
          activeuserList.remove(newuser);
          activeSessionsCache.get(s).stop();
          //new session id is added to list
          activeuserList.put(newuser, currentUser.getSession().getId());
      }
  }
  </code></pre>