spring security 登录限制 防止同一用户重复登录 同一个账号登录可以踢掉前一个用户 配置

一、限制用户登录数和session自动托管

  1.maximumSessions:限制登录人数

  2.exceptionIfMaximumExceeded:

    • 为true同一账户只能登录一次,
    • 为false同一账户可以登录多次如果配置了org.springframework.security.web.session.ConcurrentSessionFilter则会踢出前一个登录的session

  3.sessionRegistry配置session管理

  4.concurrentSessionFilter如果不配置这个则踢不出上一个登录的session,会一个账户可以登录多次

<bean id="sas" class="org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy">
  <property name="maximumSessions" value="1"/>
<property name="exceptionIfMaximumExceeded" value="false"></property> 
<constructor-arg>
  <ref bean="sessionRegistry"/>
</constructor-arg>
</bean>
<bean id="sessionRegistry" class="org.springframework.security.core.session.SessionRegistryImpl"></bean> 
<bean id="concurrentSessionFilter"
  class="org.springframework.security.web.session.ConcurrentSessionFilter">
  <property name="sessionRegistry" ref="sessionRegistry"/>
</bean>

  

二、将配置好的bean进行注入

  将上述的bean设置好通过下述方式配置好即可

<s:http access-denied-page="/403.jsp" auto-config='true'>

  <s:session-management invalid-session-url="/login.jsp" session-authentication-strategy-ref="sas"/>
  <s:custom-filter position="CONCURRENT_SESSION_FILTER" ref="concurrentSessionFilter" /> 
</s:http>

  

三、另附思路启发参考网页

http://www.mossle.com/docs/auth/html/ch214-smart-concurrent.html

猜你喜欢

转载自www.cnblogs.com/y-bobo/p/9300408.html