首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当使用带有OpenID连接提供程序的spring-security-oauth2客户端时,如何访问"id_token“和"refresh_token”?

当使用带有OpenID连接提供程序的spring-security-oauth2客户端时,如何访问"id_token“和"refresh_token”?
EN

Stack Overflow用户
提问于 2017-03-24 23:40:56
回答 2查看 3.6K关注 0票数 3

我已经成功地将Spring Security OAuth2与我的Open ID Connect提供程序(Forgerock OpenAM)集成在一起。我可以看到正在检索访问令牌。如何访问id_tokenrefresh_token,它们是/token端点响应的一部分?

EN

回答 2

Stack Overflow用户

发布于 2018-04-13 21:27:53

终于想出了答案和帖子,以防对有同样问题的人有用。在session被Spring Security OAuth2验证之后,就有了一个Authentication对象设置。它需要强制转换为OAuth2Authentication的一个实例。该对象具有令牌。

代码语言:javascript
复制
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth instanceof OAuth2Authentication) {
        Object details = auth.getDetails();
        OAuth2AccessToken token = oauth2Ctx.getAccessToken();

        if (token != null && !token.isExpired()) {
            // Do Stuff
        }
票数 0
EN

Stack Overflow用户

发布于 2019-07-07 05:21:57

另一种方法的完整示例(使用Spring Boot并禁用部分自动配置)。

application.properties:

代码语言:javascript
复制
security.oauth2.client.client-id=client-id
security.oauth2.client.client-secret=client-secret
security.oauth2.client.access-token-uri=http://my-oidc-provider/auth/oauth2/token
security.oauth2.client.user-authorization-uri=http://my-oidc-provider/auth/oauth2/authorize
security.oauth2.resource.token-info-uri=http://my-oidc-provider/auth/oauth2/check_token
security.oauth2.client.scope=openid,email,profile
security.oauth2.resource.jwk.key-set-uri=http://my-oidc-provider/auth/oidc/jwks
代码语言:javascript
复制
/**
 * Extending the AuthorizationServerEndpointsConfiguration disables the Spring
 * Boot ResourceServerTokenServicesConfiguration.
 */
@Configuration
@EnableOAuth2Sso
public class OAuth2Config extends AuthorizationServerEndpointsConfiguration {

    @Value("${security.oauth2.resource.jwk.key-set-uri}")
    private String keySetUri;

    @Value("${security.oauth2.resource.token-info-uri}")
    private String checkTokenEndpointUrl;

    @Value("${security.oauth2.client.client-id}")
    private String clientId;

    @Value("${security.oauth2.client.client-secret}")
    private String clientSecret;

    @Bean
    public RemoteTokenServices resourceServerTokenServices() {
        RemoteTokenServices tokenService = new RemoteTokenServices();

        DefaultAccessTokenConverter accessTokenConverter = new DefaultAccessTokenConverter();
        accessTokenConverter.setUserTokenConverter(new CustomIdTokenConverter(keySetUri));
        tokenService.setAccessTokenConverter(accessTokenConverter);

        tokenService.setCheckTokenEndpointUrl(checkTokenEndpointUrl);
        tokenService.setClientId(clientId);
        tokenService.setClientSecret(clientSecret);

        return tokenService;
    }

    @Bean
    public ClientDetailsService clientDetailsService() {
        return new InMemoryClientDetailsService();
    }

    @Bean
    public UserInfoRestTemplateFactory userInfoRestTemplateFactory(
            ObjectProvider<List<UserInfoRestTemplateCustomizer>> customizers,
            ObjectProvider<OAuth2ProtectedResourceDetails> details,
            ObjectProvider<OAuth2ClientContext> oauth2ClientContext) {
        return new DefaultUserInfoRestTemplateFactory(customizers, details,
                oauth2ClientContext);
    }
}
代码语言:javascript
复制
public class CustomIdTokenConverter extends DefaultUserAuthenticationConverter {

    private final JwkTokenStore jwkTokenStore;

    public CustomIdTokenConverter(String keySetUri) {
        this.jwkTokenStore = new JwkTokenStore(keySetUri);
    }

    @Override
    public Authentication extractAuthentication(Map<String, ?> map) {

        String idToken = (String) map.get("id_token");

        OAuth2AccessToken token = jwkTokenStore.readAccessToken(idToken);

        Map<String, Object> claims = token.getAdditionalInformation();
        OAuth2RefreshToken refreshToken = token.getRefreshToken();

        String principal = (String) claims.get("sub");

        List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("ROLE_USER");

        return new CustomAuthenticationData(principal, claims, authorities);
    }
}
代码语言:javascript
复制
public class CustomAuthenticationData extends UsernamePasswordAuthenticationToken {

    private final Map<String, Object> attributes;

    public CustomAuthenticationData(String username, Map<String, Object> attributes, Collection<? extends GrantedAuthority> authorities) {
        super(username, "N/A", authorities);
        this.attributes = attributes;
    }

    public Map<String, Object> getAttributes() {
        return attributes;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43003705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档