无法获取“的会话ID”;“记住我”;spring安全登录事件侦听器中的用户

无法获取“的会话ID”;“记住我”;spring安全登录事件侦听器中的用户,spring,session,grails,login,spring-security,Spring,Session,Grails,Login,Spring Security,我正在使用grails和spring security 3.0.3编写一个web应用程序,它要求我保存所有成功登录的数据库记录,包括用户名、时间、ip和会话ID 我已经创建了一个登录侦听器(groovy代码): 类DpLoginListener实现ApplicationListener{ Application event上的无效(InteractiveAuthenticationSuccessEvent事件){ def source=event.getSource() def principa

我正在使用grails和spring security 3.0.3编写一个web应用程序,它要求我保存所有成功登录的数据库记录,包括用户名、时间、ip和会话ID

我已经创建了一个登录侦听器(groovy代码):

类DpLoginListener实现ApplicationListener{
Application event上的无效(InteractiveAuthenticationSuccessEvent事件){
def source=event.getSource()
def principal=source.principal
def details=source.details
TrafficLogin.withTransaction{
new TrafficLogin(userId:principal.id,ipAddress:details.remoteAddress,sessionId:details.sessionId?:“记住我”).save(FailOneError:true)
}
}
}
当用户登录时,这会按预期触发,但当有人作为“记住我”用户返回站点时,sessionId为null。这是预期的行为吗?登录成功时不应该创建会话吗

我试图通过添加一个单独的SessionCreationEvent侦听器来解决这个问题,该侦听器将查找用户的最新数据库登录记录,并在会话存在时立即使用正确的sessionId更新该行,但似乎这个会话创建事件从未触发,我也不知道为什么

会话创建侦听器如下所示:

class DpSessionCreatedListener implements ApplicationListener<SessionCreationEvent> {
  void onApplicationEvent(SessionCreationEvent event) {
    def source = event.getSource()
    def principal = source.principal
    def details = source.details
    TrafficLogin.withTransaction {
      def rememberMe = TrafficLogin.find("from TrafficLogin as t where t.userId=? and t.sessionId='remember me' order by t.dateCreated desc", principal.id)
      if (rememberMe) {
        rememberMe.sessionId = details.sessionId
        rememberMe.save(failOnError:true)
      }
    }
  }
}
类DpSessionCreatedListener实现ApplicationListener{ Application event(SessionCreationEvent事件)无效{ def source=event.getSource() def principal=source.principal def details=source.details TrafficLogin.withTransaction{ def rememberMe=TrafficLogin.find(“从TrafficLogin作为t,其中t.userId=?和t.sessionId='memberme'按t.dateCreated desc排序”,principal.id) 如果(记住){ rememberMe.sessionId=details.sessionId memberme.save(failOnError:true) } } } } 并且在我的resources.groovy文件中为它定义了一个bean,其方式与登录侦听器相同,可以很好地启动


有没有关于如何正确设置这个sessionId的想法?

与grails无关,但如果grails支持,这可能会提供一个解决方法

class DpSessionCreatedListener implements ApplicationListener<SessionCreationEvent> {
  void onApplicationEvent(SessionCreationEvent event) {
    def source = event.getSource()
    def principal = source.principal
    def details = source.details
    TrafficLogin.withTransaction {
      def rememberMe = TrafficLogin.find("from TrafficLogin as t where t.userId=? and t.sessionId='remember me' order by t.dateCreated desc", principal.id)
      if (rememberMe) {
        rememberMe.sessionId = details.sessionId
        rememberMe.save(failOnError:true)
      }
    }
  }
}