【问题标题】:Spring security concurrent session is not working as desiredSpring 安全并发会话未按预期工作
【发布时间】:2013-04-13 17:07:59
【问题描述】:

不是限制每个用户一个会话,而是限制一个会话

整个应用程序。

因此,如果一个用户登录,则没有人可以登录。

这是我的配置

<session-management invalid-session-url="/login">
        <concurrency-control error-if-maximum-exceeded="true" max-sessions="1" />
     </session-management>  

我什至在 web.xml 中添加了监听器。

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">
    <!-- HTTP security configurations -->
    <http auto-config="true" use-expressions="true">
        <form-login login-processing-url="/resources/j_spring_security_check"
            login-page="/login" default-target-url="/index"
            authentication-success-handler-ref="myAuthenticationSuccessHandler"
            authentication-failure-url="/login?login_error=t" />
        <logout invalidate-session="true"
            logout-url="/resources/j_spring_security_logout" success-handler-ref="myLogoutSuccessHandler"/>
        <!-- Configure these elements to secure URIs in your application -->
        <intercept-url pattern="/choices/**" access="hasRole('ROLE_ADMIN')" />
        <intercept-url pattern="/member/**" access="isAuthenticated()" />
        <intercept-url pattern="/resources/**" access="permitAll" />
        <intercept-url pattern="/**" access="permitAll" />

     <session-management invalid-session-url="/login">
            <concurrency-control error-if-maximum-exceeded="true"
                max-sessions="1" />
        </session-management> 
    </http>

    <!-- Configure Authentication mechanism -->
    <authentication-manager alias="authenticationManager">
        <authentication-provider ref="customDaoAuthenticationProvider">
        </authentication-provider>
    </authentication-manager>

    <beans:bean id="myAuthenticationSuccessHandler" class="com.test.connect.web.login.MyAuthenticationSuccessHandler"/>
    <beans:bean id="myLogoutSuccessHandler" class="com.test.connect.web.login.MyLogoutSuccessHandler"/>

</beans:beans>

【问题讨论】:

  • 你如何测试这个?我希望不是来自同一个浏览器?
  • @AleksandrM 当然来自不同的浏览器
  • 显示完整的 spring-security 配置。
  • @AleksandrM skydrive.live.com/…

标签: session spring-security


【解决方案1】:

根据您提供的配置,其中包括自定义 AuthenticationProvider,以及您遇到的问题,我猜您返回的自定义 UserDetails 实现未正确实现 equals 和 hashCode 方法。

请确保您已在任何自定义 UserDetails 实现上正确实现 equals 和 hashCode,因为这些方法用于查找用户是否包含活动会话。

【讨论】:

  • 您好,我没有使用任何自定义 Userdetails 实现。我在 retriveUser 方法中返回以下 org.springframework.security.core.userdetails.UserDetails。
  • 您确定您的 customDaoAuthenticationProvider 会返回唯一用户吗?如果它意外返回具有相同用户名的 User 对象,则会导致类似的问题。如果这没有帮助,请尝试启用调试日志记录并附加第一次和第二次登录的日志。
  • 根据日志,一切正常。唯一的问题是我的浏览器。有时成功登录后 IE7 没有重定向。
【解决方案2】:

这里只想强调一下,确保equals和hashCode方法返回为真。如果方法未返回 true,则不会终止或终止现有会话。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 2019-07-30
    • 2020-10-30
    • 2019-04-08
    • 1970-01-01
    相关资源
    最近更新 更多