Skip to content

RefreshTokenOAuth2AuthorizedClientProvider initialization slows down application startup #19406

Description

@AB-xdev

Describe the bug

During application startup RefreshTokenOAuth2AuthorizedClientProvider is created by org.springframework.security.config.annotation.web.configuration.OAuth2ClientConfiguration.OAuth2AuthorizedClientManagerRegistrar#getRefreshTokenAuthorizedClientProvider

and because of

private OAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> accessTokenResponseClient = new RestClientRefreshTokenTokenResponseClient();

it requires quite some time (300ms; ~3% of my application startup time is spent there) to initialize because it creates a HttpClient:

Image

Currently it's not possible in any way to disable this behavior because

  • There is no constructor of RefreshTokenOAuth2AuthorizedClientProvider that leaves accessTokenResponseClient empty
  • The classes can't be overriden/proxied because they are all declared final
    • I'm not sure why this is the case because OAuth2AuthorizedClientManagerRegistrar#getAuthorizedClientProviderByType checks for subclasses of this type which is impossible when they have been declared final

To Reproduce
N/A see code above

Expected behavior

  • Create a constructor of RefreshTokenOAuth2AuthorizedClientProvider that does not initialize accessTokenResponseClient
  • Make RefreshTokenOAuth2AuthorizedClientProvider not final so that users can provide their custom implementations. This should be done for all classes that implement OAuth2AuthorizedClientProvider
  • Maybe initialize the default RestClientRefreshTokenTokenResponseClient on a background thread if available to not slow down the application start

Sample

Already described above

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions