gpt4 book ai didi

spring - 在身份验证回复中添加其他信息

转载 作者:行者123 更新时间:2023-12-02 14:29:05 25 4
gpt4 key购买 nike

使用Grails 3.3.5和Spring-security-rest 2.0.0-RC1在REST API应用程序上工作。

当我使用/ api / login登录时,得到的结果是正确的:

{
"username": "name.surname@acme.com",
"roles": [
"ROLE_USER"
],
"access_token": "qgsbpk05jfrbf33xx08m8r4govkg53d1"
}

我想在响应中添加其他信息,例如用户的姓名和姓氏。谢谢

更新
感谢Evgeny向我指出了正确的方向,并阅读了手册,我还实现了UserDetailsS​​ervice。
从手册中:

If you want to render additional information in your JSON response, you have to:

Configure an alternative userDetailsService bean that retrieves the additional information you want, and put it in a principal object.

Configure an alternative accessTokenJsonRenderer that reads that information from the restAuthenticationToken.principal object.

最佳答案

您可以覆盖 accessTokenJsonRenderer bean

  • 创建类:

  • class MyRestAuthTokenJsonRenderer implements AccessTokenJsonRenderer {
    @Override
    String generateJson(AccessToken accessToken){
    // create response, see DefaultAccessTokenJsonRenderer.groovy from https://github.com/alvarosanchez/grails-spring-security-rest
    return your_formatted_json_response
    }
    }

  • 覆盖resources.groovy中的bean

  • beans = {
    accessTokenJsonRenderer(MyRestAuthTokenJsonRenderer)
    }

    关于spring - 在身份验证回复中添加其他信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50445887/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com