diff --git a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/DefaultOAuth2AuthenticatedPrincipal.java b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/DefaultOAuth2AuthenticatedPrincipal.java index 75ae44a6eb5..9223935180d 100644 --- a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/DefaultOAuth2AuthenticatedPrincipal.java +++ b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/DefaultOAuth2AuthenticatedPrincipal.java @@ -64,7 +64,7 @@ public DefaultOAuth2AuthenticatedPrincipal(String name, Map attr this.attributes = Collections.unmodifiableMap(attributes); this.authorities = (authorities != null) ? Collections.unmodifiableCollection(authorities) : AuthorityUtils.NO_AUTHORITIES; - this.name = (name != null) ? name : (String) this.attributes.get("sub"); + this.name = (name != null) ? name : (String) this.attributes.get(this.getNameAttributeKey()); } /** diff --git a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/OAuth2AuthenticatedPrincipal.java b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/OAuth2AuthenticatedPrincipal.java index 160c68233be..6554deea606 100644 --- a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/OAuth2AuthenticatedPrincipal.java +++ b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/OAuth2AuthenticatedPrincipal.java @@ -64,9 +64,4 @@ default A getAttribute(String name) { */ String getNameAttributeKey(); - @Override - default String getName() { - return getAttribute(getNameAttributeKey()).toString(); - } - } diff --git a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/user/DefaultOAuth2User.java b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/user/DefaultOAuth2User.java index 10caa4b2edb..433267a708c 100644 --- a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/user/DefaultOAuth2User.java +++ b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/user/DefaultOAuth2User.java @@ -79,6 +79,11 @@ public DefaultOAuth2User(Collection authorities, Map this.nameAttributeKey = nameAttributeKey; } + @Override + public String getName() { + return this.getAttribute(this.nameAttributeKey).toString(); + } + @Override public Collection getAuthorities() { return this.authorities;