zoukankan      html  css  js  c++  java
  • 第 12 章 管理会话

    转载:http://www.mossle.com/docs/auth/html/ch102-concurrent-session.html

    第 12 章 管理会话

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

    12.1. 添加监听器

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

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

    这种监听session生命周期的监听器主要用来收集在线用户的信息,比如统计在线用户数之类的事。有关如何自己编写listener统计在线用户数,可以参考:http://www.mossle.com/docs/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" />
        <session-management>
            <concurrency-control />
        </session-management>
    </http>
            

    12.3. 控制策略

    12.3.1. 后面的用户禁止登陆

    默认情况下,如果有一个人使用了一个账号登录了系统,其他人就不能再用这个账号登录了。

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

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

    禁止同一账号多次登录

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


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

    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="false"/>
    </http>
                

    想测试一下的话,先打开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下的用户也会被踢出系统了。

  • 相关阅读:
    POJ1417 True Liars
    POJ2912 Rochambeau
    POJ3904 Sky Code
    [SDOI2016]排列计数
    POJ2947 Widget Factory
    HDU5015 233 Matrix
    Problem 2242. -- [SDOI2011]计算器
    POJ2480 Longge's problem
    Problem 2818. -- Gcd
    LA3510 Pixel Shuffle
  • 原文地址:https://www.cnblogs.com/jeremy-blog/p/4142386.html
Copyright © 2011-2022 走看看