Spring Security

【转自】http://www.mossle.com/oa/springsecurity/html/index.html

第 12 章 管理会话

多个用户不能使用同一个账号同时登陆系统。

12.1. 添加监听器

在web.xml中添加一个监听器,这个监听器会在session创建和销毁的时候通知Spring Security。

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

这种监听session生命周期的监听器主要用来收集在线用户的信息,比如统计在线用户数之类的事。有关如何自己编写listener统计在线用户数,可以参考:http://www.family168.com/tutorial/jsp/html/jsp-ch-08.html

12.2. 添加过滤器

在xml中添加控制同步session的过滤器。

<http auto-config='true'>
    <intercept-url pattern="/admin.jsp" access="ROLE_ADMIN" />
    <intercept-url pattern="/**" access="ROLE_USER" />
    <concurrent-session-control/>
</http>
        

因为Spring Security的作者不认为控制会话是一个大家都经常使用的功能,所以concurrent-session-control没有包含在默认生成的过滤器链中,在我们需要使用它的时候,需要自己把它添加到http元素中。

这个concurrent-session-control对应的过滤器类是org.springframework.security.concurrent.ConcurrentSessionFilter,它的排序代码是100,它会被放在过滤器链的最顶端,在所有过滤器使用之前起作用。

12.3. 控制策略

12.3.1. 后登陆的将先登录的踢出系统

默认情况下,后登陆的用户会把先登录的用户踢出系统。

想测试一下的话,先打开firefox使用user/user登陆系统,然后再打开ie使用user/user登陆系统。这时ie下的user用户会登陆成功,进入登陆成功页面。而firefox下的用户如何刷新页面,就会显示如下信息:

This session has been expired (possibly due to multiple concurrent logins being attempted as the same user).
            

这是因为先登录的用户已经被强行踢出了系统,如果他再次使用user/user登陆,ie下的用户也会被踢出系统了。

12.3.2. 后面的用户禁止登陆

如果不想让之前登录的用户被自动踢出系统,需要为concurrent-session-control设置一个参数。

<http auto-config='true'>
    <intercept-url pattern="/admin.jsp" access="ROLE_ADMIN" />
    <intercept-url pattern="/**" access="ROLE_USER" />
    <concurrent-session-control exception-if-maximum-exceeded="true"/>
</http>
            

这个参数用来控制是否在会话数目超过最大限制时抛出异常,默认值是false,也就是不抛出异常,而是把之前的session都销毁掉,所以之前登陆的用户就会被踢出系统了。

现在我们把这个参数改为true,再使用同一个账号同时登陆一下系统,看看会发生什么现象。

禁止同一账号多次登录

图 12.1. 禁止同一账号多次登录


很好,现在只要有一个人使用user/user登陆过系统,其他人就不能再次登录了。这样可能出现一个问题,如果有人登陆的时候因为某些问题没有进行logout就退出了系统,那么他只能等到session过期自动销毁之后,才能再次登录系统。

实例在ch102。

 

posted @ 2013-10-18 16:42  似水若冰  阅读(598)  评论(0编辑  收藏  举报