Java Spring安全性、表单登录和并发会话
我试图限制用户多次签名(强制上一个会话过期) 我已经查阅了关于这个问题的文件。我将其设置为与文档非常类似,但用户并不是一次只能进行一个会话。我可以使用同一个用户多次登录(在不同的浏览器中),并有多个并发会话 以下是我认为与我的安全设置相关的内容。我正在使用自定义UserDetailsService、UserDetails和AuthenticationFilter实现Java Spring安全性、表单登录和并发会话,java,spring,authentication,spring-security,Java,Spring,Authentication,Spring Security,我试图限制用户多次签名(强制上一个会话过期) 我已经查阅了关于这个问题的文件。我将其设置为与文档非常类似,但用户并不是一次只能进行一个会话。我可以使用同一个用户多次登录(在不同的浏览器中),并有多个并发会话 以下是我认为与我的安全设置相关的内容。我正在使用自定义UserDetailsService、UserDetails和AuthenticationFilter实现 <http entry-point-ref="authenticationEntryPoint">
<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" />
[...]
我还在我的web.xml文件中注册了org.springframework.security.web.session.HttpSessionEventPublisher
据我所知,我已经根据文档对此进行了配置。我不知道为什么这不起作用。这可能与我使用基于表单的登录有关吗?还是我上面提到的自定义实现?我想出来了。如果重新实现UserDetails,则必须提供一个hashCode()方法供SessionRegistryImpl使用。文档中没有提到这一点。我知道已经回答了这个问题,但有一个更简单的解决方案,它不需要配置特定的过滤器。您可以向http标记添加以下XML来配置会话:
<session-management invalid-session-url="/login">
<concurrency-control max-sessions="1" expired-url="/login" />
</session-management>
基于表单的登录配置的其余部分也过于复杂。我在这里写了一篇文章,展示了如何正确配置表单登录。谢谢你的回答,我也有类似的问题,但在阅读了这篇文章后,我在类注释中发现了关于
hashCode()
和equals()
的注释,这暗示了在类中重写这两个方法。