Skip to content

Commit 8f6593a

Browse files
martinr0xjgrandja
authored andcommitted
Set default timeout when fetching JWKSet for private_key_jwt
Closes spring-projectsgh-1413
1 parent c3f86d1 commit 8f6593a

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/JwtClientAssertionDecoderFactory.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2022 the original author or authors.
2+
* Copyright 2020-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828

2929
import javax.crypto.spec.SecretKeySpec;
3030

31+
import org.springframework.http.client.SimpleClientHttpRequestFactory;
3132
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
3233
import org.springframework.security.oauth2.core.DelegatingOAuth2TokenValidator;
3334
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
@@ -51,6 +52,7 @@
5152
import org.springframework.util.Assert;
5253
import org.springframework.util.CollectionUtils;
5354
import org.springframework.util.StringUtils;
55+
import org.springframework.web.client.RestTemplate;
5456
import org.springframework.web.util.UriComponentsBuilder;
5557

5658
/**
@@ -87,6 +89,15 @@ public final class JwtClientAssertionDecoderFactory implements JwtDecoderFactory
8789
JCA_ALGORITHM_MAPPINGS = Collections.unmodifiableMap(mappings);
8890
}
8991

92+
private static final RestTemplate restTemplate = new RestTemplate();
93+
94+
static {
95+
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
96+
requestFactory.setConnectTimeout(15_000);
97+
requestFactory.setReadTimeout(15_000);
98+
restTemplate.setRequestFactory(requestFactory);
99+
}
100+
90101
private final Map<String, JwtDecoder> jwtDecoders = new ConcurrentHashMap<>();
91102
private Function<RegisteredClient, OAuth2TokenValidator<Jwt>> jwtValidatorFactory = DEFAULT_JWT_VALIDATOR_FACTORY;
92103

@@ -124,7 +135,8 @@ private static NimbusJwtDecoder buildDecoder(RegisteredClient registeredClient)
124135
JWT_CLIENT_AUTHENTICATION_ERROR_URI);
125136
throw new OAuth2AuthenticationException(oauth2Error);
126137
}
127-
return NimbusJwtDecoder.withJwkSetUri(jwkSetUrl).jwsAlgorithm((SignatureAlgorithm) jwsAlgorithm).build();
138+
return NimbusJwtDecoder.withJwkSetUri(jwkSetUrl).jwsAlgorithm((SignatureAlgorithm) jwsAlgorithm)
139+
.restOperations(restTemplate).build();
128140
}
129141
if (jwsAlgorithm instanceof MacAlgorithm) {
130142
String clientSecret = registeredClient.getClientSecret();

0 commit comments

Comments
 (0)