1

我正在使用 grails spring 安全插件。当我登录时,我将用户名、令牌和角色作为 json 响应。除了这些属性,我还想获得一些其他属性,例如名字、姓氏等。使用 grails spring security rest 插件登录应用程序时,如何将这些值作为 json 响应获取?

4

2 回答 2

1

您可以添加令牌声明,我通常建议不要修改插件代码,但是

您可以通过在

AbstractJwtTokenGenerator(或者最好通过覆盖它)

JWTClaimsSet generateClaims(UserDetails details, String serializedPrincipal, Integer expiration) {
        JWTClaimsSet claimsSet = new JWTClaimsSet()
        claimsSet.setSubject(details.username)

        log.debug "Setting expiration to ${expiration}"
        Date now = new Date()
        claimsSet.setIssueTime(now)
        use(TimeCategory) {
            claimsSet.setExpirationTime(now + expiration.seconds)
        }

        claimsSet.setCustomClaim('roles', details.authorities?.collect { it.authority })
        claimsSet.setCustomClaim('principal', serializedPrincipal)
        //claimsSet.setCustomClaim('username', details.getUsername()) //added customized user name claim
        log.debug "Generated claim set: ${claimsSet.toJSONObject().toString()}"
        return claimsSet
    }

注意:主题中claimsSet.setSubject(details.username)已经包含用户名

于 2015-12-21T09:13:55.153 回答
0

根据文档

如果要在 JSON 响应中呈现附加信息,则必须:

  • 配置一个替代的 userDetailsS​​ervice bean,它检索您想要的附加信息,并将其放入主体对象中。

  • 配置从 restAuthenticationToken.principal 对象读取该信息的备用 accessTokenJsonRenderer。

有关userDetailsS​​erviceaccessTokenJsonRenderer 的附加信息

于 2017-11-08T20:15:04.963 回答