1

我创建了一个基于 wsdl 文件的客户端。

我正在使用以下代码调用 Web 服务:

    PrivateService ser = new PrivateService();

    PrivatePortType port = ser.getPrivateSOAPPort();
    BindingProvider bindingProvider = (BindingProvider) port;
    bindingProvider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
            "http://192.168.4.48/PrivateSimulator/PrivateWebService.svc");

    bindingProvider.getRequestContext().put(BindingProvider.SOAPACTION_USE_PROPERTY, true);
    bindingProvider.getRequestContext().put(BindingProvider.SOAPACTION_URI_PROPERTY, "CreateParty");

    CreatePartyRequest createReq = new CreatePartyRequest();
    createReq.setParam("123456");
    createReq.setCallback("http://192.168.5.106:9999/Service");

    try {
        CreatePartyResponse resp = port.createParty(createReq);

        PartyInfo ci = resp.getPartyInfo();
        System.out.println(ci.getPartyId());

    } catch (WSException e) {

        e.printStackTrace();
    }

在 createParty 调用中,我接受“对象引用未设置为对象的实例”异常消息。

这可能是因为 Web 服务是用 C# 编写的,并且很可能需要 oasis 标头,其中存储用户名和密码值。

我现在可以创建的信封是:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
    <CreatePartyRequest xmlns="http://teleParty.QQQ.com/CCGW">
        <param>10000903</param>
        <callback>http://192.168.5.106:9999/Service</callback>
    </CreatePartyRequest>
</soap:Body>
</soap:Envelope>

所需的信封如下:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<s:Header>
    <o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
        <o:UsernameToken u:Id="uuid-2aae5c05-19af-43e6-8814-7d7e7a306d4d-3">
            <o:Username>QQQ</o:Username>
            <o:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">QQQPass</o:Password>
        </o:UsernameToken>
    </o:Security>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <CreatePartyRequest xmlns="http://teleParty.QQQ.com/CCGW">
        <param>10000903</param>
        <callback>http://192.168.5.106:9999/Service</callback>
    </CreatePartyRequest>
</s:Body>
</s:Envelope>

如何将这些标头和属性添加到请求中?

4

2 回答 2

2

您可以像这样设置 UsernameToken。您可以在此处查看 PasswordCallback 类的实现。

public void setOutInterceptor(PrivatePortType port) {
    Map<String, Object> outProps = new HashMap<String, Object>();
    outProps.put("action", "UsernameToken");
    outProps.put("user", "QQQ");
    outProps.put("passwordType", "PasswordText");
    outProps.put("passwordCallbackClass","PasswordCallback");
    WSS4JOutInterceptor outInterceptor = new WSS4JOutInterceptor(outProps);
    Client client = ClientProxy.getClient(port);
    client.getOutInterceptors().add(outInterceptor);
    }


PrivateService ser = new PrivateService();
PrivatePortType port = ser.getPrivateSOAPPort();
setOutInterceptor(port);
于 2016-02-09T09:22:55.587 回答
1

您需要添加 WSS4JOutInterceptor + 配置它以将 UsernameToken 添加到请求中。请参阅此处称为“用户名令牌身份验证”的部分:

http://cxf.apache.org/docs/ws-security.html

于 2016-02-05T16:37:39.697 回答