crusy
crusy

Reputation: 1512

Spring Boot: Iterate and evaluate all (active) sessions

Example: I want to limit logins to, say, five desktop and five mobile sessions at a time. Therefore I added a flag to each session like

request.getSession().setAttribute("Session-Type", "mobile");

Now while any user logs in I want to count all active mobile/desktop sessions and allow/disallow the login based on that count. How do I accomplish this?

I know I can use the SessionRegistry to access Principals and/or their SessionInformation, but not the actual session...

EDIT: I'm using Hazelcast to store the sessions, if this helps

Upvotes: 1

Views: 895

Answers (1)

crusy
crusy

Reputation: 1512

With Hazelcast only:

@Autowired
private HazelcastInstance hazelcastInstance;

// ...

// from org.springframework.session.hazelcast.HazelcastSessionRepository
final Collection<Session> sessions = (Collection) hazelcastInstance.getMap(HazelcastSessionRepository.DEFAULT_SESSION_MAP_NAME).values();
for(final Session session : sessions) {
  System.out.println(session.getId() + ": " + session.getAttribute("Session-Type"));
}

Upvotes: 2

Related Questions