记录 Spring Security 之 ConcurrentSessionControlAuthenticationStrategy 的一个小问题
2021/4/18 18:28:37
本文主要是介绍记录 Spring Security 之 ConcurrentSessionControlAuthenticationStrategy 的一个小问题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
记录 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
这篇关于记录 Spring Security 之 ConcurrentSessionControlAuthenticationStrategy 的一个小问题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)
- 2024-05-30【Java】百万数据excel导出功能如何实现
- 2024-05-30我们小公司,哪像华为一样,用得上IPD(集成产品开发)?