无法使用Spring的WebServiceTemplate将Http标头添加到消息中 [英] Cannot Add Http Headers to Message with Spring's WebServiceTemplate

查看:480
本文介绍了无法使用Spring的WebServiceTemplate将Http标头添加到消息中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当简单的情况,我尝试将HTTP标头(而不是SOAP标头)添加到使用Spring的WebServiceTemplate发出的请求中.

I have a fairly simple case where I am trying to add HTTP headers (not SOAP headers) to a request I am making using Spring's WebServiceTemplate.

我在其中定义了ClientInterceptor:

@Override
    public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {

        try {
            TransportContext context = TransportContextHolder.getTransportContext();
            HttpComponentsConnection connection = (HttpComponentsConnection) context.getConnection();
            connection.addRequestHeader("Authorization", String.format("Bearer %s", someAccessToken));
        } catch (IOException exception) {
            // Do nothing
        }

        return true;
        }

这是我配置扩展WebServiceConfigurationSupportSomeClient的方式:

This is how I configure my SomeClient which extends WebServiceConfigurationSupport:

@Bean
public SomeClient someClient() {

    ...

    SomeClientImpl service =  new SomeClientImpl();

    service.setObjectFactory(new com.path.ObjectFactory());
    service.setDefaultUri(someUri);
    service.setMarshaller(marshaller);
    service.setUnmarshaller(marshaller);
    service.setxStreamMarshaller(xStreamMarshaller);
    service.setInterceptors(new ClientInterceptor[]{wss4jSecurityInterceptor()});
    service.setMessageSender(new HttpComponentsMessageSender());
    service.setInterceptors(new ClientInterceptor[]{wss4jSecurityInterceptor(), addHttpHeaderInterceptor()});
    return service;
}

@Bean
public ClientInterceptor addHttpHeaderInterceptor() {
    return new AddHttpHeaderInterceptor(someAccessToken);
}

@Bean
public Wss4jSecurityInterceptor wss4jSecurityInterceptor() {

    Wss4jSecurityInterceptor interceptor = new Wss4jSecurityInterceptor();

    interceptor.setSecurementActions(securementAction);
    interceptor.setSecurementUsername(securementUsername);
    interceptor.setSecurementPassword(securementPassword);
    interceptor.setSecurementPasswordType(WSConstants.PW_TEXT);
    interceptor.setSecurementMustUnderstand(false);

    return interceptor;
}

但是未添加Authorization标头.我也尝试过使用CustomMessageCallback:

But the Authorization header is not being added. I have also tried with a CustomMessageCallback:

public class CustomMessageCallback implements WebServiceMessageCallback {
    private String headerKey;
    private String headerValue;

    public CustomMessageCallback(String headerKey, String headerValue) {
        this.headerKey = headerKey;
        this.headerValue = headerValue;
    }

    @Override
    public void doWithMessage(WebServiceMessage webServiceMessage) throws IOException, TransformerException {

        TransportContext context = TransportContextHolder.getTransportContext();
        HttpComponentsConnection conn = (HttpComponentsConnection) context.getConnection();

        HttpPost post = conn.getHttpPost();
        post.addHeader(headerKey, headerValue);
    }
}

但是它似乎还不能正常工作.我在做什么错,为什么没有添加Authorization标头?谢谢!

But it does not seem to work as well. What am I doing wrong, why the Authorization header is not being added? Thanks!

推荐答案

使用

Use the HeadersAwareSenderWebServiceConnection interface instead of the actual underlying connection.

TransportContext context = TransportContextHolder.getTransportContext();
HeadersAwareSenderWebServiceConnection connection = (HeadersAwareSenderWebServiceConnection) context.getConnection();
connection.addRequestHeader("Authorization", String.format("Bearer %s", "********"));

现在,如果您升级/切换HTTP库,则无需更改此代码.

Now if you upgrade/switch HTTP library you don't need to change this code.

要回答关于您做错了什么的问题,是您转换为错误的类.是的,您正在使用的类已被弃用,但它是您正在使用的库的一部分,您不能仅将其强制转换为其他类,而无需更改基础HTTP库.

To answer your question about what you are doing wrong is that you are casting to the wrong class. Yes the class you are using is deprecated but it is part of the library you are using, you cannot just cast to a different class without changing the underlying HTTP library.

这篇关于无法使用Spring的WebServiceTemplate将Http标头添加到消息中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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