gpt4 book ai didi

authentication - Grails 3-springSecurity重新认证和SessionRegistry

转载 作者:行者123 更新时间:2023-12-02 14:49:18 26 4
gpt4 key购买 nike

我的Grails应用程序(带有Spring Security插件)具有非常基本的支持“快速登录”功能,基本上是在userId存储在加密的cookie中时提示用户仅键入密码登录。

所以我在 Controller 中使用springSecurityService.reauthenticate(userid)手动验证用户身份。

我的应用程序还具有不允许并发 session 的要求。因此,与上一行一样,我添加了

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

问题是这些重新认证不会反射(reflect)在 sessionRegistry的条目中,而上述代码是一个问题,因为在 sessions中我找不到该用户的任何 session 。

我目前无法解决的问题是:
  • 用户[U]登录到位置[A]
  • [U]登录到位置[B] => session [A]被迫过期
  • [U]继续导航/刷新[A],并提示您快速登录
  • [U]再次登录[A] =>触发reauthenticate,未向sessionRegistry添加任何内容
  • [U]继续导航/刷新[B],并提示您快速登录
  • [U]再次登录到[B]
  • [U]登录在[A]和[B]上

  • 我也尝试添加 sessionRegistry.registerNewSession(newSessionId, authentication.getPrincipal())处于 [L]位置,但是此 session 与重新生成的 session 似乎没有任何关系。

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

    设定档
  • Grails 3.2.4
  • Spring Security插件(核心)3.1.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,触发所有已定义的请求过滤器,这依赖于我的并发 session 管理。

    关于authentication - Grails 3-springSecurity重新认证和SessionRegistry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42029206/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com