現(xiàn)在項(xiàng)目中有這么一個(gè)需求疾就,同一時(shí)間段內(nèi)只允許一個(gè)用戶進(jìn)行登錄,后登陸的用戶會(huì)將前一個(gè)用戶“頂”掉孽鸡。
修改項(xiàng)目web.xml文件
在 web.xml 中添加監(jiān)聽(tīng)
<web-app>
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
</web-app>
修改secutiry.xml文件
在 security.xml 文件中增加以下內(nèi)容
<b:beans xmlns="http://www.springframework.org/schema/security"
xmlns:b="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
<http use-expressions="true" auto-config="true">
<!-- session管理 -->
<session-management>
<concurrency-control error-if-maximum-exceeded="false" />
</session-management>
</http>
</b:beans>
運(yùn)行結(jié)果
運(yùn)行結(jié)果示例雁比,可見(jiàn)當(dāng)在第二個(gè)瀏覽器中再次登錄,刷新前一個(gè)瀏覽器會(huì)出現(xiàn)session已經(jīng)失效的提示乡范。
示例圖片