How to invoke a Secured JAX-WS web service from Standalone Java Client:

How to invoke a Secured JAX-WS web service from Standalone Java Client:

It is assumed that you already have a secured JAX-WS web service deployed and ready to invoke.

If not then please follow the following link to create a JAX-WS web service which is secured for username/password authentication.

As the Web service is already deployed let us assume that the WSDL URL of the web service to be invoked is as below:


Now in order to create a client for this web service first of all we have to generate the client side artifacts that will be done using the clientgen ant task of Weblogic server.

For detailed reference please see the below link:


1: Create a dir: BaseDir

2: Place the following build.xml file in the above BaseDir:


<project default=”all” basedir=”.”>

<!–Setting the Property Values–>

<property value=”${basedir}/Clientdir” />

<!– setting classpath for excecuting the client class –>

<path id=”client.class.path”>

<pathelement path=”${client.dir}”/>

<pathelement path=”${java.class.path}”/>


<!– building the standalone client –>

<target name=”build-client”>








<!–compiling the standalone client–>

<target name=”client-comp”>


srcdir=”${client.dir}” destdir=”${client.dir}”




<!– excecuting the compiled client class –>

<target name=”run”>




failonerror=”true” >

<classpath refid=”client.class.path”/>

<arg line=”

http://localhost:7001/Hello/HelloService?WSDL” />





3: Place the following Standalone java Client in the same BaseDir:


//This is a standalone client to invoke the JAX-WS webservice secured with username/password

import demo.HelloService;

import demo.Hello;

import weblogic.xml.crypto.wss.provider.CredentialProvider;


import weblogic.xml.crypto.wss.WSSecurityContext;


import java.util.List;

import java.util.ArrayList;

import java.util.Map;

public class MyClient{

public static void main(String args[]){

HelloService myService = new HelloService();

Hello myServicePort = myService.getHelloPort();

List<CredentialProvider> credProviders = new ArrayList<CredentialProvider>();

String username = “weblogic”;

String password = “weblogic”;

CredentialProvider cp = new ClientUNTCredentialProvider(username.getBytes(),password.getBytes());


Map<String,Object> rc = ((BindingProvider)myServicePort).getRequestContext();

rc.put(WSSecurityContext.CREDENTIAL_PROVIDER_LIST, credProviders);

System.out.println(“calling the web service”);

String result = myServicePort.sayHello(“sandeep”);

System.out.println(“called ” + result);




4: Now from a command prompt we have to run the ant task but before that we have to make sure that the Weblogic.jar and java class are in the classpath. The best way to set the classpath is to run the setDoaminEnv.cmd file on the command that is present in the Weblogic Domain Home / bin directory.

5: So from the command prompt we can run the following task from:

ant build-client

After successful generation of client artifacts we will compile the client class: :

ant client-comp

Then in order to execute the compiled class:

ant run

Here you can see that the Client has invoked the web service and you can see the message  on the server standard output:

Hello: Sandeep

Please follow and like us:

One Response

  1. Anirban May 14, 2011

Please add your comments here