Support customization of JwtAuthenticationProvider and BearerTokenResolver in multi-tenant systems (related to JwtIssuerAuthenticationManagerResolver) #8648
Labels
status: declined
A suggestion or change that we don't feel we should currently apply
type: enhancement
A general enhancement
Expected Behavior
It should be possible to override / customize
BearerTokenResolver
andJwtAuthenticationProvider
insideJwtIssuerAuthenticationManagerResolver
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, same if there's something specific with Bearer Tokens.Context
Please, see also #8535 for the original issue regarding
BearerTokenResolver
, as the scope of it would ideally be included in a PR that could be a result of this issue.Additionally, this could be related to #6778, at least to some extent.
The context explanation will be mostly about
TrustedIssuerJwtAuthenticationManagerResolver
part of the issue, but it's very similar toJwtClaimIssuerConverter
part.Using an external
oauth2ResourceServer
, in a multi-tenant environment, it should be possible to override / select a customJwtAuthenticationProvider
with a customJwtAuthenticationConverter
, as not all of the JWT tokens are the same, and this leads to errors.Currently, according to the official documentation (https://docs.spring.io/spring-security/site/docs/current/reference/html5/#oauth2resourceserver-multitenancy), if there is a need to support multi-tenancy by JWT Claim, one can use
JwtIssuerAuthenticationManagerResolver
that has to be configured by a class extendingWebSecurityConfigurerAdapter
, however, it comes with an overridenpublic AuthenticationManager resolve(String issuer)
method in theTrustedIssuerJwtAuthenticationManagerResolver
static class, that contains:JwtAuthenticationProvider
,JwtAuthenticationConverter
,JwtGrantedAuthoritiesConverter
, the root of my problems.Normally, the issue could be solved by configuring a
Customizer<JwtConfigurer>
within theHttpSecurity
DSL, butOAuth2ResourceServerConfigurer
runs thevalidateConfiguration()
method that throws anIllegalStateException
because, quote:That's of course understandable, however, there should be a way to configure your own Customizer/JwtGrantedAuthoritiesConverter.
Currently, I have solved my own issue by using my own implementation of a
JwtIssuerAuthenticationManagerResolver
, that I have used instead of the 'official' one, that basically creates a newJwtAuthenticationConverter
and sets my customJwtGrantedAuthoritiesConverter
, but I am willing to work on a PR, to provide a more "official" solution.Some additional information would be great though: could the solution be based on allowing the end-user to provide his own
BearerTokenResolver
/JwtAuthenticationProvider
, that will take priority over the default one?The text was updated successfully, but these errors were encountered: