Grails REST安全性 - 将用户ID添加到令牌

时间:2018-04-08 07:53:49

标签: grails spring-security-rest

我想将用户ID字段添加到从/ api / login

返回的令牌

目前它:

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

我需要:

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

目标 - 使用用户ID查询,如POST / api / something 还有其他方法吗? 提前致谢

2 个答案:

答案 0 :(得分:6)

你没有提到Grails版本,所以我发布了我为Grails 2.4.4实现的答案

首先需要在AccessTokenJsonRenderer下创建的自定义类中实现src/groovy界面,如下所示。

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()
    }
}

您需要在resources.groovy中创建自定义类的bean,第二件事,如下所示

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

现在点击api/login后,您将收到该用户的ID以及其他详细信息。

希望这有帮助!

答案 1 :(得分:0)

如果要在Auth JSON响应中添加有关用户的更多详细信息,可以使用主体ID并按以下方式查询用户。

注意添加@Transactional注释。

import grails.gorm.transactions.Transactional
import grails.plugin.springsecurity.rest.token.AccessToken
import grails.plugin.springsecurity.rest.token.rendering.AccessTokenJsonRenderer
import groovy.json.JsonBuilder
import org.springframework.security.core.GrantedAuthority


@Transactional
class CustomAuthTokenRenderer implements AccessTokenJsonRenderer {


    @Override
    String generateJson(AccessToken accessToken) {

        // User the principal ID to get an instance of the user from the database
        User user = User.get accessToken.principal.id as Long

        // Add extra custom parameters if you want in this map to be rendered in login response

        Map res = [
                id               : user.id,
                username         : user.username,
                firstName        : user.firstName,
                lastName         : user.lastName,
                profilePicture   : user.profilePicture,
                dateOfBirth      : user.dob,
                expiration       : accessToken.expiration,
                access_token     : accessToken.accessToken,
                token_type       : "Bearer",
                refresh_token    : accessToken.refreshToken,
                roles            : accessToken.authorities.collect { GrantedAuthority role -> role.authority },
                friends: user.friends.collect { Friend friend ->
                    [
                            id          : friend.id,
                            firstName   : friend.firstName,
                            dateCreated : friend.dateCreated,
                            lastUpdated : friend.lastUpdated,
                    ]
                }
        ]

        return new JsonBuilder(res).toPrettyString()
    }
}

要添加到响应中的内容无关紧要,对于用户对象,您几乎可以添加任何内容。只是不要试图进行太多查询,因为这将导致登录过程非常缓慢。