-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Allow for multiple Jwt to GrantedAuthorizies converters #7596
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
Comments
To confirm that resource server only produces SCOPE_<scope> authorities by default. Issue gh-7596
Thanks for your interest in making Spring Security better, @rolaca11. It seems to me that supporting the JwtGrantedAuthoritiesConverter authorities = new JwtGrantedAuthoritiesConverter();
authorities.setAuthoritiesClaimName("authorities");
http
.oauth2ResourceServer()
.jwt()
.jwtAuthenticationConverter(jwt ->
new JwtAuthenticationToken(jwt, authorities.convert(jwt))); So, I'm not really seeing the benefit of adding complexity to the DSL. There may be value in a Also, I'm a little confused by this statement:
Spring Security's Resource Server support only adds |
Hi,
Yes and originally partly no. Partly no, because I didn't realize you can set a Another concern is that no matter what, the prefix extracted by an instance of
Of course, the |
That makes sense, yes. So then you could do: JwtGrantedAuthoritiesConverter authorities = new JwtGrantedAuthoritiesConverter();
authorities.setAuthorityPrefix("ROLE_");
authorities.setAuthoritiesClaimName("authorities");
// ... etc.
Okay, great. Yes, I think there's value in making this a bit simpler, I think in the same way that it's done with Would you be interested in changing your PR to only introduce At that point, you could do: JwtGrantedAuthoritiesConverter composite =
new DelegatingJwtGrantedAuthoritiesConverter(authorities(), scope());
http
.oauth2ResourceServer()
.jwt()
.jwtAuthoritiesConverter(jwt -> new JwtAuthenticationToken(jwt, composite.convert(jwt)));
// ...
JwtGrantedAuthoritiesConverter authorities() {
JwtGrantedAuthoritiesConverter authorities = new JwtGrantedAuthoritiesConverter();
authorities.setAuthorityPrefix("ROLE_");
authorities.setAuthoritiesClaimName("authorities");
return authorities;
}
JwtGrantedAuthoritiesConverter scope() {
return new JwtGrantedAuthoritiesConverter();
}
It sounds like you might be mixing Spring Security's OAuth 2.0 Client and Resource Server support together in your application. |
Of course, however I'll need some time with it, because I'll be busy with other stuff this week |
JwtGrantedAuthoritiesConverter composite = Please any information on the status of this addition DelegatingJwtGrantedAuthoritiesConverter ? I need to implement something similar that allows to extract my granted authorities from both the scope and authorities. |
Thanks for the ping, @NeluAkejelu. My apologies as this one fell off my radar. Please see the related PR to see if it suits your needs. |
@jzheaux Thanks. I'll take a look. |
- Adjusted internal logic to follow DelegatingOAuth2TokenValidator - Changed JavaDoc to align more closely with JwtGrantedAuthoritiesConverter - Polished test names to follow Spring Security naming convention - Updated test class name to follow Spring Security naming convention - Polished tests to use TestJwts - Added tests to address additional use cases Closes gh-7596
It is quite hard to have multiple converters extract granted authorities from JWT tokens.
In the case of an authorization server created with the latest version of spring-security supporting one, the Jwt tokens generated by the auth server contains a claim named
authorities
, which is an array of strings based on what authorities a user has.The new version of OAuth Resource server ignores this claim and the only granted authorities of a user are
ROLE_USER
andSCOPE_<scope>
which in my opinion are inadequate.One could write one's own converter, but to use it, a new instance of
JwtAuthenticationConverter
has to be created. Also, this solution means that the previously existingROLE_USER
andSCOPE_<scope>
would not be available without code duplicating or having to deal with aSCOPE_
prefix before all roles, which is not desirable.The solution could be an implementation of
JwtAuthenticationConverter
which holds aDelegatingJwtGrantedAuthoritiesConverter
able to iterate over multiple JwtGrantedAuthoritiesConverters, merging the results of one another.This solution could be wired into the
JwtConfigurer
as well, allowing the developer to add multiple converters without the need to replace default instances.The text was updated successfully, but these errors were encountered: