Using Customized Userdetail Service of Grails with NeoFourj Database

Posted By : Rajeev Kumar | 29-Dec-2014

Neo4j database is a graph based database which uses node and edges to stores data. It is different from traditional database which uses tables to store data. So Userdetail Service of grails needs to be customized when using neo4j as database.


So to use customized service, make a service in grails which implements the grail's userDetailService. Now in the custom service,

Make a reference of GrantedAuthority as given below :

static final GrantedAuthority NO_ROLE = new SimpleGrantedAuthority(SpringSecurityUtils.NO_ROLE)


Make a reference of Grails Application as follows : 

 

GrailsApplication grailsApplication


Define repositories or service you want to use in this service.

 

 

For example

 

UserRepository userRepository

 

Now override the method loadUserByloadUserByUsername as follows:

@Override

    public UserDetails loadUserByUsername(String username, boolean loadRoles)

            throws UsernameNotFoundException, DataAccessException {

        //Your logic goes here.

    }
 

 

Below is the example of overriding method.

@Override

    public UserDetails loadUserByUsername(String username, boolean loadRoles)

            throws UsernameNotFoundException, DataAccessException {

        loadUserByUsername(username)

    }

UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        def conf = SpringSecurityUtils.securityConfig

        Neo4jTemplate neo4jTemplate = grailsApplication.mainContext.getBean("neo4jTemplate")

        GraphDatabaseService graphDatabaseService = grailsApplication.mainContext.getBean("graphDatabaseService")

        Transaction tx = graphDatabaseService.beginTx()

        

        def user

        def authorities

        try{

            String userClassName = conf.userLookup.userDomainClassName

            user = userRepository.findByUsername(username);

            authorities = new ArrayList<GrantedAuthority>()

            user.each {

                it.role.each{

                    SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(it.authority)

                    authorities.add(simpleGrantedAuthority);

                }

            }

            authorities = !authorities || authorities.isEmpty() ? [NO_ROLE] : authorities

        }finally{

            tx.success()

            tx.finish()

        }

        

        createUserDetails user, authorities

    }

    protected UserDetails createUserDetails(user, Collection<GrantedAuthority> authorities) {

        def conf = SpringSecurityUtils.securityConfig

        String usernamePropertyName = conf.userLookup.usernamePropertyName

        String username = user."$usernamePropertyName"

        String password = user.password

        boolean enabled = user.isEnabled

        boolean accountExpired = user.accountExpired

        boolean accountLocked = user.accountLocked

        boolean passwordExpired = user.passwordExpired

        

        new GrailsUser(username, password, enabled, !accountExpired, !passwordExpired,

                !accountLocked, authorities, user.id)

    }

Now in resources.groovy inject the service or repositories you have used in your custom UserDetailService as given belowv:
 

userDetailsService("Your custom service class name"){

       grailsApplication = ref('grailsApplication')

       userRepository = ref('userRepository')

   }

About Author

Author Image
Rajeev Kumar

Rajeev is an experienced Grails and angular Js Developer. Rajeev likes programming, cooking, net surfing, watching news.

Request for Proposal

Name is required

Comment is required

Sending message..
Yono Rummy YonoRummy Rummy Codes Rummy Promo Codes Rummy Yono App Yono Rummy APK Game Yono Rummy Yono Rummy Games sarkari naukri dunia TOTOSGP TOTO SGP TOTOSGP TOTO SGP TOTOSGP TOTOSGP India Games Online Platform Popular Games in India India Gaming Experience Online Games India Best Rummy Games in India India Games Collection Teen Patti Games India Rummy Games Online India India Rummy Gaming Platform Popular Rummy Platform India Online Rummy Games India India Mobile Gaming India Gaming Platform Indian Online Games India Games and Entertainment India Gaming Community India Online Gaming Platform Teen Patti Online Games India Rummy and Teen Patti Games