spring-projects / spring-projects/spring-security
Add ServletBearerRequestInterceptor for RestClient (equivalent of ServletBearerExchangeFilterFunction)
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 9.6k
- Forks
- 6.3k
- Avg merge
- 2d 11h
- Merged PRs (30d)
- 52
Description
Expected Behavior
RestClient should provide an interceptor similar to ServletBearerExchangeFilterFunction for WebClient.
This would allow automatic propagation of an Authorization: Bearer header, by resolving an OAuth2AuthorizedClient through OAuth2AuthorizedClientManager and the current Authentication from SecurityContextHolder.
Current Behavior
Currently, there is no built-in integration between Spring Security and RestClient. Developers must implement their own ClientHttpRequestInterceptor to manually resolve an access token and set it in the request headers. This leads to repetitive boilerplate code and inconsistency compared to WebClient.
Context
We are adopting the new RestClient introduced in Spring Framework 6.1 for synchronous HTTP requests. While WebClient has a convenient ServletBearerExchangeFilterFunction to transparently handle OAuth2 access tokens, no such integration exists for RestClient.
This results in:
- Duplicated custom interceptors across applications.
- Inconsistency between RestClient and WebClient usage patterns.
- Higher risk of misconfigurations for token propagation.
Alternatives considered:
Implementing a custom ClientHttpRequestInterceptor for each application.
Wrapping RestClient builders to centralize token logic.
Workaround:
A manual RequestInterceptor works, but it lacks the standardization and developer experience provided by Spring Security’s support for WebClient.
Proposal:
Introduce a ServletBearerRequestInterceptor (or similar) for RestClient that behaves like ServletBearerExchangeFilterFunction does for WebClient, enabling consistent, secure, and boilerplate-free token propagation in Servlet environments.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading ServletBearerExchangeFilterFunction and the RestClient ClientHttpRequestInterceptor integration described in the issue. Trace how OAuth2AuthorizedClientManager resolves a client using the current Authentication from SecurityContextHolder. Done means RestClient can automatically propagate an Authorization: Bearer header through a built-in servlet interceptor comparable to the WebClient support.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- authentication, backend-api-design
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100