Grails 3 - springSecurity 重新验证和 SessionRegistry

Grails 3 - springSecurity reauthenticate and SessionRegistry

我的 Grails 应用程序(由 Spring 安全插件提供支持)有一个非常基本的支持 'quick login' 功能,基本上提示用户仅输入密码即可登录,而 userId存储在加密的 cookie 中。

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

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

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

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

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

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

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

配置

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

// 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,触发所有定义的请求过滤器,这依赖于我的并发会话管理。