Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for requesting protected resources with RestClient similar to ServletBearerExchangeFilterFunction #15820

Open
azizabah opened this issue Sep 17, 2024 · 2 comments
Assignees
Labels
in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) type: enhancement A general enhancement
Milestone

Comments

@azizabah
Copy link

Expected Behavior
It would be nice if the RestClient supported an equivalent of ServletBearerExchangeFilterFunction. This would allow us to easily grab a user's bearer token and pass that on to subsequent client calls without having to explicitly grab the header and token etc.

Current Behavior
Currently I can implement this very easily for a WebClient like this:

@Bean
  public WebClient profileServiceWebClient(WebClient.Builder webClientBuilder) {
    return webClientBuilder.filter(new ServletBearerExchangeFilterFunction()).baseUrl(profileServiceBaseUrl)
        .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build();
  }

As far as I know there is no equivalent implementation for the new RestClient.
Context
This issue is seen as a pretty large blocker for code bases that have to pull a dependency on Spring Boot Starter Webflux (or equivalent) to use WebClient when they are not using a reactive code base. It would be much more preferable to not have to pull that dependency and not have to use reactive code inside a non-reactive code base.

@azizabah azizabah added status: waiting-for-triage An issue we've not yet triaged type: enhancement A general enhancement labels Sep 17, 2024
@sjohnr sjohnr self-assigned this Sep 17, 2024
@sjohnr sjohnr added the in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) label Sep 17, 2024
@sjohnr
Copy link
Member

sjohnr commented Oct 16, 2024

@ch4mpy thanks for providing your thoughts! Most of the features you're sketching out here would be applicable to Spring Boot, not Spring Security. You are welcome to open an issue there. I agree that configuration properties are nice, but this style of feature is akin to programming with yaml, which I don't believe is specifically the purpose of Spring Boot's auto-configuration features. You would need to clarify that with the Boot team however, as I can't speak for them. Of course, it's fine for a 3rd party library such as yours to provide them though.

Maybe could the framework provide a few implementations for each of these two strategies?

Let's stay focused on this ticket which is aimed at providing a ClientHttpRequestInterceptor for OAuth2 Resource Server. If you have other ideas, please open a new issue with those.

@ch4mpy
Copy link
Contributor

ch4mpy commented Oct 31, 2024

In a starter of mine, what I use as ClientHttpRequestInterceptor to forward an access token in the security context of a resource server is the following:

ClientHttpRequestInterceptor forwardingClientHttpRequestInterceptor() {
  return (HttpRequest request, byte[] body, ClientHttpRequestExecution execution) -> {
    final var auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null && auth.getPrincipal() instanceof OAuth2Token oauth2Token) {
      request.getHeaders().setBearerAuth(oauth2Token.getTokenValue());
    }
    return execution.execute(request, body);
  };
}

This should work on any resource server - using a JWT decoder or introspection - but those using a custom authentication converter injecting as principal something that isn't implementing OAuth2Token (which wouldn't be wise).

I have samples in this other repo:

com:
  c4-soft:
    springaddons:
      rest:
        client:
          # This triggers the creation of a @Bean named machinClient (by default a RestClient in a servlet application)
          machin-client:
            base-url: ${machin-api-uri}
            authorization:
              oauth2:
                # This auto-configures the generated RestClient bean with the ClientHttpRequestInterceptor above
                forward-bearer: true

@sjohnr I just moved auto-configuration requests to this Boot ticket.

@sjohnr sjohnr changed the title Add support for requesting protected resources with RestClient via a ServletBearerExchangeFilterFunction or equivalent Add support for requesting protected resources with RestClient similar to ServletBearerExchangeFilterFunction Nov 13, 2024
@sjohnr sjohnr removed the status: waiting-for-triage An issue we've not yet triaged label Nov 14, 2024
@sjohnr sjohnr moved this to Planning in Spring Security Team Nov 14, 2024
@sjohnr sjohnr added this to the 6.5.x milestone Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) type: enhancement A general enhancement
Projects
Status: Planning
Development

No branches or pull requests

3 participants