Search This Blog

Monday, February 21, 2011

Registering new bean classes in grails application programmatically

As part of amazing goodies of grails is the ability to extend your applications -  registering new classes programmaticaly is a breeze
As the grails guru Burt points out here it just requires minimal effort;

//first create a bean definition
def myBeanDef = new GenericBeanDefinition()
//set bean class
myBeanDef.setBeanClass(YourBeanClass)
//set scope
myBeanDef.setScope(BeanDefinition.SCOPE_SINGLETON)
//set others

//register
grailsApplication.mainContext.registerBeanDefinition "someName", myBeanDef 

Programming in grails is really a pleasure.

Saturday, February 12, 2011

Thursday, January 27, 2011

Getting user credentials in a grails application using Spring Security


Getting user credentials in a grails application using Spring Security is pretty easy

If you followed the instructions given for spring security plugin then LoginController takes care of storing the user credentaisl for you.
To retrieve the stored credentials you need to do these
1. Import SecurityContextHolder
import org.springframework.security.core.context.SecurityContextHolder as SCH

2.Get the principal
SCH.context?.authentication?.principal

3. once you get hold of the principal object you can retrieve and use any creds.

SCH.context?.authentication?.principal?.username


Update: Thanks for Burt for pointing out how I can make this easier.

I can use SpringSecurity service injected  by declaring
def springSecurityService

Then this can be used like 
                     springSecurityService.principal 
to get the principal.




Tuesday, January 11, 2011

Removing unwanted jar dependencies in the grails built war file

Peter Ledbrook shared a neat way of removing runtime dependency in the grails built war file. If for e.g. we wanted to exclude hsqldb
    grails.project.dependency.resolution = {
        inherits("global") {
            if (Environment.current == Environment.PRODUCTION) {
                exclude "hsqldb"
            }
        }
        ...
    }