Accessing SSL enabled Web Services

<!– @page { size: 8.5in 11in; margin: 0.79in } P { margin-bottom: 0.08in } –>

This document will guide you in Consuming a Web Service over a HTTPS protocol.

I am using AXIS2 for deploying my Web Service and hence here I will present you with an example of making a client for a Web Service ( Version ) which comes deployed in AXIS2.

For security we usually deploy out Web Service over the HTTPS protocol. As you must be knowing HTTPS protocol uses Digital Certificate for the authentication and to ensure that no one else except the the authenticated parties may access/tamper the data.

Since in my previous post i:e: https://ravishbhupesh.wordpress.com/2008/05/06/enabling-ssl-on-jboss-2/ I described how to enable the JBOSS Application Server.

There we created a keystore ( which contains a certificate ).

Now we have to export the server certificate so that clients can use it. For exporting the certificate we will write

keytool ā€“ export -alias <keystore-alias-name> -keystore <keystore-name> -rfc -file <your-cert-name.cer>

then it will prompt for the password ( of the keystore ) : give it and your certificate will be generated by the name.

In my case I did :

keytool -export -alias nsdg_ssl -keystore nsdg.keystore -rfc -file nsdg_temp.cer

and a certificate by name nsdg_temp.cer was generated.

If you want to see the certificate you can write at the terminal :

cat you-cert-name.cer

If a client has to communicate to a server over SSL, it must have a truststore file. Below is given the command which will import the certificate to the client truststore :

keytool -import -alias <trust-store-name> -file <cert-file-name.cer> -keystore <client-trust-store>

FINALLY We will write a JAVA CLIENT which will call a web service.

To access a secure website from a web service client you need to set the following properties :

javax.net.ssl.trustStore and

javax.net.ssl.trustStorePassword

The value of javax.net.ssl.trustStore should be the path of your truststore file. You must have already imported the certificate to this truststore from th server to which you are going to communicate.

This is how i did :

System.setProperty(“javax.net.ssl.trustStore”,”E:\\Workplace\\workspace\\nsdgClientTrustStore”);

System.setProperty(“javax.net.ssl.trustStorePassword”, “rmi+ssl”);

My Client looked like :

import javax.xml.namespace.QName;

import javax.xml.stream.XMLOutputFactory;

import javax.xml.stream.XMLStreamWriter;

import org.apache.axiom.om.OMAbstractFactory;

import org.apache.axiom.om.OMElement;

import org.apache.axiom.om.OMFactory;

import org.apache.axiom.om.OMNamespace;

import org.apache.axis2.AxisFault;

import org.apache.axis2.addressing.EndpointReference;

import org.apache.axis2.client.*;

/**

* @author bhupesh

*/

public class VersionJavaClient {

private EndpointReference targetEPR;

private QName operationName = new QName(“version”);

public static void main(String[] args) throws AxisFault {

System.setProperty(“javax.net.ssl.trustStore”,

“E:\\Workplace\\workspace\\nsdgClientTrustStore”);

System.setProperty(“javax.net.ssl.trustStorePassword”, “rmi+ssl”);

VersionJavaClient client = new VersionJavaClient();

client.getAxisVersion();

}

private void getAxisVersion() throws AxisFault {

System.out.println(“getAxisVersion() STARTS”);

OMElement payload = createRequestMethod();

ServiceClient serviceClient = new ServiceClient();

Options options = new Options();

options.setTo(new EndpointReference(“https://202.141.151.147:8443/axis2/services/Version&#8221;));

options.setAction(“getVersion”);

serviceClient.setOptions(options);

OMElement result = serviceClient.sendReceive(payload);

System.out.println(“getAxisVersion() ENDS”);

if (result != null) {

System.out.println(“RESULT WILL BE DISPLAYED NOW”);

displayResult(result);

} else {

System.out.println(“Got Null Result”);

}}

private OMElement createRequestMethod() {

System.out.println(“createRequestMethod() :: creating envelope”);

OMFactory fac = OMAbstractFactory.getSOAP11Factory();

OMNamespace omNs = fac.createOMNamespace(“http://localhost/d2&#8221;, “d2”);

OMElement requestMethod = fac.createOMElement(“getVersion”, omNs);

OMElement firstElement = fac.createOMElement(“firstElement”, null);

requestMethod.addChild(firstElement);

//System.out.println(requestMethod.getText());

System.out.println(“createRequestMethod() :: request method returned..”);

return requestMethod;

}

private void displayResult(OMElement result) {

System.out.println(“displayResult() :: STARTS”);

try {

XMLStreamWriter writer = XMLOutputFactory.newInstance()

.createXMLStreamWriter(System.out);

//result.serialize(new OMOutputImpl(writer));

result.serialize(writer);

System.out.println(“trying to serialize the result. FLUSHING….”);

System.out.println(“displayResult() :: ENDS”);

writer.flush();

} catch (Exception e) {

System.out.println(“got Exception while printing”);

e.printStackTrace();

}}}

Advertisements