java 将 ws-security 添加到 wsdl2java 生成的类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14266237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:41:08  来源:igfitidea点击:

Adding ws-security to wsdl2java generated classes

javasoapcxfws-security

提问by Mike Thomsen

I generated a bunch of client classes from a WSDL with CXF's wsdl2java. How do I add WS-Security to the header when doing something like this:

我使用 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);

I think handleris a javax.xml.ws.Serviceinstance.

我认为handler是一个javax.xml.ws.Service例子。

采纳答案by Mike Thomsen

<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>

Then if you cast the client to a BindingProvideryou can change the endpoint as needed programatically.

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

回答by TheWhiteRabbit

Usually it's done outside the code .

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

In that case THISmight help

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

If you want to add programatically ,

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

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)

以编程方式将 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);
 }