Setting Security cookie using RestTemplate

前端 未结 3 1214
长情又很酷
长情又很酷 2021-01-04 21:48

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 achiev

3条回答
  •  醉梦人生
    2021-01-04 22:33

    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());
       }
    }
    

提交回复
热议问题