How to invoke a JAX-WS web service secured with username/password with another JAX-WS web

Here it is assumed that you already have a JAX-WS web service deployed which requires username/password to get invoked.

If not then please follow the below link:

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

Let us assume that the web service WSDL is located at URL:

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

Now In order to invoke this web service we will be creating another JAX-WS web service as mentioned below:

Here

———————————JaxWSClient.java-————————————————————-

package sandeep;

import javax.jws.WebService;

import javax.jws.WebMethod;

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;

// Import the JAX-WS Stubs for invoking the Hello Web Service.

// Stubs generated by clientgen

import demo.HelloService;

import demo.Hello;

@WebService

public class JaxWSClient {

@WebMethod()

public String callJAXWS()

{

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

return result;

}

}

————————————————————————————————————————————-

First let us look at the imports statements used in the above java class:

Below  imports are required to use the Weblogic specific annotations to make this class as a web service class and to create the username token provider(weblogic.wsee.security.unt.ClientUNTCredentialProvider) to be added to the generated port.

import javax.jws.WebService;

import javax.jws.WebMethod;

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;

below imports are used to get the web service object and the port object:

import demo.HelloService;

import demo.Hello;

Now after creating the above Class we will use the below JWSC  ant of Weblogic to make the web service artifacts from the above class:

<!– building the JAXWS Client –>

<target name=”JAXWS-build-client-service”>

<mkdir dir=”${JAXWSClient.ear.dir}”/>

<jwsc

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

destdir=”${JAXWSClient.ear.dir}” >

<jws

file=”JaxWSClient.java”>

<clientgen

type=”JAXWS”

wsdl=”http://137.72.242.156:7001/Hello/HelloService?WSDL

packageName=”demo” />

</jws>

</jwsc>

</target>

——————————–

You can notice that we have used the clientgen child ant task within the JWSC ant task here. This is the difference between the standalone client and a JAX-WS client.

The complete build.xml file for generating the complete JAX-WS client and deploying it on the Weblogic server is given below:

———————————————-build.xml——————————————————————–

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

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

<property name=”JAXWSClient.ear.dir” value=”${basedir}/JAXWS-client-ear” />

<property value=”weblogic” />

<property name=”wls.password” value=”weblogic” />

<property name=”wls.hostname” value=”localhost” />

<property name=”wls.port” value=”7001″ />

<property name=”wls.server.name” value=”AdminServer” />

<!– building the JAXWS Client –>

<target name=”JAXWS-build-client-service”>

<mkdir dir=”${JAXWSClient.ear.dir}”/>

<jwsc

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

destdir=”${JAXWSClient.ear.dir}” >

<jws

file=”JaxWSClient.java”>

<clientgen

type=”JAXWS”

wsdl=”http://137.72.242.156:7001/Hello/HelloService?WSDL

packageName=”demo” />

</jws>

</jwsc>

</target>

<target>

<javac

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

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

includes=”JaxWSClient.java”/>

</target>

<!– deploying the client web service –>

<target name=”JAXWSdeploy”>

<wldeploy

action=”deploy”

verbose=”true”

failonerror=”true”

name=”JAXWSClientEar”

source=”${JAXWSClient.ear.dir}”

user=”${wls.username}”

password=”${wls.password}”

adminurl=”t3://${wls.hostname}:${wls.port}”

targets=”${wls.server.name}” />

</target>

</project>

——————————————————————————————————————————

Steps to follow:

1: Make sure that the JAX-WS web service that is to be called is active and accessible.

Assume that the WSDL URL for that web service is : http://137.72.242.156:7001/Hello/HelloService?WSDL

2: Create a dir :  sample and place the above mentioned build.xml file in this sample dir:

3: create another dir inside sample dir : JAXWSWebServiceClient and place the JaxWSClient.java class in this dir:

4: Now open a command prompt and run the setDomainEnv.cmd file on this command prompt to set the necessary classpath and other environment variables. setDomainEnv.cmd file is available in the domain_home/bin dir:

5: Then on the command prompt opened above move to the sample dir created above:

6: from the command prompt run the following task:

ant JAXWS-build-client-service

this task is going to build the web service war for the current java class and the clientgen child ant task used within this task is going to create the necessary client artifacts from the WSDL url:

7: After successful completion of the above task run the below task to deploy the created war file:

ant JAXWSdeploy

8: After the deployment is successful you can log in to the Weblogic Admin Server Console and go to the Deployments Summary to see the deployed Web Service.

9: When you will open the Weblogic Web Service Client for this deployed Web Service with name: JAXWSClientEar then you will see the below window:

10: Just Clicking on the callJAXWS button available on the Weblogic web service Client window will invoke this client web service which in turn will call the Secured Web Service and you can see the below output on the Weblogic Server Standard output:

Please follow and like us:

No Responses

Please add your comments here