Skip to content

Commit a92f1d7

Browse files
committed
polish gh-7996
Make defensive collection copy as Collections.unmodifiableCollection does not protect from the source collection direct modification. Use Mono#map instead of Mono#flatMap as it allocates less. Use less operators to reduce allocations. Use lambda parameter instead of outer method parameter in authenticationManagers#computeIfAbsent() to make it non capturing so it could be cached by JVM. Propagate cause for InvalidBearerTokenException.
1 parent 18f62d7 commit a92f1d7

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/JwtIssuerReactiveAuthenticationManagerResolver.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
package org.springframework.security.oauth2.server.resource.authentication;
1818

19+
import java.util.ArrayList;
1920
import java.util.Arrays;
2021
import java.util.Collection;
21-
import java.util.Collections;
2222
import java.util.Map;
2323
import java.util.concurrent.ConcurrentHashMap;
2424
import java.util.function.Predicate;
@@ -54,6 +54,7 @@
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
*/
5960
public 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

Comments
 (0)