How to Call a Secure SOAP Service with WS-Security with Apache Axis

Sample.java

import static java.lang.System.out;

import br.eti.fernandoribeiro.wsclient.sample.SamplePortType;
import br.eti.fernandoribeiro.wsclient.sample.SampleRequest;
import br.eti.fernandoribeiro.wsclient.sample.SampleResponse;
import br.eti.fernandoribeiro.wsclient.sample.SampleService;
import br.eti.fernandoribeiro.wsclient.sample.SampleServiceLocator;

public final class Sample {

  public static void main(final String[] args) throws Exception {
    final SampleService service = new SampleServiceLocator();

    final SamplePortType port = service.getSamplePort();

    final SampleResponse res = port.operation(new SampleRequest());

    out.println(res);
  }

}

client-config.wsdd

<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
  <globalConfiguration>
    <requestFlow>
      <handler type="java:org.apache.ws.axis.security.WSDoAllSender">
        <parameter name="action" value="UsernameToken" />
        <parameter name="passwordCallbackClass" value="SamplePasswordCallback" />
        <parameter name="passwordType" value="PasswordText" /> <!-- or PasswordDigest -->
        <parameter name="user" value="user" />
      </handler>
    </requestFlow>
  </globalConfiguration>
  <transport name="http" pivot="java:org.apache.axis.transport.http.HTTPSender" />
</deployment>

SamplePasswordCallback.java

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;

import org.apache.ws.security.WSPasswordCallback;

public final class SamplePasswordCallback implements CallbackHandler {

public void handle(final Callback[] callbacks) {

for (int i = 0; i < callbacks.length; i++) {

if (callbacks[i] instanceof WSPasswordCallback) {
final WSPasswordCallback cb = (WSPasswordCallback) callbacks[i];

cb.setPassword("password");
}

}

}

}[/sourcecode]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s