membersound
membersound

Reputation: 86627

Add credentials on outgoing soap messages in CXF?

I'm using CXF/JAXB to autogenerate java classes from wsdl. All of the generated requests extend BaseRequest, which can take the user credentials required for the soap auth:

CityRequest req = new CityRequest();
req.setUsername("admin");
req.setPassword("test");
req.setPostalCode("1234");

new WsService().getCityPort().getTown(req);

Now I'd like to somehow "intercept" the outgoing Request, and automatically add the required credentials. So that in my client implementation I don't have to care about setting the authentication stuff. Is it possible with CXF?

Then credentials are not provided as a header, but just as normal xml fields.

Upvotes: 0

Views: 459

Answers (1)

Patrick
Patrick

Reputation: 2122

You can use a CXF Interceptor for that, as long as it executes before the object is marshalled in the MARSHAL phase. You should be able to choose any phase before that one. See the complete list in the CXF Interceptors documentation.

In the Interceptor, you can find your outgoing object with something like the following code:

@Override
public void handleMessage(Message message) throws Fault {

    List mcl = message.getContent(List.class);
    if (mcl != null) {
        for (Object o : mcl) {
            if (o instanceof BaseRequest) {
                BaseRequest baseRequest = (BaseRequest) o;
            }
        }
    }

}

Upvotes: 1

Related Questions