Skip to content

Commit 464b078

Browse files
committed
Add OAuth2AuthenticatedPrincipal.getNameAttributeKey
1 parent 27294b2 commit 464b078

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/DefaultOAuth2AuthenticatedPrincipal.java

Lines changed: 7 additions & 2 deletions
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-2024 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.
@@ -64,7 +64,7 @@ public DefaultOAuth2AuthenticatedPrincipal(String name, Map<String, Object> attr
6464
this.attributes = Collections.unmodifiableMap(attributes);
6565
this.authorities = (authorities != null) ? Collections.unmodifiableCollection(authorities)
6666
: AuthorityUtils.NO_AUTHORITIES;
67-
this.name = (name != null) ? name : (String) this.attributes.get("sub");
67+
this.name = (name != null) ? name : (String) this.attributes.get(this.getNameAttributeKey());
6868
}
6969

7070
/**
@@ -81,6 +81,11 @@ public Collection<? extends GrantedAuthority> getAuthorities() {
8181
return this.authorities;
8282
}
8383

84+
@Override
85+
public String getNameAttributeKey() {
86+
return "sub";
87+
}
88+
8489
@Override
8590
public String getName() {
8691
return this.name;

oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/OAuth2AuthenticatedPrincipal.java

Lines changed: 8 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-2024 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.
@@ -57,4 +57,11 @@ default <A> A getAttribute(String name) {
5757
*/
5858
Collection<? extends GrantedAuthority> getAuthorities();
5959

60+
/**
61+
* Get the key used to access the user's &quot;name&quot; from {@link #getAttributes()}
62+
* @return the key used to access the user's &quot;name&quot;
63+
* @since 6.4
64+
*/
65+
String getNameAttributeKey();
66+
6067
}

oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/user/DefaultOAuth2User.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ public Map<String, Object> getAttributes() {
9494
return this.attributes;
9595
}
9696

97+
@Override
98+
public String getNameAttributeKey() {
99+
return this.nameAttributeKey;
100+
}
101+
97102
private Set<GrantedAuthority> sortAuthorities(Collection<? extends GrantedAuthority> authorities) {
98103
SortedSet<GrantedAuthority> sortedAuthorities = new TreeSet<>(
99104
Comparator.comparing(GrantedAuthority::getAuthority));

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/introspection/OAuth2IntrospectionAuthenticatedPrincipal.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -80,6 +80,11 @@ public Collection<? extends GrantedAuthority> getAuthorities() {
8080
return this.delegate.getAuthorities();
8181
}
8282

83+
@Override
84+
public String getNameAttributeKey() {
85+
return this.delegate.getNameAttributeKey();
86+
}
87+
8388
@Override
8489
public String getName() {
8590
return this.delegate.getName();

0 commit comments

Comments
 (0)