// 获取当前用户的所有session final List<SessionInformation> sessions = sessionRegistry.getAllSessions( authentication.getPrincipal(), false);
int sessionCount = sessions.size(); // 同时允许几个session存在 int allowedSessions = getMaximumSessionsForThisUser(authentication); // 当前登录的数量小于允许的数量 if (sessionCount < allowedSessions) { // They haven't got too many login sessions running at present return; } // 不进行限制 if (allowedSessions == -1) { // We permit unlimited logins return; } // 已经达到允许数量了 if (sessionCount == allowedSessions) { // 当前session 是否为null HttpSession session = request.getSession(false);
if (session != null) { // 不为null则判断一下是否有与当前session同一个sessionId的 // Only permit it though if this request is associated with one of the // already registered sessions for (SessionInformation si : sessions) { if (si.getSessionId().equals(session.getId())) { return; } } } // If the session is null, a new one will be created by the parent class, // exceeding the allowed number } // 这里说明session已超过限制数量了 allowableSessionsExceeded(sessions, allowedSessions, sessionRegistry); }
protectedvoidallowableSessionsExceeded(List<SessionInformation> sessions, int allowableSessions, SessionRegistry registry) throws SessionAuthenticationException { // exceptionIfMaximumExceeded该值就是配置的maxSessionsPreventsLogin if (exceptionIfMaximumExceeded || (sessions == null)) { thrownew SessionAuthenticationException(messages.getMessage( "ConcurrentSessionControlAuthenticationStrategy.exceededAllowed", new Object[] { Integer.valueOf(allowableSessions) }, "Maximum sessions of {0} for this principal exceeded")); }
// Determine least recently used session, and mark it for invalidation SessionInformation leastRecentlyUsed = null;
for (SessionInformation session : sessions) { if ((leastRecentlyUsed == null) || session.getLastRequest() .before(leastRecentlyUsed.getLastRequest())) { leastRecentlyUsed = session; } }