【问题标题】:Updated spring security context not available in session on redirect更新的弹簧安全上下文在重定向会话中不可用
【发布时间】:2019-11-01 03:57:32
【问题描述】:

更新的 spring 安全上下文在重定向时不可用

在我的一个控制器中 - 在重定向之前 - 我执行以下代码:

Credential oldCredential = (Credential) authentication.getCredentials();
            Authentication authenticationToken = new AuthenticationToken(new Credential(oldCredential.getCookieValue(), oldCredential.getPassword()),
                    updatedAccountDetails);
            SecurityContextHolder.getContext().setAuthentication(authenticationToken);

当我在设置后检查身份验证对象时,我看到身份验证上下文已正确更新。执行上面的代码后,我重定向。 重定向完成后,加载的安全上下文是旧的!也就是说:当我检查安全上下文时,它包含 not 新的身份验证令牌。

如果我为测试执行以下操作 - 我在 setAuthentication(...) 之后添加了 setAttributeE(...) - 我在会话中获得了新的 authenticationToken 但(显然)在不同的密钥下:test.我为 SecurityContextHolder(MODE_THREADLOCAL、MODE_INHERITABLETHREADLOCAL、MODE_GLOBAL)尝试了几种策略,尽管我认为默认的 MODE_THREADLOCAL 适合我的应用程序。

request.getSession(false).setAttribute("test", authenticationToken);

奇怪的是,如果我调试并检查 SecurityContextPersistenceFilter HttpSessionSecurityContextRepository 我看到 重定向加载的上下文实际上是新的!出于某种原因 - 目前我不知道 - 这个负载在稍后的某个时间点被旧的安全上下文替换(?)。

【问题讨论】:

    标签: spring spring-security


    【解决方案1】:

    确保您已调用 setAuthenticated(true),否则不会更新安全上下文。

    【讨论】:

      猜你喜欢
      • 2020-09-19
      • 2016-09-29
      • 2012-01-18
      • 2016-10-14
      • 1970-01-01
      • 1970-01-01
      • 2018-11-24
      • 2013-07-24
      相关资源
      最近更新 更多