|
| 1 | +package com.digitalsanctuary.spring.user.service; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import org.springframework.beans.factory.annotation.Value; |
| 5 | +import org.springframework.security.core.session.SessionInformation; |
| 6 | +import org.springframework.security.core.session.SessionRegistry; |
| 7 | +import org.springframework.stereotype.Service; |
| 8 | +import com.digitalsanctuary.spring.user.persistence.model.User; |
| 9 | +import lombok.RequiredArgsConstructor; |
| 10 | +import lombok.extern.slf4j.Slf4j; |
| 11 | + |
| 12 | +/** |
| 13 | + * Service for invalidating user sessions. This is useful for admin-initiated password resets |
| 14 | + * and other security operations that require forcing users to re-authenticate. |
| 15 | + * |
| 16 | + * <p><strong>Race Condition Note:</strong> This service uses Spring's SessionRegistry to track |
| 17 | + * and invalidate sessions. Due to the nature of the SessionRegistry API, there is an inherent |
| 18 | + * race condition: sessions created after {@link SessionRegistry#getAllPrincipals()} is called |
| 19 | + * but before {@link SessionInformation#expireNow()} completes will not be invalidated. This is |
| 20 | + * a known limitation of the SessionRegistry approach. For most use cases (admin password reset), |
| 21 | + * this is acceptable as the window is very small.</p> |
| 22 | + */ |
| 23 | +@Slf4j |
| 24 | +@RequiredArgsConstructor |
| 25 | +@Service |
| 26 | +public class SessionInvalidationService { |
| 27 | + |
| 28 | + private final SessionRegistry sessionRegistry; |
| 29 | + |
| 30 | + /** Threshold for warning about high principal count that may impact performance. */ |
| 31 | + @Value("${user.session.invalidation.warn-threshold:1000}") |
| 32 | + private int warnThreshold; |
| 33 | + |
| 34 | + /** |
| 35 | + * Invalidates all active sessions for the given user. |
| 36 | + * This forces the user to re-authenticate on their next request. |
| 37 | + * |
| 38 | + * <p><strong>Note:</strong> Sessions created after this method starts iterating |
| 39 | + * but before it completes will not be invalidated. This race condition is inherent |
| 40 | + * to the SessionRegistry API and is acceptable for most security operations.</p> |
| 41 | + * |
| 42 | + * @param user the user whose sessions should be invalidated |
| 43 | + * @return the number of sessions that were invalidated |
| 44 | + */ |
| 45 | + public int invalidateUserSessions(User user) { |
| 46 | + if (user == null) { |
| 47 | + log.warn("SessionInvalidationService.invalidateUserSessions: user is null"); |
| 48 | + return 0; |
| 49 | + } |
| 50 | + |
| 51 | + int invalidatedCount = 0; |
| 52 | + List<Object> principals = sessionRegistry.getAllPrincipals(); |
| 53 | + |
| 54 | + // Performance monitoring: warn if principal count is high |
| 55 | + if (principals.size() > warnThreshold) { |
| 56 | + log.warn("SessionInvalidationService.invalidateUserSessions: high principal count ({}) may impact performance", |
| 57 | + principals.size()); |
| 58 | + } |
| 59 | + |
| 60 | + log.debug("SessionInvalidationService.invalidateUserSessions: scanning {} principals for user {}", |
| 61 | + principals.size(), user.getEmail()); |
| 62 | + |
| 63 | + // NOTE: Sessions created after getAllPrincipals() but before expireNow() |
| 64 | + // will not be invalidated. This is a known limitation of SessionRegistry. |
| 65 | + for (Object principal : principals) { |
| 66 | + User principalUser = extractUser(principal); |
| 67 | + |
| 68 | + if (principalUser != null && principalUser.getId().equals(user.getId())) { |
| 69 | + List<SessionInformation> sessions = sessionRegistry.getAllSessions(principal, false); |
| 70 | + for (SessionInformation session : sessions) { |
| 71 | + session.expireNow(); |
| 72 | + invalidatedCount++; |
| 73 | + // Log truncated session ID to avoid exposing full session identifiers |
| 74 | + String sessionId = session.getSessionId(); |
| 75 | + String safeSessionId = sessionId.length() > 8 ? sessionId.substring(0, 8) + "..." : sessionId; |
| 76 | + log.debug("SessionInvalidationService.invalidateUserSessions: expired session {} for user {}", |
| 77 | + safeSessionId, user.getEmail()); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + log.info("SessionInvalidationService.invalidateUserSessions: invalidated {} sessions for user {} (scanned {} principals)", |
| 83 | + invalidatedCount, user.getEmail(), principals.size()); |
| 84 | + return invalidatedCount; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Extracts the User object from a principal. |
| 89 | + * Handles both User and DSUserDetails principal types. |
| 90 | + * |
| 91 | + * @param principal the security principal |
| 92 | + * @return the User object, or null if not extractable |
| 93 | + */ |
| 94 | + private User extractUser(Object principal) { |
| 95 | + if (principal instanceof User user) { |
| 96 | + return user; |
| 97 | + } else if (principal instanceof DSUserDetails dsUserDetails) { |
| 98 | + return dsUserDetails.getUser(); |
| 99 | + } |
| 100 | + return null; |
| 101 | + } |
| 102 | +} |
0 commit comments