customizing Acegi (Springs Security) plugin for Grails

I worked on a project today using Spring Security (formerly known as Acegi) plugin for Grails. I needed to hold some information in session right after someone logged in but didnt know how to go about doing it. After toying around for about a half hour I decided to check out the documentation. What I found was everything I needed and a little more.

I found that the SecurityConfig.groovy file will allowed me to customize Spring Security for all of my needs.

Here is a list of events that you can latch on to:

   onInteractiveAuthenticationSuccessEvent = { e, appCtx ->
      // handle InteractiveAuthenticationSuccessEvent
   onAbstractAuthenticationFailureEvent = { e, appCtx ->
      // handle AbstractAuthenticationFailureEvent
   onAuthenticationSuccessEvent = { e, appCtx ->
      // handle AuthenticationSuccessEvent
   onAuthenticationSwitchUserEvent = { e, appCtx ->
      // handle AuthenticationSwitchUserEvent
   onAuthorizationEvent = { e, appCtx ->
      // handle AuthorizationEvent

For my needs I will use the onAuthenticationSuccessEvent to set my session info. Here is what my code looked like when it was all said and done:

  onAuthenticationSuccessEvent = { e, appCtx ->
    //lets get the principal
    def p = e.source.principal
    println 'p='+p.username
    //creating an instance of the account holder
    def account = Account.findByUsername(p.username)

    //creating my object to be held in session
    def sessionObject = ObjectToBeHeldInSession.findByAccountAndIsCurrent(account, true)

    //setting session variable
    session.sessionObject = sessionObject 


Just another reason why I like working with Grails and the Spring Security plugin.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s