I'm creating a system which regularly exports data on behalf of many users to an external system, with OAuth2-authenticated HTTP requests.
I have successfully been able to communicate with the external service using Spring Security OAuth2, with an OAuth2RestTemplate configured like this:
@Configuration
@EnableOAuth2Client
public class ExternalServiceConfiguration {
@Autowired
private OAuth2ClientContext oauth2Context;
@Bean
public OAuth2ProtectedResourceDetails credentials() {
ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails();
details.setAccessTokenUri("https://external-service.example.com/OAuth/Token");
details.setClientId("abcdefghijklmnopq");
details.setClientSecret("123456789123456789123456789");
details.setGrantType("client_credentials");
return details;
}
@Bean
public OAuth2RestTemplate externalServiceRestTemplate() {
return new OAuth2RestTemplate(credentials(), oauth2Context);
}
}
This works well, and I'm able to inject the OAuth2RestTemplate bean into my services:
@Autowired
@Qualifier("externalServiceRestTemplate")
private OAuth2RestTemplate restTemplate;
However, in my application, I have a number of users that each need to configure their own client keys. In case it's relevant, I'm doing this a a batch job, meaning it is done outside of a regular HTTP request, and sometimes in the same thread context.
This means that I will need to have multiple OAuth2ProtectedResourceDetails, and, I assume, multiple OAuth2RestTemplate instances as well. As this is something each user will be configuring on their own, it has to happen dynamically, based on client credentials saved in a database.
Does anyone have any advice on how to configure a dynamic number of OAuth2RestTemplate instances in an efficient, but thread-safe way?