spring-projects / spring-projects/spring-security

Add ServletBearerRequestInterceptor for RestClient (equivalent of ServletBearerExchangeFilterFunction)

Open
#17,958 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage type: enhancement
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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.