4

我使用 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例子。

4

2 回答 2

8

通常它是在代码之外完成的。

在这种情况下,可能会有所帮助

如果您想以编程方式添加,

以编程方式将 WS-Security UsernameToken 标头添加到 Axis 绑定,非标准,但对快速测试很有用。(存根/绑定:它是以 _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);
 }
于 2013-01-15T06:50:47.903 回答
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>

然后,如果您将客户端转换为 a BindingProvider,您可以根据需要以编程方式更改端点。

于 2013-01-22T21:13:16.563 回答