标签:var6 sessions int Spring SessionInformation ConcurrentSessionControlAuthenticati
记录 pmsys 中使用 ConcurrentSessionControlAuthenticationStrategy
作为 session
的并发控制策略,实现用户单一登录,挤掉前一个登录的用户。当出现了一些小问题,所以以此记录。
问题
使用了 ConcurrentSessionControlAuthenticationStrategy
是实现了单一登录, 但没完全实现。因为后一个登录的用户也被挤掉, 即这个账号所有用户都被下线。
原因
ConcurrentSessionControlAuthenticationStrategy
源码中 onAuthentication
方法.
官方 方法说明: In addition to the steps from the superclass, the sessionRegistry will be updated with the new session information.
机翻: 除了超类中的步骤之外,还将使用新的会话信息更新sessionRegistry。
public void onAuthentication(Authentication authentication, HttpServletRequest request, HttpServletResponse response) {
List<SessionInformation> sessions = this.sessionRegistry.getAllSessions(authentication.getPrincipal(), false);
// 全部的 session 数量
int sessionCount = sessions.size();
// 这里得到可允许的 session 并发数量
int allowedSessions = this.getMaximumSessionsForThisUser(authentication);
if (sessionCount >= allowedSessions) {
if (allowedSessions != -1) {
if (sessionCount == allowedSessions) {
HttpSession session = request.getSession(false);
if (session != null) {
Iterator var8 = sessions.iterator();
while(var8.hasNext()) {
SessionInformation si = (SessionInformation)var8.next();
if (si.getSessionId().equals(session.getId())) {
return;
}
}
}
}
// 超出允许的并发数量,就会调用此方法, 原因
this.allowableSessionsExceeded(sessions, allowedSessions, this.sessionRegistry);
}
}
}
allowableSessionsExceeded
方法源码:
protected void allowableSessionsExceeded(List<SessionInformation> sessions, int allowableSessions, SessionRegistry registry) throws SessionAuthenticationException {
if (!this.exceptionIfMaximumExceeded && sessions != null) {
sessions.sort(Comparator.comparing(SessionInformation::getLastRequest));
// 以下两行代码就是主要原因
int maximumSessionsExceededBy = sessions.size() - allowableSessions + 1;
List<SessionInformation> sessionsToBeExpired = sessions.subList(0, maximumSessionsExceededBy);
Iterator var6 = sessionsToBeExpired.iterator();
while(var6.hasNext()) {
SessionInformation session = (SessionInformation)var6.next();
// 使当前 session 失效
session.expireNow();
}
} else {
throw new SessionAuthenticationException(this.messages.getMessage("ConcurrentSessionControlAuthenticationStrategy.exceededAllowed", new Object[]{allowableSessions}, "Maximum sessions of {0} for this principal exceeded"));
}
}
为了实现用户单一登录, 那么最大并发数量肯定是 1 (默认也是 1 ), maximumSessionsExceededBy
= 2 - 1 + 1 = 2 . 那么 sessionsToBeExpired
就是把两个 session
都包含其中,然后都令其失效. 从而导致两个用户都下线.
这里提一下 subList 方法
List<E> subList(int fromIndex, int toIndex);
这个方法是左开右闭的, 即
tiIndex
这个位置是没有选到的, 例如: sublist(0,2) , 得到只有集合中下标为 0 , 1 的元素.还有一些情况,就自行百度.
个人解决方案
继承 ConcurrentSessionControlAuthenticationStrategy
重写 allowableSessionsExceeded
方法. 用自己的 session
并发控制策略
@Component
public class MyConcurrentSessionControlAuthenticationStrategy extends ConcurrentSessionControlAuthenticationStrategy {
protected MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor();
public MyConcurrentSessionControlAuthenticationStrategy(SessionRegistry sessionRegistry) {
super(sessionRegistry);
}
@Override
protected void allowableSessionsExceeded(List<SessionInformation> sessions, int allowableSessions, SessionRegistry registry) throws SessionAuthenticationException {
if (sessions != null) {
sessions.sort(Comparator.comparing(SessionInformation::getLastRequest));
int maximumSessionsExceededBy = sessions.size() - allowableSessions;
List<SessionInformation> sessionsToBeExpired = sessions.subList(0, maximumSessionsExceededBy);
Iterator var6 = sessionsToBeExpired.iterator();
while(var6.hasNext()) {
SessionInformation session = (SessionInformation)var6.next();
session.expireNow();
}
} else {
throw new SessionAuthenticationException(this.messages.getMessage("ConcurrentSessionControlAuthenticationStrategy.exceededAllowed", new Object[]{allowableSessions}, "Maximum sessions of {0} for this principal exceeded"));
}
}
}
实验结果可行.
GitHub 也有人提出这个问题,但问题待解决. ConcurrentSessionControlAuthenticationStrategy.allowableSessionsExceeded set the latest session to invalid, is this a bug? #9343
标签:var6,sessions,int,Spring,SessionInformation,ConcurrentSessionControlAuthenticati 来源: https://www.cnblogs.com/dongjiao/p/14673895.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。