Page 3 of 6
With any of these approaches, note that you can change some of the JAAS-related system property settings (as well as many
other Java security settings) in the Java security properties file. This file, java.security, is located in the <jre-home>/lib/security directory and written in the standard Java properties file format.
Using JAAS authentication from your application typically involves the following steps:
LoginContextCallbackHandler to the LoginContext, for gathering or processing authentication data
LoginContext's login() method
Subject (assuming login succeeds)
Here's a minimal example:
LoginContext lc = new LoginContext("MyExample");
try {
lc.login();
} catch (LoginException) {
// Authentication failed.
}
// Authentication successful, we can now continue.
// We can use the returned Subject if we like.
Subject sub = lc.getSubject();
Subject.doAs(sub, new MyPrivilegedAction());
Underneath the covers, a few other things occur:
LoginContext finds the configuration entry "MyExample" in a JAAS configuration file (which you configured) to determine which LoginModules to load (see Figure 2)
LoginContext calls each LoginModule's login() method
login() method performs the authentication or enlists a CallbackHandlerCallbackHandler uses one or more Callbacks to interact with the user and gather input
Subject instance is populated with authentication details such as Principals and credentials
We'll explain further details below, but to begin, let's look at the key JAAS classes and interfaces involved in the process. These are typically divided into the following three groups:
| Common | Subject, Principal, credential (credential is not any specific class, but can be any object)
|
| Authentication | LoginContext, LoginModule, CallbackHandler, Callback
|
| Authorization | Policy, AuthPermission, PrivateCredentialPermission
|
Most of these classes and interfaces are in the javax.security.auth package's subpackages, with some prebuilt implementations in the com.sun.security.auth package, included only in J2SE 1.4.
Note: Because we focus on authentication in this article, we don't delve into the authorization classes.
The Subject class represents an authenticated entity: an end-user or administrator, or a Web service, device, or another process. The
class contains three sets of security information types:
Principals
Principals represent Subject identities. They implement the java.security.Principal interface (which predates JAAS) and java.io.Serializable. A Subject's most important method is getName(), which returns an identity's string name. Since a Subject instance contains an array of Principals, it can thus have multiple names. Because a social security number, login ID, email address, and so on, can all represent
one user, multiple identities prove common in the real world.
greatBy Anonymous on October 20, 2009, 1:50 amThis is a practically usable document; and not mere a collection of fancy .........
Reply | Read entire comment
Sun site - VagueBy Anonymous on September 12, 2009, 4:52 amI always think I'm the only one who thinks Sun tutorial is hard to understand. This is something Sun needs to look into. I specifically do not like to learn from...
Reply | Read entire comment
Is there any alternative to the configuration fileBy Anonymous on July 13, 2009, 8:48 amReally a very concrete and clear introduction to JAAS, however something that I do not like is to configure JAAS with an configuration file, is it the only way to...
Reply | Read entire comment
Great article..By Anonymous on July 10, 2009, 11:29 amThis was a great article and really helped shed some light on all of this! Is there another example on the Authorization part? Thanks so much!
Reply | Read entire comment
JAAS articleBy Anonymous on March 17, 2009, 11:34 amVery enlightening, especially given how vague the Sun site can be at times (e.g., it gives a trivial, hardcoded example of a user/pw and no clear idea how one would...
Reply | Read entire comment
View all comments