Skip to content

Commit 8146b1f

Browse files
committed
Deprecate CustomUserTypesOAuth2UserService
Closes gh-8908
1 parent 73e550a commit 8146b1f

File tree

3 files changed

+10
-103
lines changed

3 files changed

+10
-103
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/client/OAuth2LoginConfigurer.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -439,10 +439,13 @@ public UserInfoEndpointConfig oidcUserService(OAuth2UserService<OidcUserRequest,
439439
* Sets a custom {@link OAuth2User} type and associates it to the provided
440440
* client {@link ClientRegistration#getRegistrationId() registration identifier}.
441441
*
442+
* @deprecated See {@link CustomUserTypesOAuth2UserService} for alternative usage.
443+
*
442444
* @param customUserType a custom {@link OAuth2User} type
443445
* @param clientRegistrationId the client registration identifier
444446
* @return the {@link UserInfoEndpointConfig} for further configuration
445447
*/
448+
@Deprecated
446449
public UserInfoEndpointConfig customUserType(Class<? extends OAuth2User> customUserType, String clientRegistrationId) {
447450
Assert.notNull(customUserType, "customUserType cannot be null");
448451
Assert.hasText(clientRegistrationId, "clientRegistrationId cannot be empty");

docs/manual/src/docs/asciidoc/_includes/servlet/oauth2/oauth2-login.adoc

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,6 @@ public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
616616
.userAuthoritiesMapper(this.userAuthoritiesMapper())
617617
.userService(this.oauth2UserService())
618618
.oidcUserService(this.oidcUserService())
619-
.customUserType(GitHubOAuth2User.class, "github")
620619
)
621620
);
622621
}
@@ -651,7 +650,6 @@ class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {
651650
userAuthoritiesMapper = userAuthoritiesMapper()
652651
userService = oauth2UserService()
653652
oidcUserService = oidcUserService()
654-
customUserType(GitHubOAuth2User::class.java, "github")
655653
}
656654
}
657655
}
@@ -875,7 +873,6 @@ return CommonOAuth2Provider.GOOGLE.getBuilder("google")
875873
The UserInfo Endpoint includes a number of configuration options, as described in the following sub-sections:
876874

877875
* <<oauth2login-advanced-map-authorities, Mapping User Authorities>>
878-
* <<oauth2login-advanced-custom-user, Configuring a Custom OAuth2User>>
879876
* <<oauth2login-advanced-oauth2-user-service, OAuth 2.0 UserService>>
880877
* <<oauth2login-advanced-oidc-user-service, OpenID Connect 1.0 UserService>>
881878

@@ -1142,104 +1139,6 @@ class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {
11421139
====
11431140

11441141

1145-
[[oauth2login-advanced-custom-user]]
1146-
===== Configuring a Custom OAuth2User
1147-
1148-
`CustomUserTypesOAuth2UserService` is an implementation of an `OAuth2UserService` that provides support for custom `OAuth2User` types.
1149-
1150-
If the default implementation (`DefaultOAuth2User`) does not suit your needs, you can define your own implementation of `OAuth2User`.
1151-
1152-
The following code demonstrates how you would register a custom `OAuth2User` type for GitHub:
1153-
1154-
[source,java]
1155-
----
1156-
@EnableWebSecurity
1157-
public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {
1158-
1159-
@Override
1160-
protected void configure(HttpSecurity http) throws Exception {
1161-
http
1162-
.oauth2Login(oauth2 -> oauth2
1163-
.userInfoEndpoint(userInfo -> userInfo
1164-
.customUserType(GitHubOAuth2User.class, "github")
1165-
...
1166-
)
1167-
);
1168-
}
1169-
}
1170-
----
1171-
1172-
The following code shows an example of a custom `OAuth2User` type for GitHub:
1173-
1174-
[source,java]
1175-
----
1176-
public class GitHubOAuth2User implements OAuth2User {
1177-
private List<GrantedAuthority> authorities =
1178-
AuthorityUtils.createAuthorityList("ROLE_USER");
1179-
private Map<String, Object> attributes;
1180-
private String id;
1181-
private String name;
1182-
private String login;
1183-
private String email;
1184-
1185-
@Override
1186-
public Collection<? extends GrantedAuthority> getAuthorities() {
1187-
return this.authorities;
1188-
}
1189-
1190-
@Override
1191-
public Map<String, Object> getAttributes() {
1192-
if (this.attributes == null) {
1193-
this.attributes = new HashMap<>();
1194-
this.attributes.put("id", this.getId());
1195-
this.attributes.put("name", this.getName());
1196-
this.attributes.put("login", this.getLogin());
1197-
this.attributes.put("email", this.getEmail());
1198-
}
1199-
return attributes;
1200-
}
1201-
1202-
public String getId() {
1203-
return this.id;
1204-
}
1205-
1206-
public void setId(String id) {
1207-
this.id = id;
1208-
}
1209-
1210-
@Override
1211-
public String getName() {
1212-
return this.name;
1213-
}
1214-
1215-
public void setName(String name) {
1216-
this.name = name;
1217-
}
1218-
1219-
public String getLogin() {
1220-
return this.login;
1221-
}
1222-
1223-
public void setLogin(String login) {
1224-
this.login = login;
1225-
}
1226-
1227-
public String getEmail() {
1228-
return this.email;
1229-
}
1230-
1231-
public void setEmail(String email) {
1232-
this.email = email;
1233-
}
1234-
}
1235-
----
1236-
1237-
[TIP]
1238-
`id`, `name`, `login`, and `email` are attributes returned in GitHub's UserInfo Response.
1239-
For detailed information returned from the UserInfo Endpoint, see the API documentation
1240-
for https://developer.github.com/v3/users/#get-the-authenticated-user["Get the authenticated user"].
1241-
1242-
12431142
[[oauth2login-advanced-oauth2-user-service]]
12441143
===== OAuth 2.0 UserService
12451144

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/userinfo/CustomUserTypesOAuth2UserService.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,13 +40,18 @@
4040
* using a {@code Map} of {@link OAuth2User} type(s) keyed by {@code String},
4141
* which represents the {@link ClientRegistration#getRegistrationId() Registration Id} of the Client.
4242
*
43+
* @deprecated It is recommended to use a delegation-based strategy of an {@link OAuth2UserService} to support custom {@link OAuth2User} types,
44+
* as it provides much greater flexibility compared to this implementation.
45+
* See the <a target="_blank" href="https://docs.spring.io/spring-security/site/docs/current/reference/html5/#oauth2login-advanced-map-authorities-oauth2userservice">reference manual</a> for details on how to implement.
46+
*
4347
* @author Joe Grandja
4448
* @since 5.0
4549
* @see OAuth2UserService
4650
* @see OAuth2UserRequest
4751
* @see OAuth2User
4852
* @see ClientRegistration
4953
*/
54+
@Deprecated
5055
public class CustomUserTypesOAuth2UserService implements OAuth2UserService<OAuth2UserRequest, OAuth2User> {
5156
private static final String INVALID_USER_INFO_RESPONSE_ERROR_CODE = "invalid_user_info_response";
5257

0 commit comments

Comments
 (0)