Secure Client Example

Error formatting macro: composition-setup: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil

The security framework establishes trust between members, and also authorizes cache operations from clients based on that trust. You establish trust by verifying credentials when one process connects to another. New members connect to the locator in a peer-to-peer topology, providing credentials to the locators. Clients connect to cache servers, providing credentials to the servers. One system connects to another in a multi-site system, using mutual authentication. Read more about security.

Running the Example

The example shows a very simple client/server configuration that uses security. The server starts on a port with security properties for client requests. The client does puts and gets on the server with valid PUT credentials. The client uses the valid LDAP username and password.

To run this example, you must have terminal sessions configured for the QuickStart examples, as described in Setting Up the Environment.
  1. In one session, start the server:
    $ java quickstart.SecurityServer ldap ou=ldapTesting,dc=pune,dc=gemstone,dc=com
    
  2. In the other session start the client:
    $ java quickstart.SecurityClient gemfire6 gemfire6
    
  3. Follow the instructions on the screens. When the client exits, press Enter to stop the server.

Example Listings

Program and cache configuration file listings for the client and the server:

Error formatting macro: deck: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
Error formatting macro: card: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
<?xml version="1.0"?>

<!-- SecurityServer.xml -->
     

<!DOCTYPE cache PUBLIC
  "-//GemStone Systems, Inc.//GemFire Declarative Caching 6.5//EN"
  "http://www.gemstone.com/dtd/cache6_5.dtd">
<cache>
  <cache-server port="40404" maximum-time-between-pings="20000"/>
  <region name="exampleRegion" refid="REPLICATE" />
</cache>
Error formatting macro: card: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
package quickstart;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.Region;

/**
 * In this example the server starts on a port with security properties for
 * client requests. Please refer to the quickstart guide for instructions on how
 * to run this example.
 * 
 * Add $GEMFIRE/lib/gfSecurityImpl.jar to your CLASSPATH before running this example.
 * 
 * @author GemStone Systems, Inc
 * 
 * @since 5.5
 */
public class SecurityServer {

  public static void main(String[] args) throws Exception {
    
    if (args.length != 2) {
      System.err.println("Usage: java  quickstart.SecurityServer <ldap-server> <ldap-basedn>");
      System.exit(1);
    }
    System.out
        .println("\nThis example demonstrates Security functionalities with ldap server.\n"
            + "This program is a server, listening on a port for client requests.\n"
            + "The client in this example is configured with security properties.");

    // Connect to the GemFire distributed system
    /*
     * User has to provide the ldap server and basedn inputs through the command line argument.
     * args[0] and args[1] are the values of ldap-server and ldap-basedn respectively.
     * example: SecurityServer ldap ou=ldapTesting,dc=pune,dc=gemstone,dc=com
     */
    System.out.println("Setting security properties for server");

    String ldapServer = args[0];
    String ldapBaseDn = args[1];
    
    System.out
        .println("\nConnecting to the distributed system and creating the cache.");

    // Create the cache which causes the cache-xml-file to be parsed
    Cache cache = new CacheFactory()
      .set("name", "SecurityServer")
      .set("cache-xml-file", "xml/SecurityServer.xml")
      .set("security-client-accessor",
           "templates.security.XmlAuthorization.create")
      .set("security-client-authenticator",
           "templates.security.LdapUserAuthenticator.create")
      .set("security-ldap-server", ldapServer)
      .set("security-ldap-basedn", ldapBaseDn)
      .set("security-authz-xml-uri", "xml/authz-ldap.xml")
      .create();

    // Get the exampleRegion
    Region exampleRegion = cache.getRegion("exampleRegion");
    System.out.println("Example region, " + exampleRegion.getFullPath()
        + ", created in cache.");

    System.out
        .println("\nPlease start the security client, and press Enter when the client finishes all the operations.\n");
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
        System.in));
    bufferedReader.readLine();

    // Close the cache and disconnect from GemFire distributed system
    System.out.println("Closing the cache and disconnecting.");
    cache.close();
    System.out.println("Closed the Server Cache");
  }
}
Error formatting macro: card: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
<?xml version="1.0"?>
<!-- SecurityClient.xml
     Configures a region as a client region in a Security cache. The 
     region's loader and writer connect to a server listening on port 40404.
-->
<!DOCTYPE client-cache PUBLIC
  "-//GemStone Systems, Inc.//GemFire Declarative Caching 6.5//EN"
  "http://www.gemstone.com/dtd/cache6_5.dtd">
<client-cache>
  <pool name="client" subscription-enabled="true">
    <server host="localhost" port="40404"/>
  </pool>
  <region name="exampleRegion" refid="PROXY"/>
</client-cache>
Error formatting macro: card: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
package quickstart;

import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.security.NotAuthorizedException;
import com.gemstone.gemfire.cache.client.*;

/**
 * In this example of secure client, the server listens on a port for client
 * requests and updates. The client does put and get on the server with valid
 * PUT credentials. This client uses the valid LDAP username and password.
 * Please refer to the quickstart guide for instructions on how to run this
 * example.
 * 
 * Add $GEMFIRE/lib/gfSecurityImpl.jar to your CLASSPATH before running this example.
 * 
 * @author GemStone Systems, Inc
 * 
 * @since 5.5
 */

public class SecurityClient {
  public static void main(String[] args) throws Exception {
    // Connect to the GemFire distributed system
    
    /*
     * User has to provide the username and password inputs through the command line argument.
     * args[0] and args[1] are the values of username and password respectively.
     * 
     * example: SecurityClient gemfire6 gemfire6
     */
    
    if (args.length != 2) {
      System.err.println("Usage: java  quickstart.SecurityClient <username> <password>");
      System.exit(1);
    }
    System.out.println("Setting security properties for client");
    
    String username = args[0];
    String password = args[1];
    
    System.out
        .println("\nConnecting to the distributed system and creating the cache.");

    // Create the cache which causes the cache-xml-file to be parsed
    ClientCache cache = new ClientCacheFactory()
      .set("name", "SecurityClient")
      .set("cache-xml-file", "xml/SecurityClient.xml")
      .set("security-client-auth-init",
           "templates.security.UserPasswordAuthInit.create")
      .set("security-username", username)
      .set("security-password", password)
      .create();

    // Get the exampleRegion
    Region exampleRegion = cache.getRegion("exampleRegion");
    if (exampleRegion == null) {
      System.out.println("The Region got is Null");
      return;
    }
    System.out.println("Example region, " + exampleRegion.getFullPath()
        + ", created in cache.");

    System.out.println("\nPutting three values in the cache...");
    Object key = null;
    Object value = null;
    for (int i = 1; i < 4; i++) {
      key = "key" + i;
      value = "value" + i;
      System.out.println("Putting entry: " + key + ", " + value);
      exampleRegion.put(key, value);
    }

    try {
      System.out.println("Getting entry: key1");
      exampleRegion.get("key1");
    }
    catch (ServerOperationException ex) {
      NotAuthorizedException naex = (NotAuthorizedException) ex.getCause();
      System.out.println("Get operation generated expected NotAuthorizedException: " + naex.getMessage());
    }
    
    // Close the cache and disconnect from GemFire distributed system
    cache.close();
    System.out.println("SecurityClient closed");
  }
}

Related Javadocs

Security package

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.