我已经成功地将Spring Security OAuth2与我的Open ID Connect提供程序(Forgerock OpenAM)集成。我可以看到访问令牌正在被检索。如何访问id_token和refresh_token,这是/token端点响应的一部分?
id_token
refresh_token
/token
cnjp1d6j1#
最后想出了答案和帖子,以防对有同样问题的人有用。在会话通过Spring Security OAuth2身份验证后,有一个Authentication Object设置。它需要转换为OAuth2Authentication的示例。该对象具有令牌。
Authentication
OAuth2Authentication
Authentication auth = SecurityContextHolder.getContext().getAuthentication(); if (auth instanceof OAuth2Authentication) { Object details = auth.getDetails(); OAuth2AccessToken token = oauth2Ctx.getAccessToken(); if (token != null && !token.isExpired()) { // Do Stuff }
zed5wv102#
替代方法的完整示例(使用Sping Boot 并禁用其自动配置的一部分)。application.properties:
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
/** * 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); } }
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); } }
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; } }
kqhtkvqz3#
我知道这是一个旧的线程,但我希望这有助于任何人寻找的回应。使用您的“Authentication authentication”对象:
OAuth2AuthenticationToken oauth2Auth = (OAuth2AuthenticationToken) authentication; String idToken = ((DefaultOidcUser) oauth2Auth.getPrincipal()).getIdToken().getTokenValue();
问候。
3条答案
按热度按时间cnjp1d6j1#
最后想出了答案和帖子,以防对有同样问题的人有用。在会话通过Spring Security OAuth2身份验证后,有一个
Authentication
Object设置。它需要转换为OAuth2Authentication
的示例。该对象具有令牌。zed5wv102#
替代方法的完整示例(使用Sping Boot 并禁用其自动配置的一部分)。
application.properties:
kqhtkvqz3#
我知道这是一个旧的线程,但我希望这有助于任何人寻找的回应。
使用您的“Authentication authentication”对象:
问候。