Spring RestTemplate unable to get JSESSIONID cookie
<p>In my Spring Boot/Spring Security application I'm trying to login with Spring RestTemplate.</p>

<p>This is a code:</p>

<pre><code>final KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(new FileInputStream(new File("keystore.p12")), "changeit".toCharArray());   

final SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).loadKeyMaterial(keyStore, "changeit".toCharArray()).build();   

final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);   
final HttpClient httpClient = HttpClientBuilder.create().setSSLSocketFactory(socketFactory).setRedirectStrategy(new LaxRedirectStrategy()).build();   
final ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);   
final RestTemplate restTemplate = new RestTemplate(requestFactory);   

final HttpHeaders headers = new HttpHeaders();   
headers.add("Cookie", "JSESSIONID=" + loginResponse.getJsessionid());    
headers.add("X-XSRF-TOKEN", loginResponse.getCsrf());   

final MultiValueMap&lt;String, String&gt; body = new LinkedMultiValueMap&lt;String, String&gt;();    
body.add("username", "username");    
body.add("password", "password");    
final HttpEntity&lt;?&gt; requestEntity = new HttpEntity&lt;Object&gt;(body, headers);

final ResponseEntity&lt;String&gt; responseEntity = restTemplate.exchange("https://localhost:" + port + "/api/login", HttpMethod.POST, requestEntity, String.class);
</code></pre>

<p>This is what I'm receiving in <code>responseEntity</code> headers:</p>

<pre><code>Server = [Apache-Coyote/1.1]
X-Content-Type-Options = [nosniff]
X-XSS-Protection = [1; mode=block]
Cache-Control = [no-cache, no-store, max-age=0, must-revalidate]
Pragma = [no-cache]
Expires = [0]
Strict-Transport-Security = [max-age=31536000 ; includeSubDomains]
Set-Cookie = [XSRF-TOKEN=cf1968b0-068b-455b-be8f-10e39e0e44a4; Path=/]
X-Application-Context = [application:0]
Content-Type = [text/plain;charset=ISO-8859-1]
Content-Length = [12]
Date = [Thu, 21 Apr 2016 19:32:34 GMT]
</code></pre>

<p>As you can see - there is only <code>XSRF-TOKEN</code> cookie but no <code>JSESSIONID</code>.</p>

<p>I think the possible issue can be in redirect after successful authentication at <a href="https://localhost/api/login" rel="nofollow">https://localhost/api/login</a> to <a href="https://localhost/api/" rel="nofollow">https://localhost/api/</a> </p>

<p>What am I doing wrong ? How to receive <code>JSESSIONID</code> cookie also ?</p>