【问题标题】:Invalidate spring security session使 Spring 安全会话无效
【发布时间】:2011-06-04 19:24:23
【问题描述】:

我需要使用户会话无效(或踢出)。该应用程序仅限制用户每个容器只能登录一个用户。

我尝试从会话注册表中调用 removeSessionInformation,完成解锁用户。这样其他用户就可以使用被踢出的会话用户名登录。

但是那个被踢的用户的 SessionContextHolder 仍然存在。所以他们仍然具有访问受保护页面的相同权限。

如何从指定的会话注册信息中使SessionContextHolder的Principal失效或移除?

ps:在我的旧应用程序中,我在 UserDomain (UserDetails) 中提供了一个保存 HttpSession 的变量。当他们需要踢用户时,我只是从指定的 UserDomain 中使 HttpSession 无效。但是我不知道在春天怎么做(它比 HttpSession 更可能删除 SessionContextHolder 的 Principal )。实现与春季的SessionRegistryImpl几乎相同。

【问题讨论】:

    标签: java spring session spring-security invalidation


    【解决方案1】:

    您可能想考虑 Spring Security 并发控制。您可以对其进行配置以限制每个用户的并发会话数,并在超过该数量时使现有会话过期(终止)。

    Spring Security Session Management

    这是我们配置的 sn-p(Spring 3):

    <http>
        ...
        <session-management>
            <concurrency-control max-sessions="1"/>
        </session-management>
        ...
    </http>
    

    【讨论】:

      【解决方案2】:

      我猜这是这样做的方法:

      SecurityContextHolder.getContext().setAuthentication(null)
      

      来自SecurityContext.setAuthentication(Authentication)Javadocs:

      改变当前认证的 主体,或删除 身份验证信息。

      参数:身份验证
      - 新的 身份验证令牌,如果没有,则返回 null 进一步的认证信息 应该保存

      【讨论】:

      • 这个答案是正确的,还要确保你的网络会话无效。
      • 嗨,布莱恩,我认为您的回答只会使当前登录用户会话的SecurityContextHolder 无效(或删除)。我需要的是删除其他用户会话的SecurityContextHolder。我们可以在 sping 做吗?
      • 这不适用于其他用户
      • @z0mb1ek 不,但这也不是问题所要求的
      【解决方案3】:

      您还可以执行以下操作来清除 SpringSecurity Session:

      SecurityContextHolder.clearContext()
      

      【讨论】:

        【解决方案4】:

        这是从我的 application-security.xml 中提取的

        class="org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy"
                p:maximumSessions="1" <br>
                            **p:exceptionIfMaximumExceeded="true"** >
                <constructor-arg name="sessionRegistry" ref="sessionRegistry" />
        

        尝试在最大会话数之后添加粗体字行

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-10-14
          • 1970-01-01
          • 1970-01-01
          • 2018-12-19
          • 2014-03-17
          • 2015-08-05
          • 2012-01-18
          相关资源
          最近更新 更多