Java 在SpringSecurity中,如何发现会话是否因多次登录而无效?

Java 在SpringSecurity中,如何发现会话是否因多次登录而无效?,java,spring,concurrency,spring-security,Java,Spring,Concurrency,Spring Security,当用户使用相同的用户ID从另一台计算机登录,而没有从第一台计算机注销时,默认行为是第一个会话无效。现在,当用户返回到第一台计算机时,我想告诉他,他的会话已无效,因为他已从其他计算机登录。我该怎么做 我想到了两种方法: 允许多次登录,当用户从第二台计算机登录时,将布尔标志添加到所有会话(其ID从sessionRegistry)中,当用户从第一台计算机返回登录时,检查当前会话是否将布尔标志设为true。如果是,则使会话无效,并向用户发送消息。这将在CustomSuccessHandler中完成

当用户使用相同的用户ID从另一台计算机登录,而没有从第一台计算机注销时,默认行为是第一个会话无效。现在,当用户返回到第一台计算机时,我想告诉他,他的会话已无效,因为他已从其他计算机登录。我该怎么做

我想到了两种方法:

  • 允许多次登录,当用户从第二台计算机登录时,将布尔标志添加到所有会话(其ID从
    sessionRegistry
    )中,当用户从第一台计算机返回登录时,检查当前会话是否将布尔标志设为true。如果是,则使会话无效,并向用户发送消息。这将在
    CustomSuccessHandler
    中完成
Flipside:可能无法通过会话id(会话注册表提供的全部内容)获取会话对象

  • 不允许多次登录,当用户返回到他的第一台计算机时,以某种方式找出会话无效的原因。如果其因多次登录而无效,则显示正确的消息

Flipside:在使会话无效时,似乎不可能添加无效原因,而且我不知道如何从第一台计算机访问此信息(如果存在)

不允许多次登录。您可以使用spring安全性轻松实现这一点

<concurrent-session-control max-sessions="1" exception-if-maximum-exceeded="false" expired-url="/jsp/invalidate.do"/> 
//you can also set expired-url to your custom invalidate page rather than create a mapping
并发会话控制添加到spring security

<concurrent-session-control max-sessions="1" exception-if-maximum-exceeded="false" expired-url="/jsp/invalidate.do"/> 
//you can also set expired-url to your custom invalidate page rather than create a mapping
然后是您的登录页面

<c:if test="${not empty invalidate}">
<script type="text/javascript">
    alert("You have been Logged Out. Someone signed in using your account.");
</script>
</script>
然后在message.properties中创建自定义消息

ConcurrentSessionControlStrategy.exceededAllowed=You have been Logged Out. Someone signed in using your account.
希望能有帮助

链接:

ConcurrentSessionControlStrategy.exceededAllowed=You have been Logged Out. Someone signed in using your account.