【问题标题】:Grails 3 - springSecurity reauthenticate and SessionRegistryGrails 3 - springSecurity reauthenticate 和 SessionRegistry
【发布时间】:2017-06-21 02:49:18
【问题描述】:

我的 Grails 应用程序(由 Spring Security 插件提供支持)有一个非常基本的支持“快速登录”功能,基本上提示用户只需输入密码即可登录,而 userId 存储在加密的 cookie 中。

所以在我的控制器中我正在使用 springSecurityService.reauthenticate(userid)
手动验证用户。

我的应用程序还要求不允许并发会话。因此,与上一行一起,我添加了

def authentication = SecurityContextHolder.context.authentication
def sessions = sessionRegistry.getAllSessions(authentication.getPrincipal(), false)
// [L]
sessions.each {
    it.expireNow()
}

问题是这些重新身份验证没有反映在sessionRegistry 中的条目中,这是上面代码的问题,在sessions 中我找不到该用户的任何会话。

我目前无法解决的问题是:

  • 用户 [U] 登录到位置 [A]
  • [U] 登录到位置 [B] => 会话 [A] 被强制过期
  • [U]不断导航/刷新[A],提示快速登录
  • [U] 再次登录到[A] => reauthenticate 被触发,sessionRegistry 没有添加任何内容
  • [U]不断导航/刷新[B],提示快速登录
  • [U]再次登录[B]
  • [U] 同时登录 [A] 和 [B]

我也试过添加
sessionRegistry.registerNewSession(newSessionId, authentication.getPrincipal())
[L] 的位置,但是这个会话和重新生成的会话似乎没有任何关系。

欢迎提出任何建议!提前致谢。

配置

  • Grails 3.2.4
  • Spring 安全插件(核心)3.1.1

【问题讨论】:

    标签: authentication grails spring-security grails3


    【解决方案1】:

    经过几次尝试,我想出了一个可行的解决方案

    // injected dependencies
    AuthenticationManager authenticationManager
    SessionAuthenticationStrategy sessionAuthenticationStrategy
    
    // code
    Authentication auth = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(user.email, params.password))
    SecurityContextHolder.context.authentication = authResult   // need to reassign to context, otherwise onAuthentication fails (wrong password)
    sessionAuthenticationStrategy.onAuthentication(authResult, request, response)
    

    诀窍似乎是调用onAuthentication,触发所有定义的请求过滤器,这依赖于我的并发会话管理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-31
      • 2017-05-31
      • 2012-10-07
      • 2013-10-31
      • 1970-01-01
      • 2016-06-23
      • 2014-05-29
      • 2015-07-08
      相关资源
      最近更新 更多