-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Allow for customization of IssuerResolver #9005
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
Changes from 3 commits
aac9194
f8a3663
3fdb9c2
86c51b2
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 |
|---|---|---|
|
|
@@ -65,7 +65,7 @@ public final class JwtIssuerAuthenticationManagerResolver implements Authenticat | |
|
|
||
| private final AuthenticationManagerResolver<String> issuerAuthenticationManagerResolver; | ||
|
|
||
| private final Converter<HttpServletRequest, String> issuerConverter = new JwtClaimIssuerConverter(); | ||
| private final Converter<HttpServletRequest, String> issuerConverter; | ||
|
|
||
| /** | ||
| * Construct a {@link JwtIssuerAuthenticationManagerResolver} using the provided | ||
|
|
@@ -85,6 +85,27 @@ public JwtIssuerAuthenticationManagerResolver(Collection<String> trustedIssuers) | |
| Assert.notEmpty(trustedIssuers, "trustedIssuers cannot be empty"); | ||
| this.issuerAuthenticationManagerResolver = new TrustedIssuerJwtAuthenticationManagerResolver( | ||
| Collections.unmodifiableCollection(trustedIssuers)::contains); | ||
| this.issuerConverter = new JwtClaimIssuerConverter(); | ||
| } | ||
|
|
||
| /** | ||
| * Construct a {@link JwtIssuerAuthenticationManagerResolver} with a custom | ||
| * {@link JwtAuthenticationConverter} using the provided parameters | ||
| * | ||
| * A custom {@link JwtAuthenticationConverter} allows to use a custom | ||
| * {@link Converter} (much like {@link JwtGrantedAuthoritiesConverter}) to handle an | ||
| * untypical JWT token | ||
| * @param trustedIssuers a list of trusted issuers | ||
| * @param jwtAuthenticationConverter a custom {@link JwtAuthenticationConverter} | ||
| * @since 5.4 | ||
| */ | ||
| public JwtIssuerAuthenticationManagerResolver(Collection<String> trustedIssuers, | ||
| JwtAuthenticationConverter jwtAuthenticationConverter) { | ||
jzheaux marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Assert.notEmpty(trustedIssuers, "trustedIssuers cannot be empty"); | ||
| Assert.notNull(jwtAuthenticationConverter, "jwtAuthenticationConverter cannot be null"); | ||
| this.issuerAuthenticationManagerResolver = new TrustedIssuerJwtAuthenticationManagerResolver( | ||
| Collections.unmodifiableCollection(trustedIssuers)::contains, jwtAuthenticationConverter); | ||
| this.issuerConverter = new JwtClaimIssuerConverter(); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -110,8 +131,41 @@ public JwtIssuerAuthenticationManagerResolver(Collection<String> trustedIssuers) | |
| */ | ||
| public JwtIssuerAuthenticationManagerResolver( | ||
| AuthenticationManagerResolver<String> issuerAuthenticationManagerResolver) { | ||
| this(issuerAuthenticationManagerResolver, new JwtClaimIssuerConverter()); | ||
| } | ||
|
|
||
| /** | ||
| * Construct a {@link JwtIssuerAuthenticationManagerResolver} using the provided | ||
| * parameters | ||
| * | ||
| * Note that the {@link AuthenticationManagerResolver} provided in this constructor | ||
| * will need to verify that the issuer is trusted. This should be done via an | ||
| * allowlist. | ||
| * | ||
| * One way to achieve this is with a {@link Map} where the keys are the known issuers: | ||
| * <pre> | ||
| * Map<String, AuthenticationManager> authenticationManagers = new HashMap<>(); | ||
| * authenticationManagers.put("https://issuerOne.example.org", managerOne); | ||
| * authenticationManagers.put("https://issuerTwo.example.org", managerTwo); | ||
| * JwtAuthenticationManagerResolver resolver = new JwtAuthenticationManagerResolver | ||
| * (authenticationManagers::get); | ||
| * </pre> | ||
| * | ||
| * The keys in the {@link Map} are the allowed issuers. | ||
| * @param issuerAuthenticationManagerResolver a strategy for resolving the | ||
| * {@link AuthenticationManager} by the issuer | ||
| * @param issuerConverter a custom converter to resolve the token A custom converter | ||
| * allows to use a custom {@link BearerTokenResolver} | ||
| * | ||
| * @since 5.4 | ||
| */ | ||
| public JwtIssuerAuthenticationManagerResolver( | ||
| AuthenticationManagerResolver<String> issuerAuthenticationManagerResolver, | ||
| Converter<HttpServletRequest, String> issuerConverter) { | ||
|
Contributor
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. Since we are using a setter for the issuer converter, this constructor is unnecessary. |
||
| Assert.notNull(issuerAuthenticationManagerResolver, "issuerAuthenticationManagerResolver cannot be null"); | ||
| Assert.notNull(issuerConverter, "issuerConverter cannot be null"); | ||
| this.issuerAuthenticationManagerResolver = issuerAuthenticationManagerResolver; | ||
| this.issuerConverter = issuerConverter; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -160,8 +214,16 @@ private static class TrustedIssuerJwtAuthenticationManagerResolver | |
|
|
||
| private final Predicate<String> trustedIssuer; | ||
|
|
||
| private final JwtAuthenticationConverter jwtAuthenticationConverter; | ||
|
Contributor
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. Let's leave these changes regarding |
||
|
|
||
| TrustedIssuerJwtAuthenticationManagerResolver(Predicate<String> trustedIssuer) { | ||
| this(trustedIssuer, null); | ||
| } | ||
|
|
||
| TrustedIssuerJwtAuthenticationManagerResolver(Predicate<String> trustedIssuer, | ||
| JwtAuthenticationConverter jwtAuthenticationConverter) { | ||
| this.trustedIssuer = trustedIssuer; | ||
| this.jwtAuthenticationConverter = jwtAuthenticationConverter; | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -171,7 +233,17 @@ public AuthenticationManager resolve(String issuer) { | |
| (k) -> { | ||
| this.logger.debug("Constructing AuthenticationManager"); | ||
| JwtDecoder jwtDecoder = JwtDecoders.fromIssuerLocation(issuer); | ||
| return new JwtAuthenticationProvider(jwtDecoder)::authenticate; | ||
| if (jwtAuthenticationConverter != null) { | ||
| this.logger.debug(("Using custom JwtAuthenticationConverter")); | ||
| final JwtAuthenticationProvider jwtAuthenticationProvider = new JwtAuthenticationProvider( | ||
| jwtDecoder); | ||
| jwtAuthenticationProvider.setJwtAuthenticationConverter(jwtAuthenticationConverter); | ||
| return jwtAuthenticationProvider::authenticate; | ||
| } | ||
| else { | ||
| this.logger.debug(("Using default JwtAuthenticationConverter")); | ||
| return new JwtAuthenticationProvider(jwtDecoder)::authenticate; | ||
| } | ||
| }); | ||
| this.logger.debug(LogMessage.format("Resolved AuthenticationManager for issuer '%s'", issuer)); | ||
| return authenticationManager; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.