Spring WebServiceTemplate 为 Cookie 添加值 header

Spring WebServiceTemplate add value to Cookie header

我必须在我的应用程序发送给另一个应用程序的请求的 cookie 中添加信息, 但是好像没有添加正确

当我用 WireShark 检查请求时,我在 headers 中看到两个 Cookie headers :

POST /service HTTP/1.1
Accept-Encoding: gzip
Cookie: iam=**************************
Accept: text/xml, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
SOAPAction: ""
Content-Type: text/xml; charset=utf-8
Content-Length: 128393
Host: host-dev:9999
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.10 (Java/1.8.0_271)
Cookie: JSESSIONID=***********************
Authorization: Basic **************************

(我更改了一些信息)

在我的代码中我有这个:

@Service
public class ESignatureSoapConnector extends WebServiceGatewaySupport {

    private ObjectFactory objectFactory;

    @Autowired
    public ESignatureSoapConnector(ESignatureMarshaller marshaller, ConfigurationProperties configurationProperties) throws Exception { 
        this.setMarshaller(marshaller);
        this.setUnmarshaller(marshaller);
        this.setDefaultUri(configurationProperties.getBaseUrl());
        this.setMessageSender(buildMessageSender(configurationProperties.getUsername(), configurationProperties.getPassword()));

        this.objectFactory = new ObjectFactory();
    }

    public ESignatureResponse signDocument(MTOMFile file, String iamCookieValue) {
        ESignature request = new ESignature();
        request.setInputDocument(file);
        JAXBElement<ESignatureResponse> response = (JAXBElement<ESignatureResponse>) getWebServiceTemplate()
                .marshalSendAndReceive(objectFactory.createESignature(request), new WebServiceMessageCallback() {
                    @Override
                    public void doWithMessage(WebServiceMessage webServiceMessage) throws IOException, TransformerException {
                        TransportContext context = TransportContextHolder.getTransportContext();
                        HttpComponentsConnection connection = (HttpComponentsConnection) context.getConnection();
                        HttpPost post = connection.getHttpPost();
                        post.addHeader("Cookie", "iam=" + iamCookieValue);
                    }
                });
        return response.getValue();
    }

    private WebServiceMessageSender buildMessageSender(String username, String password) throws Exception {
        ...
    }

}

我假设我设置 cookie 的方式不正确,但我找不到正确的方法。 每个请求的 cookie 值都不同,这是一个 soap 请求,我在 Spring

工作

我们找到的解决方案:

JAXBElement<ESignatureResponse> response = (JAXBElement<ESignatureResponse>) getWebServiceTemplate()
                .marshalSendAndReceive(objectFactory.createESignature(request), new WebServiceMessageCallback() {
                    @Override
                    public void doWithMessage(WebServiceMessage webServiceMessage) throws IOException, TransformerException {
                        HttpClient httpClient = ((HttpComponentsMessageSender) getWebServiceTemplate().getMessageSenders()[0]).getHttpClient();
                        BasicClientCookie iamCookie = new BasicClientCookie(iamConfigurationProperties.getCookieName(), iamCookieValue);
                        iamCookie.setDomain(iamConfigurationProperties.getCookieDomain());
                        iamCookie.setPath(iamConfigurationProperties.getCookiePath());
                        ((DefaultHttpClient) httpClient).getCookieStore().addCookie(iamCookie);
                    }
                });