CXF SOAP Header Basic Authentication Interceptors Java Without Spring
Here's a great guide to authentication interceptors in Java without using Spring!
Join the DZone community and get the full member experience.
Join For FreeIf 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);
}
โ
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() {
public String getUsername() {
return configService.getWSUserName();
}
โ
public String getPassword() {
return configService.getWSPassword();
}
});
Opinions expressed by DZone contributors are their own.
Comments