【问题标题】:Customize response parameters of oauth response自定义 oauth 响应的响应参数
【发布时间】:2021-05-17 20:11:26
【问题描述】:

我目前正在开发一个使用 JDBCTokenStore 的项目。默认返回

{
    "access_token": "<somedata>",
    "token_type": "bearer",
    "expires_in": <time>,
    "scope": "read write"
}

有没有办法自定义这个响应? 我尝试使用 TokenEnhancer,但是这会将信息保存在数据库中,这不是我想要的。

我只想自定义响应并将其发送回最终用户(添加一个额外的参数,如用户状态,它会根据某些条件动态变化)。

{
        "access_token": "<somedata>",
        "token_type": "bearer",
        "expires_in": <time>,
        "scope": "read write"
        "state": <true or false>
    }

我们将不胜感激。

【问题讨论】:

    标签: spring-boot spring-security-oauth2


    【解决方案1】:

    您可以使用自定义 TokenEnhancer 实现自定义令牌响应。

    public class CustomTokenEnhancer implements TokenEnhancer {
    
        private TokenEnhancer delegate;
    
        public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
            OAuth2AccessToken result = delegate.enhance(accessToken, authentication);
            result.getAdditionalInformation().put("custom_response_claim", "value");
            return result;
        }
    } 
    

    OAuth2AccessToken.additionalInformation 映射添加参数将作为令牌响应中的附加参数处理。

    【讨论】:

    • 这确实有效,但它会将附加信息值保存在数据库中。因此,如果值有更新(上例中的“custom_response_claim”),它不会选择它。在令牌过期之前,它会显示相同的旧值。有没有办法强制它选择更新的值??
    猜你喜欢
    • 2015-07-03
    • 1970-01-01
    • 2019-06-19
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多