DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Composing Custom Annotations in Spring
  • 10 Ways To Keep Your Java Application Safe and Secure
  • Using Swagger for Creating a PingFederate Admin API Java Wrapper
  • Build a Java Backend That Connects With Salesforce

Trending

  • Chaos Engineering Has a Blind Spot. Agentic AI Lives in It.
  • Stateless JWT Auth Microservice Architecture With Spring Boot 3 and Redis Sentinel
  • Slopsquatting: Building a Scanner That Catches AI-Hallucinated Packages Before They Reach Production
  • The Invisible OOMKill: Why Your Java Pod Keeps Restarting in Kubernetes
  1. DZone
  2. Coding
  3. Java
  4. CXF SOAP Header Basic Authentication Interceptors Java Without Spring

CXF SOAP Header Basic Authentication Interceptors Java Without Spring

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

By 
Bosco Ferrao user avatar
Bosco Ferrao
·
Feb. 16, 16 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
17.3K Views

Join the DZone community and get the full member experience.

Join For Free

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();
}
});
SOAP Web Protocols authentication Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Composing Custom Annotations in Spring
  • 10 Ways To Keep Your Java Application Safe and Secure
  • Using Swagger for Creating a PingFederate Admin API Java Wrapper
  • Build a Java Backend That Connects With Salesforce

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook