我使用 CXF 的 wsdl2java 从 WSDL 生成了一堆客户端类。执行以下操作时如何将 WS-Security 添加到 header :
URL url = new URL("http://fqdn:8080/service/MessageHandler");
MessageHandlerService service = new MessageHandlerService(url);
MessageHandler handler = service.getMessageHandler();
MyMessage message = new MyMessage();
message.setSender("User 1");
handler.sendMessage(message);
我认为 handler
是一个 javax.xml.ws.Service
实例。
最佳答案
通常它是在代码之外完成的。
在那种情况下THIS可能有帮助
如果你想以编程方式添加,
Programmatically adding the WS-Security UsernameToken header to the Axis binding, non-standard, but useful for quick tests. (Stub/Binding: it's the class that ends with _PortType)
/**
* Adds WS-Security header with UsernameToken element to the Axis binding
* @param binding
* @param wsUser
* @param wsPass
* @throws SOAPException
*/
protected static void addWsSecurityHeader(org.apache.axis.client.Stub binding, String wsUser, String wsPass)
throws SOAPException {
// Create the top-level WS-Security SOAP header XML name.
QName headerName = new QName(
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security");
SOAPHeaderElement header = new SOAPHeaderElement(headerName);
// no intermediate actors are involved.
header.setActor(null);
// not important, "wsse" is standard
header.setPrefix("wsse");
header.setMustUnderstand(true);
// Add the UsernameToken element to the WS-Security header
SOAPElement utElem = header.addChildElement("UsernameToken");
SOAPElement userNameElem = utElem.addChildElement("Username");
userNameElem.setValue(wsUser);
SOAPElement passwordElem = utElem.addChildElement("Password");
passwordElem.setValue(wsPass);
// Finally, attach the header to the binding.
binding.setHeader(header);
}
关于java - 将 ws-security 添加到 wsdl2java 生成的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14266237/