title: Spring Session Data Redis - Get Valid Sessions, Current User from Redis Store
body: <p>My question is, in distributed web application is it possible to get the valid sessions from Redis Store using <code>RedisOperationSessionRepository</code>. (I mean I don't want to write explicit code for putting it into Redis store and then later read it, I want to understand if framework or <code>spring-data-redis</code> library provides that).</p>
  
  <p>I am aware that Spring Redis is able to restore sessions and server restarts also preserve the login if the session is still valid (as it is backed by Redis) </p>
  
  <p>One of the functionality I am looking for is to get all the possible log in users currently in the application. I am aware of <code>SessionRegistryImpl</code>, and this method. but what I noticed that this method is not backed by Redis and after server restarts, log in users are not returned.</p>
  
  <pre><code> public List&lt;Object&gt; getAllPrincipals() {
      return new ArrayList&lt;Object&gt;(principals.keySet());
  }
  </code></pre>
  
  <p>One of the functionality I can try is from Spring Session 1.1.0, Spring session find by username.</p>
  
  <ol>
  <li><a href="http://docs.spring.io/spring-session/docs/1.1.0.M1/reference/html5/guides/findbyusername.html">http://docs.spring.io/spring-session/docs/1.1.0.M1/reference/html5/guides/findbyusername.html</a></li>
  <li><a href="https://spring.io/blog/2015/11/17/spring-session-1-1-0-m1-released">https://spring.io/blog/2015/11/17/spring-session-1-1-0-m1-released</a></li>
  </ol>
  
  <p>I tried and it indeed returns me valid session result, but the problem is I still need to know all the current valid user names that are using this application. (I don't know how to get them using Redis Store, again I can store in Redis and get them, but I want to know if there is better approach).</p>
  
  <p>This is the piece of code, this is the way I can get current user from one of the many users that are currently using the system, if I know the session id.</p>
  
  <pre><code>    final Session session = redisOperationsSessionRepository.getSession(sessionid);
  
      final Object obj = session.getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
      if (obj instanceof SecurityContext) {
          final SecurityContext context = (SecurityContext) obj;
          final Authentication authentication = context.getAuthentication();
          if (authentication != null) {
              final Object principal = authentication.getPrincipal();
              if (principal != null &amp;&amp; principal instanceof CurrentUser) {
                  return (CurrentUser) principal;
              }
          }
      }
  </code></pre>
  
  <p>Now I can use above logic to get all the current user, but again I should all the valid session ids, which I don't know how to get from Redis store.</p>
  
  <p>New Update : 
  <a href="https://github.com/spring-projects/spring-session/issues/255">https://github.com/spring-projects/spring-session/issues/255</a>
  Here in this link, I can probably get all the session ids and look for active sessions in <code>RedisOperationSessionRepository</code>, but might result in performance issues.</p>
  
  <p>I am not sure if I made myself clear, but can't we tell something to Redis using spring session api, just give me all the valid sessions and their current user that are currently log in. (based on last accessed time or something like that).</p>
  
  <p>Thank you</p>