16
16
17
17
package org .springframework .security .oauth2 .server .resource .authentication ;
18
18
19
+ import java .util .ArrayList ;
19
20
import java .util .Arrays ;
20
21
import java .util .Collection ;
21
- import java .util .Collections ;
22
22
import java .util .Map ;
23
23
import java .util .concurrent .ConcurrentHashMap ;
24
24
import java .util .function .Predicate ;
54
54
* <a href="https://tools.ietf.org/html/rfc6750#section-1.2" target="_blank">Bearer Token</a>.
55
55
*
56
56
* @author Josh Cummings
57
+ * @author Roman Matiushchenko
57
58
* @since 5.3
58
59
*/
59
60
public final class JwtIssuerReactiveAuthenticationManagerResolver
@@ -79,8 +80,7 @@ public JwtIssuerReactiveAuthenticationManagerResolver(String... trustedIssuers)
79
80
public JwtIssuerReactiveAuthenticationManagerResolver (Collection <String > trustedIssuers ) {
80
81
Assert .notEmpty (trustedIssuers , "trustedIssuers cannot be empty" );
81
82
this .issuerAuthenticationManagerResolver =
82
- new TrustedIssuerJwtAuthenticationManagerResolver
83
- (Collections .unmodifiableCollection (trustedIssuers )::contains );
83
+ new TrustedIssuerJwtAuthenticationManagerResolver (new ArrayList <>(trustedIssuers )::contains );
84
84
}
85
85
86
86
/**
@@ -133,26 +133,26 @@ private static class JwtClaimIssuerConverter
133
133
134
134
@ Override
135
135
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
+ });
149
149
}
150
150
}
151
151
152
152
private static class TrustedIssuerJwtAuthenticationManagerResolver
153
153
implements ReactiveAuthenticationManagerResolver <String > {
154
154
155
- private final Map <String , Mono <? extends ReactiveAuthenticationManager >> authenticationManagers =
155
+ private final Map <String , Mono <ReactiveAuthenticationManager >> authenticationManagers =
156
156
new ConcurrentHashMap <>();
157
157
private final Predicate <String > trustedIssuer ;
158
158
@@ -162,15 +162,15 @@ private static class TrustedIssuerJwtAuthenticationManagerResolver
162
162
163
163
@ Override
164
164
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 () );
174
174
}
175
175
}
176
176
}
0 commit comments