Added constructors to support custom principal name

closes #6893
This commit is contained in:
HaydenMeloche 2019-05-20 22:35:43 -04:00 committed by Josh Cummings
parent d660084538
commit f84ab3a255
No known key found for this signature in database
GPG Key ID: 49EF60DD7FF83443
2 changed files with 40 additions and 1 deletions

View File

@ -36,6 +36,8 @@ import org.springframework.security.oauth2.jwt.Jwt;
public class JwtAuthenticationToken extends AbstractOAuth2TokenAuthenticationToken<Jwt> { public class JwtAuthenticationToken extends AbstractOAuth2TokenAuthenticationToken<Jwt> {
private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID; private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
private final String name;
/** /**
* Constructs a {@code JwtAuthenticationToken} using the provided parameters. * Constructs a {@code JwtAuthenticationToken} using the provided parameters.
* *
@ -43,6 +45,7 @@ public class JwtAuthenticationToken extends AbstractOAuth2TokenAuthenticationTok
*/ */
public JwtAuthenticationToken(Jwt jwt) { public JwtAuthenticationToken(Jwt jwt) {
super(jwt); super(jwt);
this.name = jwt.getSubject();
} }
/** /**
@ -54,6 +57,20 @@ public class JwtAuthenticationToken extends AbstractOAuth2TokenAuthenticationTok
public JwtAuthenticationToken(Jwt jwt, Collection<? extends GrantedAuthority> authorities) { public JwtAuthenticationToken(Jwt jwt, Collection<? extends GrantedAuthority> authorities) {
super(jwt, authorities); super(jwt, authorities);
this.setAuthenticated(true); this.setAuthenticated(true);
this.name = jwt.getSubject();
}
/**
* Constructs a {@code JwtAuthenticationToken} using the provided parameters.
*
* @param jwt the JWT
* @param authorities the authorities assigned to the JWT
* @param name the principal name
*/
public JwtAuthenticationToken(Jwt jwt, Collection<? extends GrantedAuthority> authorities, String name) {
super(jwt, authorities);
this.setAuthenticated(true);
this.name = name;
} }
/** /**
@ -69,6 +86,6 @@ public class JwtAuthenticationToken extends AbstractOAuth2TokenAuthenticationTok
*/ */
@Override @Override
public String getName() { public String getName() {
return this.getToken().getSubject(); return this.name;
} }
} }

View File

@ -98,6 +98,28 @@ public class JwtAuthenticationTokenTests {
assertThat(token.isAuthenticated()).isFalse(); assertThat(token.isAuthenticated()).isFalse();
} }
@Test
public void constructorWhenProvidingJwtAndAuthoritiesThenSetsNameCorrectly() {
Map claims = Maps.newHashMap("sub", "Hayden");
Jwt jwt = this.jwt(claims);
JwtAuthenticationToken token = new JwtAuthenticationToken(jwt);
assertThat(token.getName()).isEqualTo("Hayden");
}
@Test
public void constructorWhenUsingAllParametersThenReturnsCorrectName() {
Collection authorities = Arrays.asList(new SimpleGrantedAuthority("test"));
Map claims = Maps.newHashMap("claim", "value");
Jwt jwt = this.jwt(claims);
JwtAuthenticationToken token = new JwtAuthenticationToken(jwt, authorities, "Hayden");
assertThat(token.getName()).isEqualTo("Hayden");
}
private Jwt jwt(Map<String, Object> claims) { private Jwt jwt(Map<String, Object> claims) {
Map<String, Object> headers = new HashMap<>(); Map<String, Object> headers = new HashMap<>();
headers.put("alg", JwsAlgorithms.RS256); headers.put("alg", JwsAlgorithms.RS256);