title: clear Cookies with RestTemplate before make HTTP-call
body: <p>I use Spring-Boot 2.0.2 <code>RestTemplate</code> to make following scenario:</p>
  
  <p>I'm a Rest-Consumer (client), which :</p>
  
  <ol>
  <li>first need to log in on a Spring-Security-Check</li>
  <li>then make a second call to get the data.</li>
  </ol>
  
  <p>First, I was thinking to make the auth call and manually read the JSESSIONID Cookie from SET-COOKIE and set it on the second call in the header. Here my first try:</p>
  
  <p>The RestClient : </p>
  
  <pre><code>@Service
  public class RestClient {
      private static final String ENDPOINT_DATA = "/data";
      private static final String ENDPOINT_SECURITY_CHECK = "/j_spring_security_check";
  
      private static final String HTTP_HEADER_KEY_SET_COOKIE = "Set-Cookie";
      private static final String HTTP_HEADER_KEY_COOKIE = "Cookie";
  
      private static final String PROPERTY_SPRING_SECURITY_USER = "j_username";
      private static final String PROPERTY_SPRING_SECURITY_PASS = "j_password";
  
      private final RestTemplate restTemplate;
      private final RestConfig restConfig;
  
      @Autowired
      public RestClient(RestTemplateBuilder restTemplateBuilder, final RestConfig restConfig) {
  
          notNull(restTemplateBuilder, "restTemplateBuilder must not be null!");
          this.restTemplate = restTemplateBuilder
                  .additionalCustomizers(new NoRedirectionHandlingRestTemplateCostumizer())
                  .build();
  
          notNull(restConfig, "openIdConfig must not be null!");
          this.restConfig = restConfig;
      }
  
      public String getData() {
          final String jSessionCockie = jSpringSecurityLogin();
          return getAuthorizeCode(jSessionCockie);
      }
  
      private String jSpringSecurityLogin() {
          // read config
          final String fullLoginUri = restConfig.getUrl() + ENDPOINT_SECURITY_CHECK;
          final String user = restConfig.getUser();
          final String password = restConfig.getPassword();
  
          // Build entity that is send
          final HttpHeaders headers = new HttpHeaders();
          final String body = PROPERTY_SPRING_SECURITY_USER + "=" + user + "&amp;" + PROPERTY_SPRING_SECURITY_PASS + "=" + password;
          final HttpEntity&lt;String&gt; toSend = new HttpEntity&lt;&gt;(body, headers);
  
          final String jSessionIdCockie;
  
          final ResponseEntity&lt;String&gt; response = restTemplate.postForEntity(fullLoginUri, toSend, String.class);
  
          // Get String "JSESSIONID=XXXX". If there are other Cookies, propably will fail.
          if (HttpStatus.FOUND.equals(response.getStatusCode()) &amp;&amp; response.getHeaders().containsKey(HTTP_HEADER_KEY_SET_COOKIE)) {
              jSessionIdCockie = response.getHeaders().get(HTTP_HEADER_KEY_SET_COOKIE).get(0);
          } else {
              throw new Error();
          }
  
          return jSessionIdCockie;
      }
  
      private String getAuthorizeCode(final String jSessionCockie) {
          // read config
          final String fullDataUri = restConfig.getUrl() + ENDPOINT_DATA;
  
          // Build entity that is send
          final HttpHeaders headers = new HttpHeaders();
          headers.add(HTTP_HEADER_KEY_COOKIE, jSessionCockie);
          final HttpEntity&lt;Void&gt; toSend = new HttpEntity&lt;&gt;(headers);
  
          final ResponseEntity&lt;String&gt; response = restTemplate.exchange(fullDataUri, HttpMethod.GET, toSend, String.class);
  
          if (HttpStatus.OK.equals(response.getStatusCode()) &amp;&amp; response.hasBody()) {
              return response.getBody();
          } else {
              return "";
          }
      }
  }
  </code></pre>
  
  <p>To be complete, here the Costumizer used in the Constructor:</p>
  
  <pre><code>class NoRedirectionHandlingRestTemplateCostumizer implements RestTemplateCustomizer {
      @Override
      public void customize(RestTemplate restTemplate) {
          final HttpClient httpClient = HttpClientBuilder.create()
                  .disableRedirectHandling()
                  .build();
          restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));
      }
  }
  </code></pre>
  
  <p>Now, when I make some Function-Tests with Wiremock I see folowing in the wiremock-reqeust History for the Data-call:</p>
  
  <pre><code>{
    ...
    "headers":
    {
      "Cookie": [
        "JSESSIONID=axcvueiornxniuwherwuieoiun,asdpfoiu",
        "JSESSIONID=axcvueiornxniuwherwuieoiun,asdpfoiu"
      ],
      ...
    },
    "cookies":
    {
      "JSESSIONID": [
        "axcvueiornxniuwherwuieoiun,asdpfoiu",
        "axcvueiornxniuwherwuieoiun,asdpfoiu"
      ]
    }
    ...
  }
  </code></pre>
  
  <p>Wait - JSESSIONID is set 2 times. Cool REST handles it for me ! </p>
  
  <p>Second try: I can remove the Cookie handing. And it works.</p>
  
  <p>But first and second try have on problem: After the first call of <code>RestClient.getData()</code> all following calls to the login-Endpoint also have the JSESSIONID set. I don't know how the Rest-Producer can handle this (Session-timeout e.c.t.). </p>
  
  <h2>Now my problem/question:</h2>
  
  <p>I would like to force <code>RestTemplate</code> to forget/clear all Cookies before making the login-call (reason description before). Is there an possibility ? I did not found anything that worked.</p>
  
  <hr>
  
  <p>My Workaround right now is to disable the Cookie management of <code>RestTemplate</code> and do all manually like in first try. The disabling of the CookieManagment can be done over the <code>RestTemplateCustomizer</code>:</p>
  
  <pre><code>class NoRedirectionHandlingRestTemplateCostumizer implements RestTemplateCustomizer {
      @Override
      public void customize(RestTemplate restTemplate) {
          final HttpClient httpClient = HttpClientBuilder.create()
                  .disableRedirectHandling()
                  .disableCookieManagement()
                  .build();
          restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));
      }
  }
  </code></pre>