通过配置我们可以实现两个需求 1、限制不允许第二个用户登录,2、第二个登陆用户踢掉前一个登陆用户
假设你的spring架构已经可以使用了(其他的主要功能完成),需要增加登录限制功能。
注:这里只写配置不写原理(不懂的就问度娘),其实个人认为先配置好跑起来再研究下原理最好了
- 第一步、使用注解加上
- @Autowired
- protected SessionRegistry sessionRegistry;
通过sessionRegistry可以获取系统当前在线人数和登录用户信息 - applicationContext-security.xml里你需要增加和修改的地方
- <session-management invalid-session-url="/timeout.jsp" session-authentication-error-url="/s.jsp" session-authentication-strategy-ref="sas" /> ***
- <custom-filter position="CONCURRENT_SESSION_FILTER" ref="concurrencyFilter" />
-
- <b:bean id="sas" class="org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy">
- <b:constructor-arg name="sessionRegistry" ref="sessionRegistry" /> **
- <b:property name="maximumSessions" value="1" />
- </b:bean>
- <!-- SESSION管理 -->
- <b:bean id="sessionRegistry" class="org.springframework.security.core.session.SessionRegistryImpl" /> ***
- <b:bean id="concurrencyFilter" class="org.springframework.security.web.session.ConcurrentSessionFilter">
- <b:property name="sessionRegistry" ref="sessionRegistry" /> **
- <b:property name="expiredUrl" value="/timeout.jsp" /><!-- 过期的Url -->
- </b:bean>
- <!-- 登录验证器 -->
- <b:bean id="loginFilter" class="com.oms.core.security.MyUsernamePasswordAuthenticationFilter">
- <!-- 处理登录的action -->
- <b:property name="filterProcessesUrl" value="/j_spring_security_check"/>
- <!-- 验证成功后的处理-->
- <b:property name="authenticationSuccessHandler" ref="loginLogAuthenticationSuccessHandler"/>
- <!-- 验证失败后的处理-->
- <b:property name="authenticationFailureHandler" ref="simpleUrlAuthenticationFailureHandler"/>
- <b:property name="authenticationManager" ref="authenticationManager"/>
- <!-- 注入DAO为了查询相应的用户 -->
- <b:property name="agentsUserTblDao" ref="agentsUserTblDao"/>
- <b:property name="passwordEncoder" ref="passwordEncoder"/>
- <b:property name="sessionAuthenticationStrategy" ref="sas" />**
- </b:bean>
加*号的地方是需要注意的附上我的applicationContext-security.xml配置信息或许对你有帮助参考文章:http://forum./forum/spring-projects/security/130963-sessionregistry-getallprincipals-return-empty
以上博客地址可以很好的帮助你理解
|