如何在soap请求中将会话设置为cookie

时间:2014-07-23 18:20:26

标签: spring-integration

我使用ws:outbound-gateway来调用soap web服务,该服务期望在cookie中设置会话ID。

我在上面ws-gateway的请求回调中得到了这个会话ID。

我试图丰富如下所示的标题,但这是添加soap标头而不是http标头。

<int:chain id="login.session.extractor.chain"
          input-channel="login.ws.out" output-channel="login.gateway.out">
          <cic:xml-multi-node-extractor
                 path-selector="${login.session.path}" />
          <int:header-enricher>
                 <int:header name="COOKIE" expression="'JSESSIONID=' + payload['${login.session.path}']" />
          </int:header-enricher>
          <int:transformer expression="payload['payload']" />
   </int:chain>

我希望在拨打电话之前在Cookie中设置会话ID,如下所示

DEBUG:>> "POST /api/v1/soap HTTP/1.1[\r][\n]"
DEBUG:>> "Accept-Encoding: gzip,deflate[\r][\n]"
DEBUG:>> "Content-Type: text/xml;charset=UTF-8[\r][\n]"
DEBUG:>> "SOAPAction: ""[\r][\n]"
DEBUG:>> "COOKIE: JSESSIONID=9F32328BDB000333E88AE8B7153B17FD.DC12BIZXSFAPI01[\r][\n]"
DEBUG:>> "User-Agent: Jakarta Commons-HttpClient/3.1[\r][\n]"
DEBUG:>> "Host: api5.demo.eu[\r][\n]"
DEBUG:>> "Content-Length: 232[\r][\n]"

2 个答案:

答案 0 :(得分:1)

我尝试使用拦截器 ws-outbound-gateway 的另一种方式,如下所示,这对我来说非常合适。我只是在寻找是否有任何配置方式来执行此操作而不是编写自定义代码。

public class HttpHeaderInterceptor implements ClientInterceptor {

    public boolean handleFault(MessageContext messageContext)
            throws WebServiceClientException {
        return true;
    }

    public boolean handleRequest(MessageContext messageContext)
            throws WebServiceClientException {
        TransportContext context = TransportContextHolder.getTransportContext();
        HttpUrlConnection connection = (HttpUrlConnection) context
                .getConnection();
        connection.getConnection().addRequestProperty("COOKIE",<custom value>);
        return true;
    }

    public boolean handleResponse(MessageContext messageContext)
            throws WebServiceClientException {
        return true;
    }

}

答案 1 :(得分:0)

幸运的是,您只能使用MessageSender的自定义<int-ws:outbound-gateway>来实现它:

public class CustomHttpComponentsMessageSender extends HttpComponentsMessageSender {

     @Override
     public WebServiceConnection createConnection(URI uri) throws IOException {
         String cookie = null;
         HttpComponentsConnection conn = (HttpComponentsConnection) super.createConnection(uri);
         HttpPost postMethod = conn.getHttpPost();
         cookie = "<Your Custom Cookie>";

         postMethod.addHeader("Cookie", cookie);

         return conn;
    }
}

其他自定义WebServiceMessageCallback可能会将message发送到ThreadLocal变量,以便从COOKIE处理CustomHttpComponentsMessageSender标题。