在我的soap应用程序中,我正在使用apache cxf。
这是我的代码,它将数据提交给服务器。
import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.bus.spring.SpringBusFactory;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.ws.security.wss4j.DefaultCryptoCoverageChecker;
import org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor;
import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
SpringBusFactory bf = new SpringBusFactory();
URL busFile = Submission.class.getResource(WSSEC_XML);
File f = new File(busFile.getPath());
Bus bus = bf.createBus(busFile.toString());
BusFactory.setDefaultBus(bus);
BusFactory.setThreadDefaultBus(bus);
DefaultCryptoCoverageChecker coverageChecker = new DefaultCryptoCoverageChecker();
coverageChecker.setSignBody(true);
coverageChecker.setSignTimestamp(true);
coverageChecker.setEncryptBody(true);
coverageChecker.setSignAddressingHeaders(true);
MyClaimservice service = new MyClaimservice();
Myclaims port = service.getMyClaimsSoap11();
BindingProvider provider = (BindingProvider) port;
provider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, ENDPOINT_ADDRESS);
provider.getRequestContext().put(BindingProvider.SOAPACTION_URI_PROPERTY, SOAP_ACTION);
Client client = ClientProxy.getClient(port);
client.getOutInterceptors().add(new LoggingOutInterceptor());
client.getInInterceptors().add(new LoggingInInterceptor());
client.getInInterceptors().add(new WSS4JInInterceptor(getInProps()));
client.getOutInterceptors().add(new WSS4JOutInterceptor(getOutProps()));
client.getInInterceptors().add(coverageChecker);
SubmissionResponse response = port.submission(request);
我的请求数据是这样形成的。
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
....
....
....
</soap:Envelope>
我需要为<soap:Envelope>
添加额外的命名空间。我的预期数据应该是这样的,
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:prefix1="url1" xmlns:prefix2="url2" xmlns:prefix3:"url3">
....
....
....
</soap:Envelope>
我尝试使用Map和XpathExpressions将这些前缀和命名空间URL添加到DefaultCryptoCoverageChecker
。但似乎它没有对我的请求数据进行任何更改。
Map<String, String> prefixes = new HashMap<String, String>();
prefixes.put("prefix1", "url1");
prefixes.put("prefix2", "url2");
prefixes.put("prefix3", "url3");
prefixes.put("prefix4", "url4");
prefixes.put("prefix5", "url5");
List<XPathExpression> xpaths = Arrays.asList(
new XPathExpression("//prefix1:Envelope", CoverageType.SIGNED),
new XPathExpression("//prefix2:Envelope", CoverageType.SIGNED),
new XPathExpression("//prefix3:Envelope", CoverageType.SIGNED),
new XPathExpression("//prefix4:Envelope", CoverageType.SIGNED),
new XPathExpression("//prefix5:Envelope", CoverageType.SIGNED));
DefaultCryptoCoverageChecker coverageChecker = new DefaultCryptoCoverageChecker();
coverageChecker.addPrefixes(prefixes);
coverageChecker.addXPaths(xpaths);
coverageChecker.setSignBody(true);
coverageChecker.setSignTimestamp(true);
coverageChecker.setEncryptBody(true);
coverageChecker.setSignAddressingHeaders(true);
非常感谢任何帮助。
更新2:
我尝试过Apache cxf的转换功能。
Map<String, String> outTransformMap = Collections.singletonMap("{http://schemas.xmlsoap.org/soap/envelope/}Envelope", "Envelope");
TransformOutInterceptor transformOutInterceptor = new TransformOutInterceptor();
transformOutInterceptor.setOutTransformElements(outTransformMap);
client.getOutInterceptors().add(transformOutInterceptor);
根据http://cxf.apache.org/docs/transformationfeature.html#TransformationFeature-JAX-WS,上面的代码将从Envelope
节点中删除该特定命名空间。我也可以通过查看xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/
<soap:Envelope>
来确认
我尝试将transformOutInterceptor.setOutTransformElements(outTransformMap);
替换为transformOutInterceptor.setOutAppendElements(outTransformMap);
,其中outTransformMap
现在包含Collections.singletonMap("{url1 (which I need to add)}Envelope", "Envelope");
。但它并没有将它作为新的命名空间附加到Envelope
。我在这里做错了什么..除了默认的namesapce之外,添加额外的命名空间到<soap:Envelope>
真的很困难。
答案 0 :(得分:4)
在挖掘cxf论坛后,我找到了答案。
Map<String, String> nsMap = new HashMap<>();
nsMap.put("prefix1", "url1");
nsMap.put("prefix2", "url2");
nsMap.put("prefix3", "url3");
nsMap.put("prefix4", "url4");
nsMap.put("prefix5", "http://www.w3.org/2001/04/xmlenc#");
Client client = ClientProxy.getClient(port);
client.getRequestContext().put("soap.env.ns.map", nsMap);
如果有人知道如何为<soap:Header>
答案 1 :(得分:1)
您可以添加以下标题:
UserCredentials authHeader = new UserCredentials();
authHeader.setUsername(username);
authHeader.setPassword(password);
ArrayList<Header> headers = new ArrayList<Header>(1);
try {
Header soapHeader = new Header(new QName(TQIntegrationV2.TQIntegrationV2Soap.getNamespaceURI(), "UserCredentials"), authHeader, new JAXBDataBinding(UserCredentials.class));
headers.add(soapHeader);
} catch (JAXBException ex) {
LOGGER.error("Exception trying to serialize header: {}", ex);
}
client.getRequestContext().put(Header.HEADER_LIST, headers);
如果你不想要,可以保留setUsername / setPassword。