My Service A makes a GET call to Service B, to get some details. In my code, I've configured below that builds RestTemplate with Basic Auth creds.
@Bean(name=Bobresttemplate)
public RestTemplate resttemplate(RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder
.basicAuthentication("username", "password")
.build();
}
I use this in my RestClient.Java/ @Service classes by Autowiring.
@Autowired
@Qualifier("Bobresttemplate")
RestTemplate restTemplate;
My Question: I'm building Bobresttemplate
with Basic Auth, now if I want to use this same Rest template but instead of Basic Auth, I want to use OAuth token to call 3rd party service.I want to acheive a functionality where based on a Toggle, I can use this same BobRestTemplate
but with either of Basic Auth or OAuth.
What extra config do I need to do? Do I need to create another Bean and build this RestTemplate with OAuth creds?
I'm looking to understand whether do I need to create another Bean for configuring this template? Or can I use Interceptors?
It may be helpfull to use spring.profiles
feature:
@Profile("basic-auth")
@Bean(name="Bobresttemplate")
public RestTemplate resttemplateBasic(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder
.basicAuthentication("username", "password")
.build();
}
@Profile("jwt")
@Bean(name="Bobresttemplate")
public RestTemplate resttemplateJwt(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder
...
.build();
}
so in config you just enable profile that you need (no changes in place of injecting resttemplate bean):
spring.profiles.active=jwt
or
spring.profiles.active=basic-auth
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.