使用RestTemplate设置安全性cookie [英] Setting Security cookie using RestTemplate

查看:5055
本文介绍了使用RestTemplate设置安全性cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用RestTemplate和Jackson json转换器调用Restful JSON服务。现在为了调用服务,我需要传入一个安全cookie。我可以通过使用URLConnection(见下面的代码)实现这一点。

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>

RestTemplate中的并行是什么?这里是一个代码片段,我一直在使用RestTemplate调用Restful服务:

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>

我不知道如何在使用RestTemplate调用服务时传递安全cookie。

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.

推荐答案

您可以访问底层的 HttpURLConnection RestTemplate 连接 RestTemplate springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/http/client/ClientHttpRequestFactory.htmlrel =nofollow> ClientHttpRequestFactory ,它允许您访问底层连接以设置头,属性等。 ClientHttpRequestFactory RestTemplate

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.

特别是,您可以扩展 SimpleClientHttpRequestFactory 并覆盖 prepareConnection()方法:

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

这篇关于使用RestTemplate设置安全性cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆