Java RestTemplate removing all cookies

Soldato
Joined
30 Jan 2007
Posts
15,485
Location
PA, USA (Orig UK)
I need to remove/reset cookies from a RestTemplate (header) before I make another call.

System: Spring Boot and using RestTemplate, and building it with RestTemplateBuilder. This is a backend call only in this particular instance.

Issue: A call is being made and a cookie is being set by the called system to maintain 'stickyness' to a particular RHOS (OpenShift) Node. I need to remove this cookie before making a second call.

Yes, yes, we know how to fix it in helm charts by removing the route notation, but right now due to a system limitation, we need to find a work-around.

How can I remove the cookie without having to create another instance of RestTemplate. (for real 'fun' reasons I might need to make this call ~10 times to get all the information I need and need to hit different hosts). Possibly sending in a new HTTPEntity with new headers?

Alternatively... block cookies being returned to me.
 
OK, so the approach I took was to disable cookie management for this particular call. Too Achieve this, I used a

// Create your java class as below where *** is the name of your package you need to change.
Code:
package com.***;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.boot.web.client.RestTemplateCustomizer;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
public class CustomRestTemplateCustomizer implements RestTemplateCustomizer {
    @Override
    public void customize(RestTemplate restTemplate) {
        final CloseableHttpClient httpClient = HttpClientBuilder.create().disableCookieManagement().build();
        restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));
    }
}
--------

// In your ApplicationConfiguration or where you define your Beans:
Code:
@Bean
    public CustomRestTemplateCustomizer customRestTemplateCustomizer() {
        return new CustomRestTemplateCustomizer();
    }


// Then if you are using a bean for your setup of the RestTemplate make sure you have the DependsOn annotation.
Code:
@Bean
    @DependsOn(value = {"customRestTemplateCustomizer"})
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        String baseUrl = applicationProperties.getBaseUrl();
       
       // TODO I removed any credential handling here you might need to do that yourself.
        RestTemplate template = builder.rootUri(baseUrl).build();
       
        template.setErrorHandler(new ResponseErrorHandler() {
            // TODO Any custom error handling you have can go here
        });

Again, this is for a short term use case, where we need to check the status of multiple Nodes on OpenShift containers, and get around the routing that was being done by the cookie's being returned to 'stick' us to a particular node. As soon as we redeploy with an update to the app it is calling, the need for this goes away. Was just interesting, as I've not got that in-depth with Rest Templates before.
 
Back
Top Bottom