gpt4 book ai didi

java - 无法使用 Spring 的 WebServiceTemplate 将 Http header 添加到消息中

转载 作者:行者123 更新时间:2023-12-05 00:40:27 26 4
gpt4 key购买 nike

我有一个相当简单的案例,我尝试将 HTTP header (不是 SOAP header )添加到我使用 Spring 的 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;
}

这就是我配置 SomeClient 的方式,它扩展了 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 header 。我也试过 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 header ?谢谢!

最佳答案

使用 HeadersAwareSenderWebServiceConnection接口(interface)而不是实际的底层连接。

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

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

要回答你做错了什么的问题是你投错了类(class)。是的,您正在使用的类已弃用,但它是您正在使用的库的一部分,您不能在不更改底层 HTTP 库的情况下转换为不同的类。

关于java - 无法使用 Spring 的 WebServiceTemplate 将 Http header 添加到消息中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48623512/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com