Skip to content

Commit f89a34c

Browse files
committed
Don't Cache ReactiveJwtDecoders Errors
Closes gh-10444
1 parent 89db1c3 commit f89a34c

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

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

19+
import java.time.Duration;
1920
import java.util.ArrayList;
2021
import java.util.Arrays;
2122
import java.util.Collection;
@@ -174,7 +175,7 @@ public Mono<ReactiveAuthenticationManager> resolve(String issuer) {
174175
return this.authenticationManagers.computeIfAbsent(issuer,
175176
(k) -> Mono.<ReactiveAuthenticationManager>fromCallable(() -> new JwtReactiveAuthenticationManager(ReactiveJwtDecoders.fromIssuerLocation(k)))
176177
.subscribeOn(Schedulers.boundedElastic())
177-
.cache()
178+
.cache((manager) -> Duration.ofMillis(Long.MAX_VALUE), (ex) -> Duration.ZERO, () -> Duration.ZERO)
178179
);
179180
// @formatter:on
180181
}

oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/authentication/JwtIssuerAuthenticationManagerResolverTests.java

+29
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,35 @@ public void resolveWhenUsingTrustedIssuerThenReturnsAuthenticationManager() thro
8484
}
8585
}
8686

87+
@Test
88+
public void resolveWhednUsingTrustedIssuerThenReturnsAuthenticationManager() throws Exception {
89+
try (MockWebServer server = new MockWebServer()) {
90+
server.start();
91+
String issuer = server.url("").toString();
92+
// @formatter:off
93+
server.enqueue(new MockResponse().setResponseCode(500)
94+
.setHeader("Content-Type", "application/json")
95+
.setBody(String.format(DEFAULT_RESPONSE_TEMPLATE, issuer, issuer))
96+
);
97+
server.enqueue(new MockResponse().setResponseCode(200)
98+
.setHeader("Content-Type", "application/json")
99+
.setBody(String.format(DEFAULT_RESPONSE_TEMPLATE, issuer, issuer))
100+
);
101+
// @formatter:on
102+
JWSObject jws = new JWSObject(new JWSHeader(JWSAlgorithm.RS256),
103+
new Payload(new JSONObject(Collections.singletonMap(JwtClaimNames.ISS, issuer))));
104+
jws.sign(new RSASSASigner(TestKeys.DEFAULT_PRIVATE_KEY));
105+
JwtIssuerAuthenticationManagerResolver authenticationManagerResolver = new JwtIssuerAuthenticationManagerResolver(
106+
issuer);
107+
MockHttpServletRequest request = new MockHttpServletRequest();
108+
request.addHeader("Authorization", "Bearer " + jws.serialize());
109+
assertThatExceptionOfType(IllegalArgumentException.class)
110+
.isThrownBy(() -> authenticationManagerResolver.resolve(request));
111+
AuthenticationManager authenticationManager = authenticationManagerResolver.resolve(request);
112+
assertThat(authenticationManager).isNotNull();
113+
}
114+
}
115+
87116
@Test
88117
public void resolveWhenUsingUntrustedIssuerThenException() {
89118
JwtIssuerAuthenticationManagerResolver authenticationManagerResolver = new JwtIssuerAuthenticationManagerResolver(

oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/authentication/JwtIssuerReactiveAuthenticationManagerResolverTests.java

+25
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,31 @@ public void resolveWhenUsingTrustedIssuerThenReturnsAuthenticationManager() thro
8686
}
8787
}
8888

89+
// gh-10444
90+
@Test
91+
public void resolveWhednUsingTrustedIssuerThenReturnsAuthenticationManager() throws Exception {
92+
try (MockWebServer server = new MockWebServer()) {
93+
String issuer = server.url("").toString();
94+
// @formatter:off
95+
server.enqueue(new MockResponse().setResponseCode(500).setHeader("Content-Type", "application/json")
96+
.setBody(String.format(DEFAULT_RESPONSE_TEMPLATE, issuer, issuer)));
97+
server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-Type", "application/json")
98+
.setBody(String.format(DEFAULT_RESPONSE_TEMPLATE, issuer, issuer)));
99+
// @formatter:on
100+
JWSObject jws = new JWSObject(new JWSHeader(JWSAlgorithm.RS256),
101+
new Payload(new JSONObject(Collections.singletonMap(JwtClaimNames.ISS, issuer))));
102+
jws.sign(new RSASSASigner(TestKeys.DEFAULT_PRIVATE_KEY));
103+
JwtIssuerReactiveAuthenticationManagerResolver authenticationManagerResolver = new JwtIssuerReactiveAuthenticationManagerResolver(
104+
issuer);
105+
MockServerWebExchange exchange = withBearerToken(jws.serialize());
106+
assertThatExceptionOfType(IllegalArgumentException.class)
107+
.isThrownBy(() -> authenticationManagerResolver.resolve(exchange).block());
108+
ReactiveAuthenticationManager authenticationManager = authenticationManagerResolver.resolve(exchange)
109+
.block();
110+
assertThat(authenticationManager).isNotNull();
111+
}
112+
}
113+
89114
@Test
90115
public void resolveWhenUsingUntrustedIssuerThenException() {
91116
JwtIssuerReactiveAuthenticationManagerResolver authenticationManagerResolver = new JwtIssuerReactiveAuthenticationManagerResolver(

0 commit comments

Comments
 (0)