views:

52

answers:

1

I'm wrapping up an implementation of Spring Security in Grails. This is my first implementation on Spring Security- previously I used Acegi.

Here's the problem I'm having. In Acegi, I was able to retrieve the authenticated user in the onInteractiveAuthenticationSuccessEvent() callback by accessing the SecurityContextHolder, getting the user principle, and then getting the 'live' User from the database. In Spring Security, the SecurityContextHolder does not seem to be available- when I try to access it, I get low level Grails complaints about no such method on such and such object.

So, my questions:

  • How can I access SecurityContextHolder from onInteractiveAuthenticationSuccessEvent

  • Or is there a better way of getting the authenticated principle?

Thanks, Dan

+2  A: 

You may need to update the import - it changed from Spring Security 2 to 3 - it's now org.springframework.security.core.context.SecurityContextHolder but the methods haven't changed.

But it's cleaner to use springSecurityService, which is accessible from the event callback using the appCtx variable:

onInteractiveAuthenticationSuccessEvent = { e, appCtx ->
   def principal = appCtx.springSecurityService.principal
   def user = User.get(principal.id)
}
Burt Beckwith
Thanks Burt- appreciate the several responses you've provided. Just out of curiosity, are you on the Grails development team? Or working closely with them?
ecodan
Yes, I work for SpringSource on the Grails team.
Burt Beckwith