|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2016 the original author or authors. |
| 2 | + * Copyright 2002-2019 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
15 | 15 | */
|
16 | 16 | package org.springframework.security.web.authentication.session;
|
17 | 17 |
|
| 18 | +import java.util.Comparator; |
18 | 19 | import java.util.List;
|
19 | 20 |
|
20 | 21 | import javax.servlet.http.HttpServletRequest;
|
|
45 | 46 | * </p>
|
46 | 47 | * <p>
|
47 | 48 | * If a user has reached the maximum number of permitted sessions, the behaviour depends
|
48 |
| - * on the <tt>exceptionIfMaxExceeded</tt> property. The default behaviour is to expired |
49 |
| - * the least recently used session, which will be invalidated by the |
| 49 | + * on the <tt>exceptionIfMaxExceeded</tt> property. The default behaviour is to expire |
| 50 | + * any sessions that exceed the maximum number of permitted sessions, starting with the |
| 51 | + * least recently used sessions. The expired sessions will be invalidated by the |
50 | 52 | * {@link ConcurrentSessionFilter} if accessed again. If <tt>exceptionIfMaxExceeded</tt>
|
51 | 53 | * is set to <tt>true</tt>, however, the user will be prevented from starting a new
|
52 | 54 | * authenticated session.
|
@@ -156,18 +158,13 @@ protected void allowableSessionsExceeded(List<SessionInformation> sessions,
|
156 | 158 | "Maximum sessions of {0} for this principal exceeded"));
|
157 | 159 | }
|
158 | 160 |
|
159 |
| - // Determine least recently used session, and mark it for invalidation |
160 |
| - SessionInformation leastRecentlyUsed = null; |
161 |
| - |
162 |
| - for (SessionInformation session : sessions) { |
163 |
| - if ((leastRecentlyUsed == null) |
164 |
| - || session.getLastRequest() |
165 |
| - .before(leastRecentlyUsed.getLastRequest())) { |
166 |
| - leastRecentlyUsed = session; |
167 |
| - } |
| 161 | + // Determine least recently used sessions, and mark them for invalidation |
| 162 | + sessions.sort(Comparator.comparing(SessionInformation::getLastRequest)); |
| 163 | + int maximumSessionsExceededBy = sessions.size() - allowableSessions + 1; |
| 164 | + List<SessionInformation> sessionsToBeExpired = sessions.subList(0, maximumSessionsExceededBy); |
| 165 | + for (SessionInformation session: sessionsToBeExpired) { |
| 166 | + session.expireNow(); |
168 | 167 | }
|
169 |
| - |
170 |
| - leastRecentlyUsed.expireNow(); |
171 | 168 | }
|
172 | 169 |
|
173 | 170 | /**
|
|
0 commit comments