Using Security Programmatically  Locate

This section describes how to write a client that uses the Security API. Note that the service (/Service1) must be deployed and configured for security, as described in Creating and Securing a Service. We will use the class from Example 3 as a Web service client. This code automatically authenticates as a test identity to a Web service it calls.

Example 3. Simple Web Service Client with Security

// Copyright 2002 Systinet Corp. All rights reserved.
// Use is subject to license terms.
package example.security;

import org.idoox.security.Credentials;
import org.idoox.wasp.WaspSecurity;
import org.systinet.wasp.webservice.ServiceClient;
import org.systinet.wasp.Wasp;

public class Client2 {
    public static void main(String[] args) throws Exception {
        // initializing libraries
        Wasp.init();

        // create a service client
        ServiceClient serviceClient = ServiceClient.create("http://localhost:6060/Service1");

        // acquire credentials using WaspSecurity
        Credentials credentials = WaspSecurity.acquireClientCredentials("test", "abcd", 
            "HttpBasic");

        // set credentials
        WaspSecurity.setCredentials(serviceClient, new Credentials[]{credentials});

        // set an initiating security provider
        WaspSecurity.setInitiatingProvider(serviceClient, "HttpBasic");

        // create a service proxy
        ServiceIface service = (ServiceIface) serviceClient.createProxy(ServiceIface.class);

        // do a call
        System.out.println(service.doIt("hello"));

        // uninitializing
        Wasp.destroy();
    }
}

To test this example, we will first need to compile it. Use the following command under Windows (On UNIX systems, change each backslash (\) to a forward slash (/).):

javac -d. example\security\Client2.java

To run this example use the following Windows command; on UNIX, change the config value to $WASP_HOME/conf/jaas.config.

Notice that the java.security.auth.login.config property must be present for the WSO2 SOA Enablement Server security framework to work properly.

java -Djava.security.auth.login.config=%WASP_HOME%\conf\jaas.config example.security.Client2

If the service has successfully been deployed, you see the Web service output, hello.

This section covers only a small part of WSO2 SOA Enablement Server Security API. The other chapters in this document cover all aspects of WSO2 SOA Enablement Server Security Framework in greater detail.