10

I am trying to call a Restful JSON service using RestTemplate and Jackson json convertor. Now in order to call the service I need to pass in a Security cookie. I can achieve this by using URLConnection (See the code below)

URL url= new URL("https://XXXXXXXX");

URLConnection yc = url.openConnection();
yc.setRequestProperty("SecurityCookie", ssocookie.getValue());</code>

Whats the parallel for this in RestTemplate? Here is a code snippet which I have been using to call a Restful Service using RestTemplate:

RestTemplate rest = new RestTemplate();  
InputBean input = new InputBean();  
input.setResource("SampleResource");  
HttpEntity<InputBean > entity = new HttpEntity<InputBean>(input);  
ResponseEntity<OutputBean> response1 = rest.postForEntity(
    "https://XXXXXXXXX", 
    entity, OutputBean.class);</code>

I can not figure out how to pass the security cookie while using RestTemplate to call the service. Any help on this would be great.

Trygve Laugstøl
  • 7,440
  • 2
  • 36
  • 40

3 Answers3

38

I wrote a blog post that explains how to do this using request headers:

http://springinpractice.com/2012/04/08/sending-cookies-with-resttemplate/

Here's the code:

HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Cookie", "JSESSIONID=" + session.getValue());
HttpEntity requestEntity = new HttpEntity(null, requestHeaders);
ResponseEntity rssResponse = restTemplate.exchange(
    "https://jira.example.com/sr/jira.issueviews:searchrequest-xml/18107/SearchRequest-18107.xml?tempMax=1000",
    HttpMethod.GET,
    requestEntity,
    Rss.class);
Rss rss = rssResponse.getBody();
  • Seems `HttpEntity` [is abstract](https://hc.apache.org/httpcomponents-core-4.3.x/httpcore/apidocs/org/apache/http/HttpEntity.html) since apache http 4.0. Is there a way to keep using this? – Aritz Mar 31 '14 at 15:55
  • To clarify, this is Spring's HttpEntity class rather than Apache's. –  Mar 31 '14 at 21:27
  • Oops! Could you provide the required libraries and imports for your example? I'm becoming crazy with an Android + spring project. Same class names everywhere! – Aritz Apr 01 '14 at 04:56
  • They are in the spring-web library, org.springframework.http package. –  Apr 01 '14 at 05:03
  • This only works with exchange. If you want to use methods like getForObject it doesn't – ryber Dec 19 '14 at 16:22
  • 1
    That's right. The getForObject() and related methods are really just convenience methods for the more general-purpose exchange() methods. –  Feb 05 '15 at 17:55
  • 2
    The link is dead for me now, but available on the Internet Archive: https://web.archive.org/web/20150320081025/http://springinpractice.com/2012/04/08/sending-cookies-with-resttemplate/ – Jean-François Beauchef Aug 11 '17 at 13:26
  • please post answer to https://stackoverflow.com/questions/22853321/resttemplate-client-with-cookies – mattsmith5 Jul 26 '23 at 23:38
  • posted question here, https://stackoverflow.com/questions/76775831/setting-security-cookie-using-resttemplate-with-domain-and-as-secure – mattsmith5 Jul 27 '23 at 00:31
3

This is how it has worked for us

requestHeaders.add("Cookie", "JSESSIONID=" + session.getValue());
RamenChef
  • 5,557
  • 11
  • 31
  • 43
Pravin Bansal
  • 4,315
  • 1
  • 28
  • 19
  • posted question here, https://stackoverflow.com/questions/76775831/setting-security-cookie-using-resttemplate-with-domain-and-as-secure – mattsmith5 Jul 27 '23 at 00:32
3

You can access the underlying HttpURLConnection used by RestTemplate by wiring your RestTemplate up with a custom ClientHttpRequestFactory, which lets you access the underlying connection to set headers, properties, etc. The ClientHttpRequestFactory is used by RestTemplate when creating new connections.

In particular, you can extend the SimpleClientHttpRequestFactory implementation and override the prepareConnection() method:

public class YourClientHttpRequestFactory extends SimpleClientHttpRequestFactory {
  @Override
   protected void prepareConnection(HttpURLConnection connection, String httpMethod) {
     connection.setRequestProperty("SecurityCookie", ssocookie.getValue());
   }
}
matt b
  • 138,234
  • 66
  • 282
  • 345
  • I also have similar question [here](https://stackoverflow.com/questions/25698072/simpleclienthttprequestfactory-vs-httpcomponentsclienthttprequestfactory-for-htt) on `RestTemplate` in which I am having doubts on `ClientHttpRequestFactory` implementations. See if you can help me out if possible. I am stuck on this for a while. Any help will be appreciated. – AKIWEB Sep 07 '14 at 00:43
  • This answer is outdated - the other answer is better. – Robin Green Dec 08 '14 at 18:14