Skip Headers

Oracle9iAS Containers for J2EE Services Guide
Release 2 (9.0.2)

Part Number A95879-01
Go To Documentation Library
Go To Product List
Solution Area
Go To Table Of Contents
Go To Index

Go to previous page Go to next page

Developing Secure J2SE Applications

This chapter describes how to develop secure Java2 Platform, Standard Edition (J2SE) applications using the Oracle9iAS Containers for J2EE (OC4J) JAAS Provider.

This chapter contains these topics:

Developing Secure J2SE Applications Overview

J2SE application developers develop, deploy, and manage Java applications on local desktops or servers. Using the JAAS provider enables developers to make these applications secure.

After the creation of realms and related components described in Chapter 6, "Integrating the JAAS Provider with Java2 Applications", the JAAS provider can be integrated into J2SE applications to provide the following services:

Authentication in the J2SE Environment

Authentication is the process of verifying the identity of a user in a computing system, often as a prerequisite to granting access to resources in a system. User authentication in the J2SE environment is performed with the following:

The constructor for the LoginContext class requires the name of the client login and a new instance of a callback handler, an object you must implement. The callback handlers, which are described in JAAS documentation, are required by the login module to communicate with users.

The user of the computing service is the Subject. The Subject is passed to the LoginContext class. The LoginContext.login() method compares the Subject to configuration settings in the JAAS Provider RealmLoginModule or other login module. If login() is successful, the login module associates the Principal (a specific identity) and credentials with the Subject.

This authenticates the Subject, which can then be retrieved by invoking LoginContext.getSubject in the authorization process.

See Also:

JAAS documentation at the following Web site for more information about authentication, login modules, and callback handlers:

Authorization in the J2SE Environment

Once a user is successfully authenticated, the authorization policy is enforced upon the user. Authorization is achieved through the following methods and interface based on the Java2 and JAAS Security Model:


After retrieving the authenticated Subject from the LoginContext, the client invokes Subject.doAs with the application as a parameter. The application starts, which activates security checking in the server. An AccessControlException is thrown if security checking fails.


Security checking in J2SE applications requires the use of the JDK 1.3 or greater java.lang.SecurityManager in the server.

The security manager determines whether to permit operations. The classes in Java packages cooperate with the security manager by asking the application's security manager for permission to perform certain operations. Each Java application can have its own security manager object that acts as a full-time security guard.

The SecurityManager.checkPermission method performs security checking.


The application must implement the interface PrivilegedAction.

See Also:

Java security architecture at the following Web site:

Testing and Executing an Application

In order to test or execute the application, you must start the SecurityManager at the command line and, if using a login module to start an application, call it.

This is the first real test of the JAAS provider.


The security manager is automatically started in JAAS provider-based J2EE applications.

Starting With RealmLoginModule

To start the application using the RealmLoginModule:
  1. Go to the computer on which the J2SE application is installed.

  2. Start the security manager and test the application at the command prompt:

    java MyApp

    where the client, MyApp, calls your application. The jazn.xml file is the property file that identifies the provider type you are using (Oracle Internet Directory or XML-Based Provider Type). The jaas.config file indicates that RealmLoginModule is required for authentication.

    This command can be used with the sample code shown in "Sample J2SE Application".

Start Without Using RealmLoginModule

It is possible to start J2SE applications without using authentication and the RealmLoginModule or any login module, but that is not the preferred method. To do so and use the sample code provided in this chapter, you need to modify the MyApp code in Example 8-1, "Client Login Code" so that it does not require the objects described in "Authentication in the J2SE Environment".

After you have modified the MyApp code, you can start it.

To start the application without using the RealmLoginModule:
  1. Go to the computer that the J2SE application is installed on.

  2. Start the security manager and execute the application at the command prompt:

    For example, to test a sample application, enter:

    java MyApp

    where the client, MyApp, calls your application. The type of JAAS provider you are using (LDAP-based or XML-based) is identified in the jazn.xml file.

Sample J2SE Application

This section shows a sample client login, MyApp, and a brief test application using the JAAS provider in a J2SE environment.

Table 8-1 Sample Client Login Code
Objects Names Comments



myCallbackHandler is a callback handler that you must implement.

sample application


AccessTest1 is the application that the user wants to start. The code for AccessTest1 is show in Example 8-2.

sample external realm


abcRealm was created in Example 7-1.

client user

Jane.Smith or unknown

The client user added in Example 7-1. Since Jane.Smith is the only user added; that is, the only name returned to Principal p.

The following is executed using the commands described in "Testing and Executing an Application".

Example 8-1 Client Login Code

MyApp Code

import java.util.*;


public class MyApp {

    public static void main(String[] args) {

	LoginContext lc = null;
	try {
	    // you must create a CallbackHandler class 		
    lc = new LoginContext("MyApp", new myCallbackHandler());
    	} catch (LoginException le) {

	try {
	    // attempt authentication
	} catch (AccountExpiredException aee) {	   
	    System.out.println("Your account has expired.  " +
			       "Please notify your administrator.");

 // other exceptions
	 // FailedLoginException 

	// checking what Principals the user has
	Iterator principalIterator = lc.getSubject().getPrincipals().iterator();
	System.out.println("Authenticated user has the following Principals:");
	while (principalIterator.hasNext()) {
	    Principal p = (Principal);
	    System.out.println("\t" + p.toString());
	System.out.println("User has " +
			   lc.getSubject().getPublicCredentials().size() +
			   " Public Credential(s)");
	// now try to execute the sample application as the authenticated Subject
	Subject.doAs(lc.getSubject(), new AccessTest1());



Sample J2SE Application Code

This is the sample application that is executed when a successfully authenticated principal runs MyApp.

Table 8-2 Objects in Sample Application Code
Objects Names


Example 8-2 Sample Application Code

import java.lang.*;

public class AccessTest1 implements PrivilegedAction {

    public Object run() {

        File f = new File("");

        // Security checking is invoked
        if (f.exists()) {
            System.out.println("*** accessed ***");
        return null;

Discussion of the J2SE Sample Client Login and Application Code

In the MyApp client, once the authentication process is completed, Subject.doAs starts the sample application AccessTest1.

AccessTest1 starts and requests to read the file. This request invokes security checking in the server, which determines if the user has permission on AccessTest1 to read the file.

Permission has been granted previously to Jane.Smith in Example 7-1. If Jane.Smith is the user logging in, AccessTest1 runs.

If the user is not Jane.Smith, the authorization fails because no other users have been granted this permission.

Go to previous page Go to next page
Copyright © 2002 Oracle Corporation.

All Rights Reserved.
Go To Documentation Library
Go To Product List
Solution Area
Go To Table Of Contents
Go To Index