Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

CXF SOAP Header Basic Authentication Interceptors Java Without Spring

DZone's Guide to

CXF SOAP Header Basic Authentication Interceptors Java Without Spring

Here's a great guide to authentication interceptors in Java without using Spring!

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

If you have used the tool:

org.apache.cxf.tools.wsdlto.WSDLToJava

to create your SOAP CXF stubs.

Create a Basic Authentication Interceptor as follows:

package com.bos.services.cxf.interceptors;

import org.apache.cxf.interceptor.AbstractOutDatabindingInterceptor;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.Phase;
import org.apache.cxf.ws.security.SecurityConstants;

public abstract class CxfBasicAuthInterceptor extends AbstractOutDatabindingInterceptor {

public CxfBasicAuthInterceptor() {
super(Phase.WRITE);
}

@Override
public void handleMessage(Message outMessage) throws Fault {
outMessage.setContextualProperty(SecurityConstants.USERNAME, getUsername());
outMessage.setContextualProperty(SecurityConstants.PASSWORD, getPassword());
}

public abstract String getUsername();

public abstract String getPassword();
}

To use the Basic Authetication Interceptor:

QName serviceName = new QName("http://bos.asoap.service.com/v1.0/ServiceApi", "BosWS");
URL wsdlURL = BosWS.WSDL_LOCATION;

BosWS ss = new BosWS(wsdlURL, serviceName);
IBosWS service = ss.getBasicHttpBindingIBosWS();
Client client = ClientProxy.getClient(service);

client.getOutInterceptors().add(new CxfBasicAuthInterceptor() {
@Override
public String getUsername() {
return configService.getWSUserName();
}

@Override
public String getPassword() {
return configService.getWSPassword();
}
});

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
cxf ,java 1.7 ,soap header ,basic authentication

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}