Skip to content

Commit f84ab3a

Browse files
HaydenMelochejzheaux
authored andcommitted
Added constructors to support custom principal name
closes #6893
1 parent d660084 commit f84ab3a

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/JwtAuthenticationToken.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,16 @@
3636
public class JwtAuthenticationToken extends AbstractOAuth2TokenAuthenticationToken<Jwt> {
3737
private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
3838

39+
private final String name;
40+
3941
/**
4042
* Constructs a {@code JwtAuthenticationToken} using the provided parameters.
4143
*
4244
* @param jwt the JWT
4345
*/
4446
public JwtAuthenticationToken(Jwt jwt) {
4547
super(jwt);
48+
this.name = jwt.getSubject();
4649
}
4750

4851
/**
@@ -54,6 +57,20 @@ public JwtAuthenticationToken(Jwt jwt) {
5457
public JwtAuthenticationToken(Jwt jwt, Collection<? extends GrantedAuthority> authorities) {
5558
super(jwt, authorities);
5659
this.setAuthenticated(true);
60+
this.name = jwt.getSubject();
61+
}
62+
63+
/**
64+
* Constructs a {@code JwtAuthenticationToken} using the provided parameters.
65+
*
66+
* @param jwt the JWT
67+
* @param authorities the authorities assigned to the JWT
68+
* @param name the principal name
69+
*/
70+
public JwtAuthenticationToken(Jwt jwt, Collection<? extends GrantedAuthority> authorities, String name) {
71+
super(jwt, authorities);
72+
this.setAuthenticated(true);
73+
this.name = name;
5774
}
5875

5976
/**
@@ -69,6 +86,6 @@ public Map<String, Object> getTokenAttributes() {
6986
*/
7087
@Override
7188
public String getName() {
72-
return this.getToken().getSubject();
89+
return this.name;
7390
}
7491
}

oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/authentication/JwtAuthenticationTokenTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,28 @@ public void constructorWhenUsingOnlyJwtThenConstructedCorrectly() {
9898
assertThat(token.isAuthenticated()).isFalse();
9999
}
100100

101+
@Test
102+
public void constructorWhenProvidingJwtAndAuthoritiesThenSetsNameCorrectly() {
103+
Map claims = Maps.newHashMap("sub", "Hayden");
104+
Jwt jwt = this.jwt(claims);
105+
106+
JwtAuthenticationToken token = new JwtAuthenticationToken(jwt);
107+
108+
assertThat(token.getName()).isEqualTo("Hayden");
109+
}
110+
111+
@Test
112+
public void constructorWhenUsingAllParametersThenReturnsCorrectName() {
113+
114+
Collection authorities = Arrays.asList(new SimpleGrantedAuthority("test"));
115+
Map claims = Maps.newHashMap("claim", "value");
116+
Jwt jwt = this.jwt(claims);
117+
118+
JwtAuthenticationToken token = new JwtAuthenticationToken(jwt, authorities, "Hayden");
119+
120+
assertThat(token.getName()).isEqualTo("Hayden");
121+
}
122+
101123
private Jwt jwt(Map<String, Object> claims) {
102124
Map<String, Object> headers = new HashMap<>();
103125
headers.put("alg", JwsAlgorithms.RS256);

0 commit comments

Comments
 (0)