|
Part I: Java[tm] Authentication and Authorization Service (JAAS) Class Libraries and Methodsby Allen Lai(May 2002) We want to hear from you! Please send us your FEEDBACK. The following code sample may contain actual software programs in source code form. This source code is made available for developers to use as needed, pursuant to the terms and conditions of this license.
SECTION A - Short Description/SynopsisThe Java[tm] Authentication and Authorization Service (JAAS) API is a Java[tm] software package that enables a Java language programmer to write services to authenticate users and to enforce access control on resources that the application provides. It is a Java programming language implementation of the Pluggable Authentication Module (PAM) framework for authenticating users; it implements a variety of authentication methods. These authentication methods can be a username/password combination, biometric identification, or other operating systems' login mechanisms - for example, the UNIX username/password authentication mechanism available in the Solaris[tm] Operating Environment (OE) or other UNIX operating systems. It is a 100% pure Java platform implementation. Therefore, it can be used on all operating systems that have the Java 2 platform ported to them.This code sample illustrates the basic usage of the class libraries and methods of JAAS. Particularly for this article, which is Part I of a two-part series, we will look at the Authentication classes and methods to show an example of how one might use JAAS to authenticate a user. The core classes for the Authentication portion of JAAS are:
We will need to make a few assumptions for this article: Assumption #1 - The LoginModule
is implemented correctly and is configured properly for this Java application
program.
SECTION B - Task and Skeleton codeI. Task List1. Import the relevant Java 2 platform security and JAAS packages for performing Authentication.2. Instantiate a LoginContext with the CallbackHandler that is specifically implemented for this application. 3. Check that the instantiated LoginContext consults the configuration files and loads all the LoginModules for this application. 4. Invoke the login method on the LoginContext. If the login fails, an exception will be caught and the application will exit. If the login is successful, the Subject is retrieved using the getSubject method against the LoginContext. 5. Using the doAs method, invoke the actual application that is intended for execution using the Subject that was retrieved. 6. Exit the application with a System.exit
method call.
ii. Skeleton Code//import statements//----------------- //required by the Java 2 Principal package for serialization import java.io.*; //Java 2 interface to represent a Principal
//JAAS Authentication packages
public class Authenticate { public static void main(String[] args) { // Instantiate a LoginContext
which will consult the configuration for the system and load all of the
LoginModules
// Invoke the login
method on the LoginContext instance.
Using a try-catch block, catch the LoginException
// If the login is successful, retrieve the authenticated Subject from the LoginContext // Use the doAs method on the Subject to invoke the intended application with the Subject // Exit the Authenticate application } class ThisAppCallbackHandler implements CallbackHandler { //Actual implementation of the CallbackHandler for this application. } }
SECTION C - Solution Source//import statements//----------------- //required by the Java 2 Principal package for serialization import java.io.*; //Java 2 interface to represent a Principal
//JAAS Authentication packages
public class Authenticate { public static void main(String[] args) {
//Instantiates a LoginContext. It consults the configurations to load all
of the
LoginContext logincontext = null; logincontext = new LoginContext("Aunthenticate", new ThisAppCallbackHandler()); try {
//Using the login() method to begin the authentication process
} catch (LoginException error) {
System.out.println("The authentication was not successful due to " + error
+ " exception");
//Get the authenticated Subject if the login is successful
//Executing the application ExecuteAuthApplication() as the authenticated
Subject
//Exiting this authentication application after the ExecuteAuthAppilcation
application has started
} class ThisAppCallbackHandler implements CallbackHandler {
// The actual implementation of the CallbackHandler is not described as
this not the focus of
} } DOC ID #1198 | |||||||||||||||||||||||||||||||||||||||