我使用CXF的wsdl2java从WSDL生成了一堆客户端类。在执行以下操作时,如何将WS-Security添加到标题中:
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
个实例。
答案 0 :(得分:5)
通常是在代码之外完成的。
在这种情况下THIS可能有帮助
如果要以编程方式添加,
以编程方式将WS-Security UsernameToken标头添加到 轴绑定,非标准,但对快速测试很有用。 (存根/装订: 它是以_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);
}
答案 1 :(得分:0)
<jaxws:client id="client"
serviceClass="com.mycompany.TheServiceInterface"
address="http://fqdn/service/Endpoint?wsdl">
<jaxws:outInterceptors>
<bean class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor">
<constructor-arg>
<map>
<entry key="action" value="Timestamp UsernameToken"/>
<entry key="user" value="test.user"/>
<entry key="passwordType" value="PasswordNone"/>
</map>
</constructor-arg>
</bean>
</jaxws:outInterceptors>
</jaxws:client>
然后,如果您将客户端转换为BindingProvider
,则可以按编程方式更改端点。