1616
1717package org .springframework .security .oauth2 .server .resource .authentication ;
1818
19+ import java .util .ArrayList ;
1920import java .util .Arrays ;
2021import java .util .Collection ;
21- import java .util .Collections ;
2222import java .util .Map ;
2323import java .util .concurrent .ConcurrentHashMap ;
2424import java .util .function .Predicate ;
5454 * <a href="https://tools.ietf.org/html/rfc6750#section-1.2" target="_blank">Bearer Token</a>.
5555 *
5656 * @author Josh Cummings
57+ * @author Roman Matiushchenko
5758 * @since 5.3
5859 */
5960public final class JwtIssuerReactiveAuthenticationManagerResolver
@@ -79,8 +80,7 @@ public JwtIssuerReactiveAuthenticationManagerResolver(String... trustedIssuers)
7980 public JwtIssuerReactiveAuthenticationManagerResolver (Collection <String > trustedIssuers ) {
8081 Assert .notEmpty (trustedIssuers , "trustedIssuers cannot be empty" );
8182 this .issuerAuthenticationManagerResolver =
82- new TrustedIssuerJwtAuthenticationManagerResolver
83- (Collections .unmodifiableCollection (trustedIssuers )::contains );
83+ new TrustedIssuerJwtAuthenticationManagerResolver (new ArrayList <>(trustedIssuers )::contains );
8484 }
8585
8686 /**
@@ -133,26 +133,26 @@ private static class JwtClaimIssuerConverter
133133
134134 @ Override
135135 public Mono <String > convert (@ NonNull ServerWebExchange exchange ) {
136- return this .converter .convert (exchange )
137- . cast (BearerTokenAuthenticationToken . class )
138- . flatMap ( this :: issuer );
139- }
140-
141- private Mono < String > issuer ( BearerTokenAuthenticationToken token ) {
142- try {
143- String issuer = JWTParser . parse ( token . getToken ()). getJWTClaimsSet (). getIssuer () ;
144- return Mono . justOrEmpty ( issuer ). switchIfEmpty (
145- Mono . error (() -> new InvalidBearerTokenException ( "Missing issuer" )));
146- } catch ( Exception e ) {
147- return Mono . error ( new InvalidBearerTokenException ( e . getMessage ()));
148- }
136+ return this .converter .convert (exchange ). map ( convertedToken -> {
137+ BearerTokenAuthenticationToken token = (BearerTokenAuthenticationToken ) convertedToken ;
138+ try {
139+ String issuer = JWTParser . parse ( token . getToken ()). getJWTClaimsSet (). getIssuer ();
140+ if ( issuer == null ) {
141+ throw new InvalidBearerTokenException ( "Missing issuer" );
142+ } else {
143+ return issuer ;
144+ }
145+ } catch ( Exception e ) {
146+ throw new InvalidBearerTokenException ( e . getMessage (), e );
147+ }
148+ });
149149 }
150150 }
151151
152152 private static class TrustedIssuerJwtAuthenticationManagerResolver
153153 implements ReactiveAuthenticationManagerResolver <String > {
154154
155- private final Map <String , Mono <? extends ReactiveAuthenticationManager >> authenticationManagers =
155+ private final Map <String , Mono <ReactiveAuthenticationManager >> authenticationManagers =
156156 new ConcurrentHashMap <>();
157157 private final Predicate <String > trustedIssuer ;
158158
@@ -162,15 +162,15 @@ private static class TrustedIssuerJwtAuthenticationManagerResolver
162162
163163 @ Override
164164 public Mono <ReactiveAuthenticationManager > resolve (String issuer ) {
165- return Mono . just (issuer )
166- . filter ( this . trustedIssuer )
167- . flatMap ( iss ->
168- this .authenticationManagers .computeIfAbsent (iss , k ->
169- Mono .fromCallable (() -> ReactiveJwtDecoders . fromIssuerLocation ( iss ))
170- . subscribeOn ( Schedulers . boundedElastic ( ))
171- . map ( JwtReactiveAuthenticationManager :: new )
172- . cache ())
173- );
165+ if (! this . trustedIssuer . test (issuer )) {
166+ return Mono . empty ();
167+ }
168+ return this .authenticationManagers .computeIfAbsent (issuer , k ->
169+ Mono .< ReactiveAuthenticationManager > fromCallable (() ->
170+ new JwtReactiveAuthenticationManager ( ReactiveJwtDecoders . fromIssuerLocation ( k ))
171+ )
172+ . subscribeOn ( Schedulers . boundedElastic ())
173+ . cache () );
174174 }
175175 }
176176}
0 commit comments