Skip to content

Add timeouts for JWT decoder in AadResourceServerConfiguration#49312

Closed
berry120 wants to merge 2 commits into
Azure:mainfrom
berry120:patch-1
Closed

Add timeouts for JWT decoder in AadResourceServerConfiguration#49312
berry120 wants to merge 2 commits into
Azure:mainfrom
berry120:patch-1

Conversation

@berry120
Copy link
Copy Markdown

@berry120 berry120 commented May 29, 2026

Set default read & connect timeouts on NimbusJwtDecoder.

Description

This spring security issue highlighted the potential for 15 minute (or longer) hangs due to the absence of explicit connect & read timeouts on the NimbusJwtDecoder. This is fixed in Spring Security, however the Azure SDK explicitly overrides restOperations in the NimbusJwtDecoder it creates, meaning we need the fix explicitly here, too.

As a current workaround, the following can be defined as a bean on a configuration class:

    @Bean
    RestTemplateBuilder restTemplateBuilder(RestTemplateBuilderConfigurer configurer) {
        return configurer.configure(new RestTemplateBuilder())
            .connectTimeout(Duration.ofMillis(JWKSourceBuilder.DEFAULT_HTTP_CONNECT_TIMEOUT))
            .readTimeout(Duration.ofMillis(JWKSourceBuilder.DEFAULT_HTTP_READ_TIMEOUT));
    }

However, this isn't ideal as it applies globally, and has to be specified in each project.

All SDK Contribution checklist:

  • The pull request does not introduce [breaking changes]
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

Set connection and read timeouts for JWT decoder.
Copilot AI review requested due to automatic review settings May 29, 2026 14:54
@berry120 berry120 requested review from a team, Netyyyy, moarychan, rujche and saragluna as code owners May 29, 2026 14:54
@github-actions github-actions Bot added azure-spring All azure-spring related issues Community Contribution Community members are working on the issue customer-reported Issues that are reported by GitHub users external to the Azure organization. labels May 29, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Thank you for your contribution @berry120! We will review the pull request and get back to you soon.

@rujche rujche assigned rujche and berry120 and unassigned rujche Jun 1, 2026
@rujche rujche added this to the 2026-06 milestone Jun 1, 2026
@rujche
Copy link
Copy Markdown
Member

rujche commented Jun 1, 2026

Hi, @berry120 . Thanks for creating this PR. How about using RestTemplateCustomizer to add timeouts? Here are related codes: https://github.com/spring-projects/spring-boot/blob/bcecf922c6ee98d9451f414b4bf2213ed8f47922/module/spring-boot-restclient/src/main/java/org/springframework/boot/restclient/autoconfigure/RestTemplateAutoConfiguration.java#L48-L72

public final class RestTemplateAutoConfiguration {

	@Bean
	@Lazy
	RestTemplateBuilderConfigurer restTemplateBuilderConfigurer(
			ObjectProvider<ClientHttpRequestFactoryBuilder<?>> clientHttpRequestFactoryBuilder,
			ObjectProvider<HttpClientSettings> httpClientSettings,
			ObjectProvider<ClientHttpMessageConvertersCustomizer> convertersCustomizers,
			ObjectProvider<RestTemplateCustomizer> restTemplateCustomizers,
			ObjectProvider<RestTemplateRequestCustomizer<?>> restTemplateRequestCustomizers) {
		RestTemplateBuilderConfigurer configurer = new RestTemplateBuilderConfigurer();
		configurer.setRequestFactoryBuilder(clientHttpRequestFactoryBuilder.getIfAvailable());
		configurer.setClientSettings(httpClientSettings.getIfAvailable());
		configurer.setHttpMessageConvertersCustomizers(convertersCustomizers.orderedStream().toList());
		configurer.setRestTemplateCustomizers(restTemplateCustomizers.orderedStream().toList());
		configurer.setRestTemplateRequestCustomizers(restTemplateRequestCustomizers.orderedStream().toList());
		return configurer;
	}

	@Bean
	@Lazy
	@ConditionalOnMissingBean
	RestTemplateBuilder restTemplateBuilder(RestTemplateBuilderConfigurer restTemplateBuilderConfigurer) {
		return restTemplateBuilderConfigurer.configure(new RestTemplateBuilder());
	}

}

@rujche rujche requested review from Copilot and removed request for Copilot June 1, 2026 05:21
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the Spring Cloud Azure AAD resource server auto-configuration to ensure the internally created NimbusJwtDecoder uses explicit HTTP connect/read timeouts when retrieving the JWK set, preventing potentially long hangs when network timeouts aren’t otherwise configured.

Changes:

  • Introduces default connect and read timeout values for the NimbusJwtDecoder JWK retrieval path.
  • Applies these timeouts via RestTemplateBuilder when constructing the RestOperations used by the decoder.

Comment on lines +47 to +48
private static final Duration JWT_DECODER_CONNECT_TIMEOUT = Duration.ofMillis(500);
private static final Duration JWT_DECODER_READ_TIMEOUT = Duration.ofMillis(500);
Comment on lines +64 to +67
.restOperations(createRestTemplate(restTemplateBuilder
.connectTimeout(JWT_DECODER_CONNECT_TIMEOUT)
.readTimeout(JWT_DECODER_READ_TIMEOUT)))
.build();
@rujche
Copy link
Copy Markdown
Member

rujche commented Jun 1, 2026

Closing in favor of #49329

@rujche rujche closed this Jun 1, 2026
@github-project-automation github-project-automation Bot moved this from Todo to Done in Spring Cloud Azure Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

azure-spring All azure-spring related issues Community Contribution Community members are working on the issue customer-reported Issues that are reported by GitHub users external to the Azure organization.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants