I have rest template config to use restTemplate for calling 3rd Party Service API. That 3rd Party Service API needs only Basic Auth from security. So in general it looks like this
My local uri i.e. localhost:8082/api/caller -> restTemplate.getForEntity(exact 3rd party service API URL)
. The issue is that on restTemplate.getForEntity
step, my headers are empty, but it's not a problem, I can set it using interceptor, like this
@Configuration
public class RestTemplateConfig {
@NonNull
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
HttpHeaders headers = request.getHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Basic c3VHaWxzNFlBaExZNEg6NWtwQTJuV1AAAA3YXVOd1FWWXVkTEdNjoRrQks0MUVjY1hNa3VRYUdSdE1VMDdyWUtpclNycDIzcGtASktuRQ==");
return execution.execute(request, body);
}
@Bean
public RestTemplate restTemplate() {
final RestTemplate restTemplate = new RestTemplate();
final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(new ObjectMapper());
restTemplate.getMessageConverters().add(converter);
restTemplate.getInterceptors().add(this::intercept);
return restTemplate;
}
}
As you see, I've hardcoded value Authorization
, but I need to have to get it retrieved automatically for every request. How to make it available for restTemplate too?