spring-projects / spring-projects/spring-security

`RefreshTokenOAuth2AuthorizedClientProvider` initialization slows down application startup

Open
#19,406 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage type: bug
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

Describe the bug

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

and because of

https://github.com/spring-projects/spring-security/blob/9161eced5ba4b65d18fc0585fdf0e29b9f670235/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/RefreshTokenOAuth2AuthorizedClientProvider.java#L53

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with OAuth2ClientConfiguration.OAuth2AuthorizedClientManagerRegistrar#getRefreshTokenAuthorizedClientProvider and RefreshTokenOAuth2AuthorizedClientProvider.java at the linked initialization line. Trace which OAuth2AuthorizedClientProvider implementations and constructors are created during startup, then use the existing startup reproduction to verify that the chosen change avoids unnecessary initialization while preserving refresh-token behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
authentication, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.