Class OidcUserService
java.lang.Object
org.springframework.security.oauth2.client.oidc.userinfo.OidcUserService
- All Implemented Interfaces:
OAuth2UserService<OidcUserRequest,
OidcUser>
An implementation of an
OAuth2UserService
that supports OpenID Connect 1.0
Provider's.- Since:
- 5.0
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionReturns the defaultConverter
's used for type conversion of claim values for anOidcUserInfo
.loadUser
(OidcUserRequest userRequest) Returns anOAuth2User
after obtaining the user attributes of the End-User from the UserInfo Endpoint.final void
setAccessibleScopes
(Set<String> accessibleScopes) Deprecated, for removal: This API element is subject to removal in a future version.final void
setClaimTypeConverterFactory
(Function<ClientRegistration, org.springframework.core.convert.converter.Converter<Map<String, Object>, Map<String, Object>>> claimTypeConverterFactory) Sets the factory that provides aConverter
used for type conversion of claim values for anOidcUserInfo
.final void
setOauth2UserService
(OAuth2UserService<OAuth2UserRequest, OAuth2User> oauth2UserService) Sets theOAuth2UserService
used when requesting the user info resource.final void
setOidcUserMapper
(BiFunction<OidcUserRequest, OidcUserInfo, OidcUser> oidcUserMapper) final void
setRetrieveUserInfo
(Predicate<OidcUserRequest> retrieveUserInfo) Sets thePredicate
used to determine if the UserInfo Endpoint should be called to retrieve information about the End-User (Resource Owner).
-
Constructor Details
-
OidcUserService
public OidcUserService()
-
-
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
Map
ofConverter
's keyed byclaim name
- Since:
- 5.2
-
loadUser
Description copied from interface:OAuth2UserService
Returns anOAuth2User
after obtaining the user attributes of the End-User from the UserInfo Endpoint.- Specified by:
loadUser
in interfaceOAuth2UserService<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 final void setOauth2UserService(OAuth2UserService<OAuth2UserRequest, OAuth2User> oauth2UserService) Sets theOAuth2UserService
used when requesting the user info resource.- Parameters:
oauth2UserService
- theOAuth2UserService
used when requesting the user info resource.- Since:
- 5.1
-
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 aConverter
used for type conversion of claim values for anOidcUserInfo
. The default isClaimTypeConverter
for allclients
.- Parameters:
claimTypeConverterFactory
- the factory that provides aConverter
used for type conversion of claim values for a specificclient
- Since:
- 5.2
-
setAccessibleScopes
@Deprecated(since="6.3", forRemoval=true) public final void setAccessibleScopes(Set<String> accessibleScopes) Deprecated, for removal: This API element is subject to removal in a future version.UsesetRetrieveUserInfo(Predicate)
insteadSets the scope(s) that allow access to the user info resource. The default isprofile
,email
,address
andphone
. The scope(s) are checked against the "granted" scope(s) associated to theaccess token
to determine if the user info resource is accessible or not. If there is at least one match, the user info resource will be requested, otherwise it will not.- Parameters:
accessibleScopes
- the scope(s) that allow access to the user info resource- Since:
- 5.2
-
setRetrieveUserInfo
Sets thePredicate
used 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_CODE
- The access token contains one or more scopes allowed to access the UserInfo
Endpoint (
profile
,email
,address
orphone
) or the access token scopes are empty
- 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, OidcUser> oidcUserMapper) Sets theBiFunction
used to map theuser
from theuser request
anduser 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 OidcUserService oidcUserService() { var userService = new OidcUserService(); userService.setOidcUserMapper(oidcUserMapper()); return userService; } private static BiFunction<OidcUserRequest, OidcUserInfo, 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 new DefaultOidcUser( grantedAuthorities, userRequest.getIdToken(), userInfo, userNameAttributeName ); }; }
Note that you can access the
userNameAttributeName
via theClientRegistration
as follows:var userNameAttributeName = userRequest.getClientRegistration() .getProviderDetails() .getUserInfoEndpoint() .getUserNameAttributeName();
By default, a
DefaultOidcUser
is created with authorities mapped as follows:- An
OidcUserAuthority
is created from theOidcIdToken
andOidcUserInfo
with an authority ofOIDC_USER
- Additional
authorities
are mapped from theaccess token scopes
with a prefix ofSCOPE_
- Parameters:
oidcUserMapper
- the function used to map theOidcUser
from theOidcUserRequest
andOidcUserInfo
- Since:
- 6.3
- An
-
setRetrieveUserInfo(Predicate)
instead