@ -22,23 +22,32 @@ import org.springframework.core.convert.converter.Converter;
@@ -22,23 +22,32 @@ import org.springframework.core.convert.converter.Converter;
import org.springframework.security.authentication.AbstractAuthenticationToken ;
import org.springframework.security.core.GrantedAuthority ;
import org.springframework.security.oauth2.jwt.Jwt ;
import org.springframework.security.oauth2.jwt.JwtClaimNames ;
import org.springframework.util.Assert ;
/ * *
* @author Rob Winch
* @author Josh Cummings
* @author Evgeniy Cheban
* @since 5 . 1
* /
public class JwtAuthenticationConverter implements Converter < Jwt , AbstractAuthenticationToken > {
private Converter < Jwt , Collection < GrantedAuthority > > jwtGrantedAuthoritiesConverter
= new JwtGrantedAuthoritiesConverter ( ) ;
private String principalClaimName ;
@Override
public final AbstractAuthenticationToken convert ( Jwt jwt ) {
Collection < GrantedAuthority > authorities = extractAuthorities ( jwt ) ;
if ( this . principalClaimName = = null ) {
return new JwtAuthenticationToken ( jwt , authorities ) ;
}
String name = jwt . getClaim ( this . principalClaimName ) ;
return new JwtAuthenticationToken ( jwt , authorities , name ) ;
}
/ * *
* Extracts the { @link GrantedAuthority } s from scope attributes typically found in a { @link Jwt }
*
@ -65,4 +74,16 @@ public class JwtAuthenticationConverter implements Converter<Jwt, AbstractAuthen
@@ -65,4 +74,16 @@ public class JwtAuthenticationConverter implements Converter<Jwt, AbstractAuthen
Assert . notNull ( jwtGrantedAuthoritiesConverter , "jwtGrantedAuthoritiesConverter cannot be null" ) ;
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 ;
}
}