I am writing a batch using Spring Boot 2.7.1 and spring-boot-starter-batch
. The batch needs 2 different WebClient
to call 2 different APIs with different authentication systems, that I configure through standard Spring Boot properties (spring.security.oauth2.client etc).
It works well, but I realized the batch was listening on port 8080 when running, because I have imported spring-boot-starter-web
, which enables the auto-configuration of my WebClient
, by injecting a ClientRegistrationRepository
. It's not a major issue, but it prevents me from launching the batch twice in parallel for instance, because the port is already used... so I would like to disable the web server part.
The problem is that when I disable the web server, either through properties, code or dependencies (by removing spring-boot-starter-web
), then the batch doesn't start anymore, because ClientRegistrationRepository
is not loaded anymore, because I require
a bean of type 'org.springframework.security.oauth2.client.registration.ClientRegistrationRepository' that could not be found
This is because, there's a conditional on Spring's OAuth2ClientAutoConfiguration
:
@AutoConfiguration(before = SecurityAutoConfiguration.class)
@ConditionalOnClass({ EnableWebSecurity.class, ClientRegistration.class })
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@Import({ OAuth2ClientRegistrationRepositoryConfiguration.class, OAuth2WebSecurityConfiguration.class })
public class OAuth2ClientAutoConfiguration {
}
Because the application is not of type SERVLET
, but NONE
, this doesn't get enabled.
I've tried to "force load" it :
@ImportAutoConfiguration(OAuth2ClientAutoConfiguration.class)
but it doesn't work.
Looking into the source code, I see that OAuth2ClientAutoConfiguration
is actually loading 2 config classes, but they are not public, so I can't import them directly :
@Import({ OAuth2ClientRegistrationRepositoryConfiguration.class, OAuth2WebSecurityConfiguration.class })
there must be a trick to achieve that.. but what is it ?