-
Notifications
You must be signed in to change notification settings - Fork 6k
Support customization of JwtAuthenticationConverter #9096
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
Thanks for logging this ticket, @AbstractConcept I agree that customizing how the underlying Instead of taking a So, instead of new JwtIssuerAuthenticationManagerResolver(myTrustedIssuers,
myAuthenticationConverter) you'd do: new JwtIssuerAuthenticationManagerResolver(myTrustedIssuers,
(issuer) -> {
JwtDecoder decoder = JwtDecoders.fromIssuerLocation(issuer);
JwtAuthenticationProvider provider = new JwtAuthenticationProvider(decoder);
provider.setJwtAuthenticationConverter(myAuthenticationConverter);
return provider::authenticate;
}); While slightly more verbose, it allows applications to specify a wider range of customizations. I think it would also be good to adjust a parameter name in another constructor, giving this class four constructors like so: (String... trustedIssuers)
(Collection<String> trustedIssuers)
(Collection<String> trustedIssuers, AuthenticationManagerResolver<String> issuerAuthenticationManagerResolver)
(AuthenticationManagerResolver<String> trustedIssuerAuthenticationManagerResolver) |
#9168 did not take care of this ticket, thanks for double-checking. |
I should be available to complete this one then, but if someone can do it faster, by all means, go ahead. |
This affects a project I'm currently working on. We need both a custom JWTConverter and multi-tenancy. Our workaround is a configuration that looks like this:
Is this issue still going to be adressed? |
Thanks for checking, @fabiangr. I think it's open for someone with time to contribute a PR. @AbstractConcept, is this something you are still able to contribute? |
@jzheaux @fabiangr @AbstractConcept I didn't see any work in progress on this, and I find it useful as well, so I opened a PR. |
…r trusted JWT issuer JwtIssuer(Reactive)AuthenticationManagerResolver did not provide a simple means of customizing, for example, the JWT authentication converter or decoder. This commit introduces a new constructor that accepts a collection of trusted issuers and a strategy for resolving the (Reactive)AuthenticationManager given a trusted issuer, which offers the ability to make the aforementioned customizations. Closes spring-projectsgh-9096
After reviewing this in conjunction with #10002, I don't think we want to add more constructors to Instead, please consider a custom resolver like so: @Component
public class MyAuthenticationManagerResolver implements AuthenticationManagerResolver<String> {
private final Collection<String> validIssuers;
// ...
@Override
@Cacheable(unless="#result==null")
public AuthenticationManager resolve(String issuer) {
if (!this.validIssuers.contains(issuer)) {
return null;
}
JwtAuthenticationProvider provider = new JwtAuthenticationProvider(
JwtDecoders.fromIssuerLocation(issuer));
provider.setAuthenticationConverter(myConverter);
return provider::authenticate;
}
}
// ...
@Bean
JwtIssuerAuthenticationManagerResolver multitenancy(
AuthenticationManagerResolver<String> resolver) {
return new JwtIssuerAuthenticationManagerResolver(resolver);
} Or, with the introduction of @Bean
JwtIssuerAuthenticationManagerResolver byIssuer(MyJwtConverter converter) {
Map<String, AuthenticationManager> managers = new HashMap<>();
for (String issuer : issuers) {
JwtDecoder decoder = new SupplierJwtDecoder(() -> JwtDecoders.fromIssuerLocation(issuer));
JwtAuthenticationProvider provider = new JwtAuthenticationProvider(decoder);
provider.setJwtAuthenticationConverter(converter);
managers.put(issuer, provider::authenticate);
}
return new JwtIssuerAuthenticationManagerResolver(managers::get);
} |
This issue is related to @jzheaux as he requested some of the changes from PR #9005 to be moved to a separate ticket.
On-hold until 9005 is merged.
Expected Behavior
It should be possible to override / customize JwtAuthenticationProvider inside JwtIssuerAuthenticationManagerResolver class, in a multi-tenant environment, so that the end-user can set non-standard behavior that may be desired (for example, custom JWT parsing).
Current Behavior
Customization is not possible at all, end-user is forced to use predefined implementations inside JwtIssuerAuthenticationManagerResolver, and this leads to errors if JWTs contain something uncommon.
Context
Using an external oauth2ResourceServer, in a multi-tenant environment, it should be possible to override / select a custom JwtAuthenticationProvider with a custom JwtAuthenticationConverter, as not all of the JWT tokens are the same, and this leads to errors.
Please also see #9005 and #8535 for more information and the timeline of changes.
The text was updated successfully, but these errors were encountered: