1. 程式人生 > >Spring security防止使用者重複登入

Spring security防止使用者重複登入

使用Spring security防止使用者的重複登入。如果使用者賬號已登入,這時再進行第二次或多次登入,需要阻止這樣的多次登入。

首先在web.xml中配置listener:

<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>

在security.xml中配置:
<session-management>
        <concurrency-control max-sessions="1" error-if-maximum-exceeded="true"/>
</session-management>

max-sessions表示最多允許多少次重複登入。如果沒有配置error-if-maximum-exceeded,那麼使用者賬號的第二次登入會使第一次登入失效,而配置了的話,那麼第二次登入會被阻止。通常的做法是阻止第二次登入。