Class OidcReactiveOAuth2UserService
java.lang.Object
org.springframework.security.oauth2.client.oidc.userinfo.OidcReactiveOAuth2UserService
- All Implemented Interfaces:
ReactiveOAuth2UserService<OidcUserRequest,OidcUser>
public class OidcReactiveOAuth2UserService
extends Object
implements ReactiveOAuth2UserService<OidcUserRequest,OidcUser>
An implementation of an
ReactiveOAuth2UserService that supports OpenID Connect
1.0 Provider's.- Since:
- 5.1
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns the defaultConverter's used for type conversion of claim values for anOidcUserInfo.reactor.core.publisher.Mono<OidcUser>loadUser(OidcUserRequest userRequest) Returns anOAuth2Userafter obtaining the user attributes of the End-User from the UserInfo Endpoint.final voidsetClaimTypeConverterFactory(Function<ClientRegistration, org.springframework.core.convert.converter.Converter<Map<String, Object>, Map<String, Object>>> claimTypeConverterFactory) Sets the factory that provides aConverterused for type conversion of claim values for anOidcUserInfo.voidsetOauth2UserService(ReactiveOAuth2UserService<OAuth2UserRequest, OAuth2User> oauth2UserService) final voidsetOidcUserMapper(BiFunction<OidcUserRequest, OidcUserInfo, reactor.core.publisher.Mono<OidcUser>> oidcUserMapper) final voidsetRetrieveUserInfo(Predicate<OidcUserRequest> retrieveUserInfo) Sets thePredicateused to determine if the UserInfo Endpoint should be called to retrieve information about the End-User (Resource Owner).
-
Constructor Details
-
OidcReactiveOAuth2UserService
public OidcReactiveOAuth2UserService()
-
-
Method Details
-
createDefaultClaimTypeConverters
public static Map<String,org.springframework.core.convert.converter.Converter<Object, createDefaultClaimTypeConverters()?>> Returns the defaultConverter's used for type conversion of claim values for anOidcUserInfo.- Returns:
- a
MapofConverter's keyed byclaim name - Since:
- 5.2
-
loadUser
public reactor.core.publisher.Mono<OidcUser> loadUser(OidcUserRequest userRequest) throws OAuth2AuthenticationException Description copied from interface:ReactiveOAuth2UserServiceReturns anOAuth2Userafter obtaining the user attributes of the End-User from the UserInfo Endpoint.- Specified by:
loadUserin interfaceReactiveOAuth2UserService<OidcUserRequest,OidcUser> - Parameters:
userRequest- the user request- Returns:
- an
OAuth2User - Throws:
OAuth2AuthenticationException- if an error occurs while attempting to obtain the user attributes from the UserInfo Endpoint
-
setOauth2UserService
public void setOauth2UserService(ReactiveOAuth2UserService<OAuth2UserRequest, OAuth2User> oauth2UserService) -
setClaimTypeConverterFactory
public final void setClaimTypeConverterFactory(Function<ClientRegistration, org.springframework.core.convert.converter.Converter<Map<String, Object>, Map<String, Object>>> claimTypeConverterFactory) Sets the factory that provides aConverterused for type conversion of claim values for anOidcUserInfo. The default isClaimTypeConverterfor allclients.- Parameters:
claimTypeConverterFactory- the factory that provides aConverterused for type conversion of claim values for a specificclient- Since:
- 5.2
-
setRetrieveUserInfo
Sets thePredicateused to determine if the UserInfo Endpoint should be called to retrieve information about the End-User (Resource Owner).By default, the UserInfo Endpoint is called if all of the following are true:
- The user info endpoint is defined on the ClientRegistration
- The Client Registration uses the
AuthorizationGrantType.AUTHORIZATION_CODEand scopes in the access token are defined in theClientRegistration
- Parameters:
retrieveUserInfo- the function used to determine if the UserInfo Endpoint should be called- Since:
- 6.3
-
setOidcUserMapper
public final void setOidcUserMapper(BiFunction<OidcUserRequest, OidcUserInfo, reactor.core.publisher.Mono<OidcUser>> oidcUserMapper) Sets theBiFunctionused to map theuserfrom theuser requestanduser info.This is useful when you need to map the user or authorities from the access token itself. For example, when the authorization server provides authorization information in the access token payload you can do the following:
@Bean public OidcReactiveOAuth2UserService oidcUserService() { var userService = new OidcReactiveOAuth2UserService(); userService.setOidcUserMapper(oidcUserMapper()); return userService; } private static BiFunction<OidcUserRequest, OidcUserInfo, Mono<OidcUser>> oidcUserMapper() { return (userRequest, userInfo) -> { var accessToken = userRequest.getAccessToken(); var grantedAuthorities = new HashSet<GrantedAuthority>(); // TODO: Map authorities from the access token var userNameAttributeName = "preferred_username"; return Mono.just(new DefaultOidcUser( grantedAuthorities, userRequest.getIdToken(), userInfo, userNameAttributeName )); }; }Note that you can access the
userNameAttributeNamevia theClientRegistrationas follows:var userNameAttributeName = userRequest.getClientRegistration() .getProviderDetails() .getUserInfoEndpoint() .getUserNameAttributeName();By default, a
DefaultOidcUseris created with authorities mapped as follows:- An
OidcUserAuthorityis created from theOidcIdTokenandOidcUserInfowith an authority ofOIDC_USER - Additional
authoritiesare mapped from theaccess token scopeswith a prefix ofSCOPE_
- Parameters:
oidcUserMapper- the function used to map theOidcUserfrom theOidcUserRequestandOidcUserInfo- Since:
- 6.3
- An
-