auto-configure ClientRegistrationRepository in a non-web Spring Boot application

Viewed 77

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 ?

1 Answers

With Spring Boot, you can customize the port of your webapplication by setting the property server.port=8095 in your application.properties or any customized env properties file ie application-dev.properties, application-prod.properties and so on.

Using these property, your webapplication will listen on the port of your choice and will not conflict anymore with other stuff. And if you need multiple instances running in parallel, run them with a range of ports could be great (@see https://stackoverflow.com/a/56884100/390462. You can add listeners too (@see https://stackoverflow.com/a/51876661/390462). Or at least multiple instances of your batch application with differents server.port values.

If you want to disable the webserver, the best bet is to use the property : spring.main.web-application-type=none.

I've just found something interesting using AuthorizedClientServiceOAuth2AuthorizedClientManager in your service layer (@see https://stackoverflow.com/a/60173451/390462

Related