Friday, March 26, 2010

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.

http://weblogictips.wordpress.com/2010/03/25/how-secure-a-jax-ws-web-service-on-weblogic/

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

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

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:

http://download.oracle.com/docs/cd/E13222_01/wls/docs81/webserv/anttasks.html#1080160

Steps:


1: Create a dir: BaseDir

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

------------------------------------------BUILD.XML---------------------------------------------------------

<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}"/>

</path>

<!-- building the standalone client -->

<target name="build-client">

<clientgen

type="JAXWS"

wsdl="http://localhost:7001/Hello/HelloService?WSDL"

destDir="${client.dir}"

packageName=""

/>

</target>

<!--compiling the standalone client-->

<target name="client-comp">

<javac

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

classpath="${java.class.path};${client.dir}"

includes="MyClient.java"/>

</target>

<!-- excecuting the compiled client class -->

<target name="run">

<java

fork="true"

classname="MyClient"

failonerror="true" >

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

<arg line="

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

</java>

</target>

</project>

----------------------------------------------------------------------------------------------------------------------------

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

-----------------------------------------MyClient.java-----------------------------------------------------------------

//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.wsee.security.unt.ClientUNTCredentialProvider;

import weblogic.xml.crypto.wss.WSSecurityContext;

import javax.xml.ws.BindingProvider;

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());

credProviders.add(cp);

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: MyClient.java :

ant client-comp

Then in order to execute the compiled class:

ant run

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

Hello: Sandeep

1 comment:

Anirban said...

Hi Sandeep,
I am new to web Service Security. I have built a simple web service using top-down approach using JAX-WS and Weblogic 10.3 server. Now I want to secure my web service using WebLogic Admin Console and also write a test client to verify its security feature. Can you please help me as it urgent.

Thanks and Regards
Anirban Bhowmick