-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Support custom OAuth2AuthenticatedPrincipal
in Jwt-based authentication flow
#17191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.security.authentication.AbstractAuthenticationToken; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal; | ||
import org.springframework.security.oauth2.jwt.Jwt; | ||
import org.springframework.security.oauth2.jwt.JwtClaimNames; | ||
import org.springframework.util.Assert; | ||
|
@@ -30,10 +31,12 @@ | |
* @author Josh Cummings | ||
* @author Evgeniy Cheban | ||
* @author Olivier Antoine | ||
* @author Andrey Litvitski | ||
* @since 5.1 | ||
*/ | ||
public class JwtAuthenticationConverter implements Converter<Jwt, AbstractAuthenticationToken> { | ||
|
||
private Converter<Jwt, OAuth2AuthenticatedPrincipal> jwtPrincipalConverter; | ||
private Converter<Jwt, Collection<GrantedAuthority>> jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter(); | ||
|
||
private String principalClaimName = JwtClaimNames.SUB; | ||
|
@@ -42,8 +45,26 @@ public class JwtAuthenticationConverter implements Converter<Jwt, AbstractAuthen | |
public final AbstractAuthenticationToken convert(Jwt jwt) { | ||
Collection<GrantedAuthority> authorities = this.jwtGrantedAuthoritiesConverter.convert(jwt); | ||
|
||
String principalClaimValue = jwt.getClaimAsString(this.principalClaimName); | ||
return new JwtAuthenticationToken(jwt, authorities, principalClaimValue); | ||
if (this.jwtPrincipalConverter == null) { | ||
String principalClaimValue = jwt.getClaimAsString(this.principalClaimName); | ||
return new JwtAuthenticationToken(jwt, authorities, principalClaimValue); | ||
} else { | ||
OAuth2AuthenticatedPrincipal principal = this.jwtPrincipalConverter.convert(jwt); | ||
authorities.addAll(principal.getAuthorities()); | ||
return new JwtAuthenticationToken(jwt, principal, authorities); | ||
} | ||
} | ||
|
||
/** | ||
* Sets the {@link Converter Converter<Jwt, Collection<OAuth2AuthenticatedPrincipal>>} | ||
* to use. | ||
* @param jwtPrincipalConverter The converter | ||
* @since 6.5.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be |
||
*/ | ||
public void setJwtPrincipalConverter( | ||
Converter<Jwt, OAuth2AuthenticatedPrincipal> jwtPrincipalConverter) { | ||
Assert.notNull(jwtPrincipalConverter, "jwtPrincipalConverter cannot be null"); | ||
this.jwtPrincipalConverter = jwtPrincipalConverter; | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
import org.springframework.security.oauth2.core.OAuth2AccessToken; | ||
import org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal; | ||
import org.springframework.security.oauth2.jwt.Jwt; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* A {@link Converter} that takes a {@link Jwt} and converts it into a | ||
|
@@ -41,6 +42,7 @@ | |
* {@link BearerTokenAuthentication}. | ||
* | ||
* @author Josh Cummings | ||
* @author Andrey Litvitski | ||
* @since 5.2 | ||
*/ | ||
public final class JwtBearerTokenAuthenticationConverter implements Converter<Jwt, AbstractAuthenticationToken> { | ||
|
@@ -58,4 +60,16 @@ public AbstractAuthenticationToken convert(Jwt jwt) { | |
return new BearerTokenAuthentication(principal, accessToken, authorities); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use the principal provided by the principal conversion instead of constructing a new It might be a little simpler at this point to change this class to have its own There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding your own links to converters will indeed be easier, but I have a question. Do we want to create the same There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd recommend that the principal converter in And I'm not sure, but I believe that may require calling the authorities converter twice, once to populate |
||
} | ||
|
||
/** | ||
* Sets the {@link Converter Converter<Jwt, Collection<OAuth2AuthenticatedPrincipal>>} | ||
* to use. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will you please state the default value as well? For example: * <p>By default, constructs a {@link DefaultOAuth2AuthenticatedPrincipal} based on the claims and authorities derived from the {@link Jwt}. |
||
* @param jwtPrincipalConverter The converter | ||
* @since 6.5.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will you please update this to |
||
*/ | ||
public void setJwtPrincipalConverter( | ||
Converter<Jwt, OAuth2AuthenticatedPrincipal> jwtPrincipalConverter) { | ||
Assert.notNull(jwtPrincipalConverter, "jwtPrincipalConverter cannot be null"); | ||
this.jwtAuthenticationConverter.setJwtPrincipalConverter(jwtPrincipalConverter); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the help of a private inner class, I believe we can adapt
Jwt
into anOAuth2AuthenticatedPrincipal
so that this if statement is not necessary. Consider adding a class like this:Then, I believe
jwtPrincipalConverter
can default toJwtAuthenticatedPrincipal::new
andprincipalClaimName
can be removed by havingsetPrincipalClaimName
useJwtAuthenticatedPrincipal
as well.