spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

Add Autoconfiguration for OAuth2 + Interface HTTP Clients

Open rwinch opened this issue 1 year ago • 11 comments

It would be nice if Spring Boot added some autoconfiguration for applying Spring Security's OAuth2 Support for Interface HTTP Clients.

The configuration looks like this for RestClient and should be activated with the classpath has RestClient, the OAuth2 client jar, and has an OAuth2AuthorizedClientManager bean.

@Bean
OAuth2RestClientHttpServiceGroupConfigurer securityConfigurer(
		OAuth2AuthorizedClientManager manager) {
	return OAuth2RestClientHttpServiceGroupConfigurer.from(manager);
}

The configuration looks like this for WebClient and should be activated with the classpath has WebClient and OAuth2 client jar, and has an OAuth2AuthorizedClientManager bean.

@Bean
OAuth2RestClientHttpServiceGroupConfigurer securityConfigurer(
		OAuth2AuthorizedClientManager manager) {
	return OAuth2RestClientHttpServiceGroupConfigurer.from(manager);
}

cc @joshlong

rwinch avatar Aug 25 '25 18:08 rwinch

Hi there, I'd like to contribute to this issue.

I'll start working on adding autoconfiguration for OAuth2 support to both RestClient and WebClient in Spring Boot.

Are there any additional considerations or points I should be aware of before I get started?

Torres-09 avatar Aug 26 '25 15:08 Torres-09

Thanks for the offer, @Torres-09.

In addition to the two scenarios that @rwinch has described above. I think we should also consider the use of WebClient in a servlet application. In that case, the OAuth2WebClientHttpServiceGroupConfigurer should be created using from(OAuth2AuthorizedClientManager).

@rwinch please let us know if there's good reason not to do so.

wilkinsona avatar Aug 26 '25 16:08 wilkinsona

Hello, I'm following this issue with interest. There have been no updates for a few weeks, so I wanted to ask if there is any news or progress on this. Thank you!

Torres-09 avatar Sep 08 '25 14:09 Torres-09

Nothing as yet @Torres-09, but rest assured that we'll update this issue when progress has been made.

philwebb avatar Sep 08 '25 18:09 philwebb

Is this issue still open if so im currently working on OAuth2 and i believe i will be able to resolve the issue

builtDifferentCoder avatar Sep 16 '25 04:09 builtDifferentCoder

@builtDifferentCoder we're awaiting feedback from @rwinch. Once we have that, @Torres-09 has already offered to make a contribution.

wilkinsona avatar Sep 16 '25 06:09 wilkinsona

Thank you for the ping @wilkinsona

In addition to the two scenarios that @rwinch has described above. I think we should also consider the use of WebClient in a servlet application. In that case, the OAuth2WebClientHttpServiceGroupConfigurer should be created using from(OAuth2AuthorizedClientManager).

Yes. This is another valid use case that should be accounted for. Thank you!

rwinch avatar Sep 17 '25 19:09 rwinch

Hello, Would it be okay if I start working on this? Before I begin, I want to propose my implementation plan to ensure I'm on the right track.

1. Implementation Plan

My plan is to create a single auto-configuration class that handles all the discussed scenarios. I intend to use nested @Configuration classes with conditional annotations (@ConditionalOnWebApplication) to provide the correct Configurer bean for each environment:

RestClient: from(OAuth2AuthorizedClientManager) OAuth2RestClientHttpServiceGroupConfigurer

WebClient in a servlet environment & WebClient in a non-servlet environment(reactive&none): A specific configuration that internally uses from(OAuth2AuthorizedClientManager) to correctly handle the servlet context. OAuth2WebClientHttpServiceGroupConfigurer

Does this overall approach seem reasonable to you?

2. Package Location

For the package location, I believe the new auto-configuration class fits best in org.springframework.boot.security.oauth2.client.autoconfigure

My reasoning is that this feature is fundamentally about applying OAuth2 Client security to the HTTP clients, rather than being a core feature of RestClient or WebClient themselves.

I would appreciate your thoughts on this placement. Thank you!

cc. @rwinch , @wilkinsona

Torres-09 avatar Sep 22 '25 11:09 Torres-09

Hi @wilkinsona, can i work on this?

ahomaidat avatar Jan 20 '26 21:01 ahomaidat

@ahomaidat, Thanks for the offer, but I think we may need some design work on this issue before we accept any contributions. I've added a label to the issue to show we're not ready yet.

philwebb avatar Jan 20 '26 21:01 philwebb

I was hoping to squeeze this into 4.1, but unfortunately it's more complex that it first appears.

The configuration ... should be activated with the classpath has RestClient, the OAuth2 client jar, and has an OAuth2AuthorizedClientManager bean

Detecting the OAuth2AuthorizedClientManager turns out to be quite difficult. Using a regular @ConditionalOnBean doesn't work. I think this is because in a standard setup the bean definition isn't created in time.

If I'm following the logic correctly OAuth2ClientConfiguration.OAuth2AuthorizedClientManagerConfiguration is registered by OAuth2ImportSelector which happens as part of @EnableWebSecurity. That configuration in turn registers a OAuth2AuthorizedClientManagerRegistrar.

Things get interesting here because OAuth2AuthorizedClientManagerRegistrar is BeanDefinitionRegistryPostProcessor and it doesn't get around to registering authorizedClientManagerRegistrar until it's too late.

Perhaps we can detect a OAuth2AuthorizedClientManager bean or a authorizedClientManagerRegistrar bean? It looks like OAuth2AuthorizedClientManagerRegistrar might still not register a bean there's not exactly one ClientRegistrationRepository and one OAuth2AuthorizedClientRepository. It's also not a public class so we'd be doing a bean name match I guess.

Another option might be for us to auto-configure a DefaultOAuth2AuthorizedClientManager with a @ConditionalOnMissingBean(OAuth2AuthorizedClientManager.class). I'm not sure we'd do the correct thing because the logic to create the default manager is quite involved.

philwebb avatar Apr 23 '26 02:04 philwebb