spring security扩展自定义会话管理(一)控制用户重复登陆

springsecurity扩展自定义会话管理(一)控制用户重复登陆

springsecurity有控制单账号只能在一个地方登陆的功能,后登陆用户将踢掉前登陆用户;或者限制账号重复登陆,一个账号没有退出,另外一个人想用这个账号就登陆不上。

现在我想根据原有的这些功能参考,做一个管理员可以将某正在session中的用户踢出系统的功能。便于管理员修改了登陆用户的权限或者部门设置后,强制让该用户重新登录。

主要参考了如下两篇文章:
http://www.family168.com/oa/springsecurity/html/ch214-smart-concurrent.html
http://www.blogjava.net/beyondwcm/archive/2009/05/08/269545.html

主要涉及的类如下(我自己的山寨理解):
HttpSessionEventPublisher             监听session创建和销毁
ConcurrentSessionFilter                  每次有http请求时校验,看你当前的session是否是过期的
SessionRegistryImpl                       存放session中的信息,并做处理
ConcurrentSessionControllerImpl     用户登入登出的控制
SessionInformation       存储session中信息的model

先实现springsecurity文档上的,限制用户重复登陆,后登陆用户将前登陆用户冲掉 ,只需要在xml中配置如下:
<authentication-manager alias="authenticationManager"
session-controller-ref="currentController" />
<beans:bean id="concurrentSessionFilter"   class="org.springframework.security.concurrent.ConcurrentSessionFilter">
<custom-filter position="CONCURRENT_SESSION_FILTER" />
<beans:property name="sessionRegistry" ref="sessionRegistry" />
<!-- 踢出的用户转向的页面-->
<beans:property name="expiredUrl" value="/user/user.action" />
<beans:property name="logoutHandlers">
   <beans:list>
    <beans:bean class="org.springframework.security.ui.logout.SecurityContextLogoutHandler" />
    <beans:bean
     class="org.springframework.security.ui.rememberme.TokenBasedRememberMeServices">
     <beans:property name="key" value="e37f4b31-0c45-11dd-bd0b-0800200c9a66"/>
     <beans:property name="userDetailsService" ref="userDetailsService"/>
    </beans:bean>
   </beans:list>
</beans:property>
</beans:bean>
<beans:bean id="sessionRegistry"   class="org.springframework.security.concurrent.SessionRegistryImpl" />
<beans:bean id="currentController"
class="org.springframework.security.concurrent.ConcurrentSessionControllerImpl">
<beans:property name="sessionRegistry" ref="sessionRegistry" />
<!-- true限制不允许第二个用户登录,false第二个登陆用户踢掉前一个登陆用户 -->
<beans:property name="exceptionIfMaximumExceeded" value="false" />
<!-- 等第二种情况是,允许同时多少个用户同时登陆 -->
<beans:property name="maximumSessions" value="2"/>
</beans:bean>

其中web.xml还需要添加:
<!-- 登入和登出时对SessionRegistryImpl进行处理 -->
<listener>
<listener-class>org.springframework.security.ui.session.HttpSessionEventPublisher</listener-class>
</listener>

猜你喜欢

转载自lanluo.iteye.com/blog/1177471
今日推荐