在CXF中添加安全标头以请求

时间:2016-02-04 14:11:05

标签: java web-services cxf cxf-client

我基于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>

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

您可以像这样设置UsernameToken。您可以看到PasswordCallback类here的实现。

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