Search code examples
grailsspring-security-rest

Grails REST security - Add user id to token


I want to add user id field to token returned from /api/login

Currently it's:

{
    "username": "user",
    "roles": [
        "ROLE_USER"
    ],
    "token_type": "Bearer",
    "access_token": "eyJhbGciOiJIUzI1NiJ9.2uk2YoHsyd7bqUdtUYN19ef..",
    "expires_in": 3600,
    "refresh_token": "eyJhbGciOiJIUzI1NiJ9.eyJwcmluY2lwYWwiOiJINH.."
}

I need:

{
    "id": "1",
    "username": "user",
    "roles": [
        "ROLE_USER"
    ],
    "token_type": "Bearer",
    "access_token": "eyJhbGciOiJIUzI1NiJ9.2uk2YoHsyd7bqUdtUYN19ef..",
    "expires_in": 3600,
    "refresh_token": "eyJhbGciOiJIUzI1NiJ9.eyJwcmluY2lwYWwiOiJINH.."
}

the target - queries with user id, like POST /api/something Is there any other approaches? Thanks in advance


Solution

  • You have not mentioned the Grails version, so I am posting the answer which I have implemented for Grails 2.4.4

    1st thing you need to implement the AccessTokenJsonRenderer interface in your custom class created under src/groovy like below.

    import grails.plugin.springsecurity.SpringSecurityUtils
    import grails.plugin.springsecurity.rest.token.AccessToken
    import grails.plugin.springsecurity.rest.token.rendering.AccessTokenJsonRenderer
    import groovy.json.JsonBuilder
    import org.springframework.beans.factory.annotation.Autowired
    import org.springframework.security.core.GrantedAuthority
    
    /**
     * Created by Prakash Thete on 17/04/2018
     */
    class CustomAppRestAuthTokenJsonRenderer implements AccessTokenJsonRenderer  {
    
        @Override
        String generateJson(AccessToken accessToken){
    
            // Add extra custom parameters if you want in this map to be rendered in login response
            Map response = [
                    id           : accessToken.principal.id,
                    username     : accessToken.principal.username,
                    access_token : accessToken.accessToken,
                    token_type   : "Bearer",
                    refresh_token: accessToken.refreshToken,
                    roles        : accessToken.authorities.collect { GrantedAuthority role -> role.authority }
            ]
    
            return new JsonBuilder( response ).toPrettyString()
        }
    }
    

    2nd thing you need to create the bean of our custom class in resources.groovy, like below

    // For overriding the token json renderer
    accessTokenJsonRenderer(CustomAppRestAuthTokenJsonRenderer)
    

    Now after hitting the api/login you will receive the id of the user along with the other details.

    Hope this helps !