Added setPrincipalClaimName to JwtAuthenticationConverter

Fixes gh-8186
This commit is contained in:
Evgeniy Cheban 2020-04-03 02:59:13 +03:00 committed by Josh Cummings
parent 420737fa88
commit 25fb1f417d
2 changed files with 54 additions and 1 deletions

View File

@ -22,21 +22,30 @@ import org.springframework.core.convert.converter.Converter;
import org.springframework.security.authentication.AbstractAuthenticationToken; import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.jwt.Jwt; import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.jwt.JwtClaimNames;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
* @author Rob Winch * @author Rob Winch
* @author Josh Cummings * @author Josh Cummings
* @author Evgeniy Cheban
* @since 5.1 * @since 5.1
*/ */
public class JwtAuthenticationConverter implements Converter<Jwt, AbstractAuthenticationToken> { public class JwtAuthenticationConverter implements Converter<Jwt, AbstractAuthenticationToken> {
private Converter<Jwt, Collection<GrantedAuthority>> jwtGrantedAuthoritiesConverter private Converter<Jwt, Collection<GrantedAuthority>> jwtGrantedAuthoritiesConverter
= new JwtGrantedAuthoritiesConverter(); = new JwtGrantedAuthoritiesConverter();
private String principalClaimName;
@Override @Override
public final AbstractAuthenticationToken convert(Jwt jwt) { public final AbstractAuthenticationToken convert(Jwt jwt) {
Collection<GrantedAuthority> authorities = extractAuthorities(jwt); Collection<GrantedAuthority> authorities = extractAuthorities(jwt);
return new JwtAuthenticationToken(jwt, authorities); if (this.principalClaimName == null) {
return new JwtAuthenticationToken(jwt, authorities);
}
String name = jwt.getClaim(this.principalClaimName);
return new JwtAuthenticationToken(jwt, authorities, name);
} }
/** /**
@ -65,4 +74,16 @@ public class JwtAuthenticationConverter implements Converter<Jwt, AbstractAuthen
Assert.notNull(jwtGrantedAuthoritiesConverter, "jwtGrantedAuthoritiesConverter cannot be null"); Assert.notNull(jwtGrantedAuthoritiesConverter, "jwtGrantedAuthoritiesConverter cannot be null");
this.jwtGrantedAuthoritiesConverter = jwtGrantedAuthoritiesConverter; this.jwtGrantedAuthoritiesConverter = jwtGrantedAuthoritiesConverter;
} }
/**
* Sets the principal claim name.
* Defaults to {@link JwtClaimNames#SUB}.
*
* @param principalClaimName The principal claim name
* @since 5.4
*/
public void setPrincipalClaimName(String principalClaimName) {
Assert.hasText(principalClaimName, "principalClaimName cannot be empty");
this.principalClaimName = principalClaimName;
}
} }

View File

@ -35,6 +35,7 @@ import static org.springframework.security.oauth2.jwt.TestJwts.jwt;
* Tests for {@link JwtAuthenticationConverter} * Tests for {@link JwtAuthenticationConverter}
* *
* @author Josh Cummings * @author Josh Cummings
* @author Evgeniy Cheban
*/ */
public class JwtAuthenticationConverterTests { public class JwtAuthenticationConverterTests {
JwtAuthenticationConverter jwtAuthenticationConverter = new JwtAuthenticationConverter(); JwtAuthenticationConverter jwtAuthenticationConverter = new JwtAuthenticationConverter();
@ -73,4 +74,35 @@ public class JwtAuthenticationConverterTests {
assertThat(authorities).containsExactly( assertThat(authorities).containsExactly(
new SimpleGrantedAuthority("blah")); new SimpleGrantedAuthority("blah"));
} }
@Test
public void whenSettingNullPrincipalClaimName() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.jwtAuthenticationConverter.setPrincipalClaimName(null))
.withMessage("principalClaimName cannot be empty");
}
@Test
public void whenSettingEmptyPrincipalClaimName() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.jwtAuthenticationConverter.setPrincipalClaimName(""))
.withMessage("principalClaimName cannot be empty");
}
@Test
public void whenSettingBlankPrincipalClaimName() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.jwtAuthenticationConverter.setPrincipalClaimName(" "))
.withMessage("principalClaimName cannot be empty");
}
@Test
public void convertWhenPrincipalClaimNameSet() {
this.jwtAuthenticationConverter.setPrincipalClaimName("user_id");
Jwt jwt = jwt().claim("user_id", "100").build();
AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt);
assertThat(authentication.getName()).isEqualTo("100");
}
} }