1

If I understand the oauth2 spec correctly I should be able to add custom content like an user_id to the oauth2 access token. The server can then decode the access-token and add an User Domain Object to the SecurityContextHolder.

(Of course I could add the user_id to every REST API method but this would mean a lot of refactoring)

Right now from reading the manual I think that I need to implement or extend a TokenEnhancer. Since I am using a javascript client maybe I should extend JwtAccessTokenConverter? Am I on the right path here?

4

2 回答 2

1

首先,为什么您需要将 userId 广告到访问令牌,而您可以将数据存储在 Scope 或附加信息中,让我描述一下使用范围:

如果您的客户端需要使用访问令牌获取特定数据,您可以将其作为范围参数发送到 Auth 服务器,然后当将访问令牌重定向到重定向 URL 时,您将获得放入范围参数的数据,这用于存储您的用户的统计信息认证前后。

如果您希望此用户 ID 用于其他用途而不是存储状态,我建议在 Auth 服务器上创建端点以提供用户详细信息并使用带有访问令牌的 OAuth 保护它。

我不建议放置任何数据来生成访问令牌,因为它可能会削弱令牌的复杂性,也不建议在客户端解码您的令牌。

于 2014-09-21T12:01:15.340 回答
0
@Bean
public TokenEnhancer tokenEnhancer() {
    return new TokenEnhancer() {
        @Override
        public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {
            DefaultOAuth2AccessToken newToken = new DefaultOAuth2AccessToken(oAuth2AccessToken);
            Map<String, Object> addInfo = new HashMap<>();
            // add here what u need
            addInfo.put("putName", oAuth2Authentication.getName());
            newToken.setAdditionalInformation(addInfo);
            return newToken;
        }
    };
}

在 AuthorizationServerConfiguration:

@Autowired
private TokenEnhancer tokenEnhancer;
...
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints)
        throws Exception {
    endpoints
            .tokenStore(this.tokenStore)
            .authenticationManager(this.authenticationManager)
            .userDetailsService(this.userDetailsService)
            .tokenEnhancer(this.tokenEnhancer);
}
于 2017-05-14T16:30:56.067 回答