5
votes

In my Config.groovy I used:

grails.plugins.springsecurity.useSecurityEventListener = true

grails.plugins.springsecurity.onInteractiveAuthenticationSuccessEvent = { e, appCtx ->
    User.withTransaction {
        def user = User.findById(appCtx.springSecurityService.principal.id)
        if(!user.isAttached())
            user.attach()
        user.lastLoginDate = new Date()
        user.save(flush: true, failOnError: true)
    }
}

My User domain has this field and I don't get any errors while logging in, however, the field is not updated.

I tried debugging everything from org.hibernate but I couldn't find any updateor other relevant statements.

Is there anything else I need to add anywhere in order to get this working?

For the record:

  • Grails 1.3.7
  • Spring Security Core 1.1.2
  • Hibernate 1.3.7
2
I'm using Grails 1.3.6 and SSC 1.1.2, with almost exactly the same code as you and for me it's working - so I'm thinking there must be something else that is the problem. Eg the user cannot be saved due to errors or something similar... The only thing I have that you don't is that I catch "OptimisticLockingFailureException" and then do a User.merge(user), but I doubt that should be the cause of your problems. - wwwclaes
I was able to solve it, actually really really stupid. See my answer below. - slhck

2 Answers

6
votes

Ok, the answer was plain simple. I had forgot the package name so it wouldn't find User.

So this one worked if your User object is in the package org.test:

org.test.User.withTransaction {
      def user = org.test.User.get(appCtx.springSecurityService.principal.id)
      user.lastLoginDate = new Date()
      user.save(failOnError: true)
}
0
votes

What version r u using of spring security core?? Look at this question and its relevant answer given by the author of spring security.... May be you need to upgrade spring security version?

:: UPDATE ::

This post have the same issue as yours, hopefully you will get solution by looking into it...