我正在尝试使用期望 WS-Security 和 WS-Adressing 标头作为 SOAPHeaders 的 SOAP Web 服务。与Java SOAP-WS 客户端恐惧类似,我无法在我的请求中获取所有这些标头。
我正在使用 CXF 并使用 cxf-codegen 插件生成了一个 Web 服务存根。我尝试使用 WSSA4J 拦截器添加 WSS 标头(基于此处的 cxf 文档):
//WS-Security
org.apache.cxf.endpoint.Endpoint cxfEndpoint = client.getEndpoint();
Map<String,Object> outProps = new HashMap<String,Object>();
outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
// Specify our username
outProps.put(WSHandlerConstants.USER, user.getUsername());
// Password type : plain text
outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
outProps.put("password", user.getPassword());
//Add the interceptor
WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
cxfEndpoint.getOutInterceptors().add(wssOut);
但该请求不包含任何 WS-Security 标头。
我在添加 WS-A 标头时遇到了同样的问题。基于此,我想添加一个 <To> 和 <ReplyTo> 标签:
//WS-A
Map<String, Object> reqCtx = client.getRequestContext();
AddressingProperties addrProperties = new AddressingProperties();
EndpointReferenceType endpointRef = new EndpointReferenceType();
AttributedURIType address = new AttributedURIType();
address.setValue("http://www.w3.org/2005/08/addressing/anonymous");
endpointRef.setAddress(address);
ReferenceParametersType refParams = new ReferenceParametersType();
refParams.getAny().add(new JAXBElement<String>(new QName("ServiceGroupId"), String.class, sessionId));
endpointRef.setReferenceParameters(refParams);
addrProperties.setReplyTo(endpointRef);
endpointRef = new EndpointReferenceType();
address.setValue(endpointUrl);
endpointRef.setAddress(address);
addrProperties.setTo(endpointRef);
reqCtx.put("javax.xml.ws.addressing.context", addrProperties);
我期待这样的事情:
<h:ReplyTo xmlns:h="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns="http://schemas.xmlsoap.org/ws/2004/08/addressing">
<Address>http://www.w3.org/2005/08/addressing/anonymous</Address>
<ReferenceParameters>
<ServiceGroupId>urn:uuid:8f27332c08854fb09989b2d</ServiceGroupId>
</ReferenceParameters>
</h:ReplyTo>
<h:To xmlns:h="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns="http://schemas.xmlsoap.org/ws/2004/08/addressing">
http://...
</h:To>
但同样,我的 SOAP 请求中没有任何内容。我错过了什么/做错了什么?
在这一点上,如果我可以直接修改 SOAP 标头并手动添加它们,我也会很高兴。我可以在使用拦截器发送消息之前修改标头吗?
我正在使用 CXF 3.1.2、wss4j 2.1.2、Java 1.8 和 Spring Boot 1.2.5。
谢谢!