Is there a straightforward way to grant authorities in Spring Security/Spring Session based on whether there is currently an active session? What I would like to do is to limit ADMIN authority to a single active session at a time.
For example:
- Both User A and User B can potentially be granted ADMIN authority.
- User A logs in.
- Create session and grant authority ADMIN
- User B logs in while user A's session is still active.
- Create session and grant authority USER
If User B logs in when no other session is active, grant authority ADMIN. If User A logs in again when there is already an active session associated with user A, associate authority USER with the new session (leaving authority ADMIN associated with the 'old' session for user A).
Assuming SessionRegistry and HttpSessionEventPublisher are already configured, we can check the existence of an active admin session as follows:
You can then use it in your authentication logic to grant authority. Note that this will only work on a single server.