dushkin
dushkin

Reputation: 2101

Adding security headers to request in CXF

I created a client based on a wsdl file.

I am using the following code to call a Web Service:

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

On the createParty call I accept "Object reference not set to an instance of an object" exception message.

This is probably because the web service was written with C#, and, most likely, it expects oasis headers, in which it store username and password values.

The enevelope which I could create by now is:

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

And the required enevelope is as follows:

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

How can I add those headers and properties to the request?

Upvotes: 1

Views: 5514

Answers (2)

Bzisch
Bzisch

Reputation: 101

You can set the UsernameToken like this. You can see implementation of PasswordCallback class 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);

Upvotes: 2

Colm O hEigeartaigh
Colm O hEigeartaigh

Reputation: 1900

You need to add the WSS4JOutInterceptor + configure it to add a UsernameToken to the request. See the section here called "Username Token Authentication":

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

Upvotes: 1

Related Questions