我试图限制用户不止一次签名(迫使以前的会话过期)。Spring Security,表单登录和并发会话
我已经检查了有关主题here的文档。我已经把它设置得非常类似于文档,但用户并没有被限制在一个会话中。我可以使用同一个用户多次(在不同的浏览器中)登录,并且有多个并发会话正在进行。
下面是我认为是我的安全设置的相关位。我使用自定义的UserDetailsService,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文件中的监听器。
据我所知,我已经根据文档配置了它。我不明白为什么这不起作用。这可能与我使用基于表单的登录有关吗?或者我上面提到的自定义实现?
感谢您的回答,我有类似的问题,但在阅读本文后,我在['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
我一直在寻找这个答案,所以很多时间:)它的作品! – sanluck 2016-04-14 02:19:29