how to implement Custom UserDetailsService in spring security core plugin?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

how to implement Custom UserDetailsService in spring security core plugin?

chichibek bros
hi group, im trying to add fullName and emal property to User domain class, and i want to retrieve in views does properties values, reading documentation i got to understand i need to imeplement my own UserDatailsService, but i can get where to put my code, i followed some links in google but i would like to see the best or recomended way to do it.

Thanks for your time
Reply | Threaded
Open this post in threaded view
|

Re: how to implement Custom UserDetailsService in spring security core plugin?

arcseldon
For a custom userdetailsservice, just create your custom service, and put it under the Grails ‘services’ hierarchy somewhere

Should look something like:

class CustomUserDetailsService implements GrailsUserDetailsService  {

.. whatever you want here..

}

Register it in Resources.groovy

userDetailsService(CustomUserDetailsService)

The Spring Security Core 2 plugin docs explain all this in sufficient detail if you get lost.

Good luck.



On 27 Mar 2014, at 04:45, chichibek bros <[hidden email]> wrote:

hi group, im trying to add fullName and emal property to User domain class, and i want to retrieve in views does properties values, reading documentation i got to understand i need to imeplement my own UserDatailsService, but i can get where to put my code, i followed some links in google but i would like to see the best or recomended way to do it.

Thanks for your time

Reply | Threaded
Open this post in threaded view
|

Re: how to implement Custom UserDetailsService in spring security core plugin?

chichibek bros
thanks Richard


2014-03-26 14:21 GMT-06:00 Richard Seldon <[hidden email]>:
For a custom userdetailsservice, just create your custom service, and put it under the Grails ‘services’ hierarchy somewhere

Should look something like:

class CustomUserDetailsService implements GrailsUserDetailsService  {

.. whatever you want here..

}

Register it in Resources.groovy

userDetailsService(CustomUserDetailsService)

The Spring Security Core 2 plugin docs explain all this in sufficient detail if you get lost.

Good luck.



On 27 Mar 2014, at 04:45, chichibek bros <[hidden email]> wrote:

hi group, im trying to add fullName and emal property to User domain class, and i want to retrieve in views does properties values, reading documentation i got to understand i need to imeplement my own UserDatailsService, but i can get where to put my code, i followed some links in google but i would like to see the best or recomended way to do it.

Thanks for your time


Reply | Threaded
Open this post in threaded view
|

Re: how to implement Custom UserDetailsService in spring security core plugin?

burtbeckwith
In reply to this post by arcseldon
It should go in src/groovy since it's not a Grails service, it just has Service in the name because it's a Spring Security interface. See section 10 in the docs at http://grails-plugins.github.io/grails-spring-security-core/docs/manual/ which discusses creating a custom implementation.

Burt

arcseldon wrote
For a custom userdetailsservice, just create your custom service, and put it under the Grails ‘services’ hierarchy somewhere

Should look something like:

class CustomUserDetailsService implements GrailsUserDetailsService  {

.. whatever you want here..

}

Register it in Resources.groovy

userDetailsService(CustomUserDetailsService)

The Spring Security Core 2 plugin docs explain all this in sufficient detail if you get lost.

Good luck.



On 27 Mar 2014, at 04:45, chichibek bros <[hidden email]> wrote:

> hi group, im trying to add fullName and emal property to User domain class, and i want to retrieve in views does properties values, reading documentation i got to understand i need to imeplement my own UserDatailsService, but i can get where to put my code, i followed some links in google but i would like to see the best or recomended way to do it.
>
> Thanks for your time
Reply | Threaded
Open this post in threaded view
|

Re: how to implement Custom UserDetailsService in spring security core plugin?

cfaddict
In reply to this post by chichibek bros
Maybe I am missing something here but if you are just trying to add some custom properties (email,firstName,lastName) and a custom method (getFullName) you can do so on the generated User Class.

package com.ssdemo.auth

class User {

        transient springSecurityService

        String username
        String password
        boolean enabled = true
        boolean accountExpired
        boolean accountLocked
        boolean passwordExpired

        String firstName
        String lastName
        String emailAddress

        static transients = ['springSecurityService']

        static constraints = {
                username blank: false, unique: true
                password blank: false
        }

        static mapping = {
                password column: '`password`'
        }

        String getFullName(){
               "$firstName $lastName"
        }

        Set<Role> getAuthorities() {
                UserRole.findAllByUser(this).collect { it.role } as Set
        }

        def beforeInsert() {
                encodePassword()
        }

        def beforeUpdate() {
                if (isDirty('password')) {
                        encodePassword()
                }
        }

        protected void encodePassword() {
                password = springSecurityService.encodePassword(password)
        }
}
Reply | Threaded
Open this post in threaded view
|

Re: how to implement Custom UserDetailsService in spring security core plugin?

burtbeckwith
You can do that, but then you have to load the instance from the database.  If you customize the UserDetails with extra data, it will be stored in memory as the authentication principal and save a database call.

Burt

cfaddict wrote
Maybe I am missing something here but if you are just trying to add some custom properties (email,firstName,lastName) and a custom method (getFullName) you can do so on the generated User Class.

package com.ssdemo.auth

class User {

        transient springSecurityService

        String username
        String password
        boolean enabled = true
        boolean accountExpired
        boolean accountLocked
        boolean passwordExpired

        String firstName
        String lastName
        String emailAddress

        static transients = ['springSecurityService']

        static constraints = {
                username blank: false, unique: true
                password blank: false
        }

        static mapping = {
                password column: '`password`'
        }

        String getFullName(){
               "$firstName $lastName"
        }

        Set<Role> getAuthorities() {
                UserRole.findAllByUser(this).collect { it.role } as Set
        }

        def beforeInsert() {
                encodePassword()
        }

        def beforeUpdate() {
                if (isDirty('password')) {
                        encodePassword()
                }
        }

        protected void encodePassword() {
                password = springSecurityService.encodePassword(password)
        }
}