2010-09-08 83 views
4

我试图限制用户不止一次签名(迫使以前的会话过期)。Spring Security,表单登录和并发会话

我已经检查了有关主题here的文档。我已经把它设置得非常类似于文档,但用户并没有被限制在一个会话中。我可以使用同一个用户多次(在不同的浏览器中)登录,并且有多个并发会话正在进行。

下面是我认为是我的安全设置的相关位。我使用自定义的UserDetailsS​​ervice,UserDetails和AuthenticationFilter实现。


    <http entry-point-ref="authenticationEntryPoint"> 
     <!-- Make sure everyone can access the login page --> 
     <intercept-url pattern="/login.do*" filters="none" /> 

     [...] 

     <custom-filter position="CONCURRENT_SESSION_FILTER" ref="concurrencyFilter" /> 
     <custom-filter position="FORM_LOGIN_FILTER" ref="authenticationFilter" /> 

     <logout logout-url="/logout" logout-success-url="/login.do" /> 
    </http> 

    <authentication-manager alias="authenticationManager"> 
     <authentication-provider user-service-ref="userDetailsService"> 
      <password-encoder hash="sha" /> 
     </authentication-provider> 
    </authentication-manager> 

    <beans:bean id="userDetailsService" class="[...]"> 
     <beans:property name="userManager" ref="userManager" /> 
    </beans:bean> 

    <beans:bean id="authenticationFilter" class="[...]"> 
     <beans:property name="authenticationManager" ref="authenticationManager" /> 
     <beans:property name="eventPublisher"> 
      <beans:bean 
       class="org.springframework.security.authentication.DefaultAuthenticationEventPublisher" /> 
     </beans:property> 
     <beans:property name="filterProcessesUrl" value="/security_check" /> 
     <beans:property name="authenticationFailureHandler"> 
      <beans:bean 
       class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"> 
       <beans:property name="defaultFailureUrl" value="/login.do?login_error=true" /> 
      </beans:bean> 
     </beans:property> 
     <beans:property name="sessionAuthenticationStrategy" 
      ref="sessionAuthenticationStrategy" /> 
    </beans:bean> 

    <beans:bean id="authenticationEntryPoint" 
     class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint"> 
     <beans:property name="loginFormUrl" value="/login.do" /> 
    </beans:bean> 

    <beans:bean id="concurrencyFilter" 
     class="org.springframework.security.web.session.ConcurrentSessionFilter"> 
     <beans:property name="sessionRegistry" ref="sessionRegistry" /> 
     <beans:property name="expiredUrl" value="/login.do?login_error=true!" /> 
    </beans:bean> 

    <beans:bean id="sessionAuthenticationStrategy" 
     class="org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy"> 
     <beans:constructor-arg name="sessionRegistry" 
      ref="sessionRegistry" /> 
     <beans:property name="maximumSessions" value="1" /> 
    </beans:bean> 

    <beans:bean id="sessionRegistry" 
     class="org.springframework.security.core.session.SessionRegistryImpl" /> 

我也注册org.springframework.security.web.session.HttpSessionEventPublisher在我的web.xml文件中的监听器。

据我所知,我已经根据文档配置了它。我不明白为什么这不起作用。这可能与我使用基于表单的登录有关吗?或者我上面提到的自定义实现?

回答

9

我想通了。如果您重新实现UserDetails,则必须为SessionRegistryImpl提供一个使用的hashCode()方法。这在文档中没有提及。

+0

感谢您的回答,我有类似的问题,但在阅读本文后,我在['User'](http://static.springsource.org/spring)中发现了关于'hashCode()'和'equals() -security/site/docs/3.0.x/apidocs/org/springframework/security/core/userdetails/User.html)类的注释,这暗示了在你的类中重写这两个方法。 – Xaerxess 2011-11-30 09:05:52

+0

我一直在寻找这个答案,所以很多时间:)它的作品! – sanluck 2016-04-14 02:19:29

2

我知道这已被回答,但有一个更简单的解决方案,不需要配置特定的过滤器。您可以添加到HTTP标签下面的XML配置您的会话:

<session-management invalid-session-url="/login"> 
    <concurrency-control max-sessions="1" expired-url="/login" /> 
</session-management> 

基于表单登录配置的其余部分也过于复杂。我在这里写了一篇文章http://codehustler.org/blog/spring-security-form-login-tutorial/,其中显示了如何正确配置表单登录。

相关问题