Issue with spring security plugin event callbacks
I created a property called lastLogin in my User domain-class that extends the AuthUser class from the spring security plugin. I want to update this every time a user logs in.
I read this User Guide: Events.
I added the following code in Config.groovy:
Code:
grails.plugins.springsecurity.onInteractiveAuthenticationSuccessEvent = { e, appCtx ->
User.withTransaction {
def user = User.get(appCtx.springSecurityService.currentUser.id)
println "DEBUG: ${new Date()} user logged in: ${user.username}"
}
}
After examining my log for a few hours, I noticed that the event gets called multiple times in short periods of time. Here is the output:
Code:
DEBUG: Thu May 12 19:31:32 EDT 2011 user logged in: thetruth300
DEBUG: Thu May 12 19:31:32 EDT 2011 user logged in: thetruth300
DEBUG: Thu May 12 19:31:32 EDT 2011 user logged in: thetruth300
DEBUG: Thu May 12 19:31:32 EDT 2011 user logged in: thetruth300
DEBUG: Thu May 12 19:31:32 EDT 2011 user logged in: thetruth300
DEBUG: Thu May 12 19:31:33 EDT 2011 user logged in: thetruth300
DEBUG: Thu May 12 19:31:33 EDT 2011 user logged in: thetruth300
DEBUG: Thu May 12 19:31:33 EDT 2011 user logged in: thetruth300
DEBUG: Thu May 12 19:31:33 EDT 2011 user logged in: thetruth300
DEBUG: Thu May 12 19:31:33 EDT 2011 user logged in: thetruth300
DEBUG: Thu May 12 19:31:33 EDT 2011 user logged in: thetruth300
DEBUG: Thu May 12 19:31:33 EDT 2011 user logged in: thetruth300
DEBUG: Thu May 12 19:31:39 EDT 2011 user logged in: thetruth300
DEBUG: Thu May 12 19:31:42 EDT 2011 user logged in: thetruth300
DEBUG: Thu May 12 19:31:44 EDT 2011 user logged in: thetruth300
DEBUG: Thu May 12 19:31:46 EDT 2011 user logged in: thetruth300
DEBUG: Thu May 12 19:31:48 EDT 2011 user logged in: thetruth300
DEBUG: Thu May 12 19:31:50 EDT 2011 user logged in: thetruth300
DEBUG: Thu May 12 19:31:55 EDT 2011 user logged in: thetruth300
DEBUG: Thu May 12 19:31:55 EDT 2011 user logged in: thetruth300
DEBUG: Thu May 12 19:34:41 EDT 2011 user logged in: williamtravissmith
DEBUG: Thu May 12 19:35:10 EDT 2011 user logged in: oddalias
DEBUG: Thu May 12 19:39:44 EDT 2011 user logged in: hereitcomes42
DEBUG: Thu May 12 19:39:48 EDT 2011 user logged in: hereitcomes42
DEBUG: Thu May 12 19:39:51 EDT 2011 user logged in: hereitcomes42
DEBUG: Thu May 12 19:42:15 EDT 2011 user logged in: OKComputer
I wanted to use the closure to update the user's lastLogin property. I added the following code:
Code:
grails.plugins.springsecurity.onInteractiveAuthenticationSuccessEvent = { e, appCtx ->
User.withTransaction {
def user = User.get(appCtx.springSecurityService.currentUser.id)
user.lastLogin = new Date()
user.save()
}
}
Since the callback is called multiple times, I get a StaleObjectStateException once in a while. Does anyone know why the callback happens so often in a short period of time??