javax.security.auth.login
Class LoginContext

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by javax.security.auth.login.LoginContext

public class LoginContext
extends Object sample code for java.lang.Object definition code for java.lang.Object

The LoginContext class describes the basic methods used to authenticate Subjects and provides a way to develop an application independent of the underlying authentication technology. A Configuration specifies the authentication technology, or LoginModule, to be used with a particular application. Different LoginModules can be plugged in under an application without requiring any modifications to the application itself.

In addition to supporting pluggable authentication, this class also supports the notion of stacked authentication. Applications may be configured to use more than one LoginModule. For example, one could configure both a Kerberos LoginModule and a smart card LoginModule under an application.

A typical caller instantiates a LoginContext with a name and a CallbackHandler. LoginContext uses the name as the index into a Configuration to determine which LoginModules should be used, and which ones must succeed in order for the overall authentication to succeed. The CallbackHandler is passed to the underlying LoginModules so they may communicate and interact with users (prompting for a username and password via a graphical user interface, for example).

Once the caller has instantiated a LoginContext, it invokes the login method to authenticate a Subject. The login method invokes the configured modules to perform their respective types of authentication (username/password, smart card pin verification, etc.). Note that the LoginModules will not attempt authentication retries nor introduce delays if the authentication fails. Such tasks belong to the LoginContext caller.

If the login method returns without throwing an exception, then the overall authentication succeeded. The caller can then retrieve the newly authenticated Subject by invoking the getSubject method. Principals and Credentials associated with the Subject may be retrieved by invoking the Subject's respective getPrincipals, getPublicCredentials, and getPrivateCredentials methods.

To logout the Subject, the caller calls the logout method. As with the login method, this logout method invokes the logout method for the configured modules.

A LoginContext should not be used to authenticate more than one Subject. A separate LoginContext should be used to authenticate each different Subject.

The following documentation applies to all LoginContext constructors:

  1. Subject
  2. Configuration
  3. CallbackHandler

Note that Security Properties (such as auth.login.defaultCallbackHandler) can be set programmatically via the java.security.Security class, or statically in the Java security properties file located in the file named <JAVA_HOME>/lib/security/java.security, where <JAVA_HOME> refers to the directory where the JDK was installed.

See Also:
Security sample code for java.security.Security definition code for java.security.Security , AuthPermission sample code for javax.security.auth.AuthPermission definition code for javax.security.auth.AuthPermission , Subject sample code for javax.security.auth.Subject definition code for javax.security.auth.Subject , CallbackHandler sample code for javax.security.auth.callback.CallbackHandler definition code for javax.security.auth.callback.CallbackHandler , Configuration sample code for javax.security.auth.login.Configuration definition code for javax.security.auth.login.Configuration , LoginModule sample code for javax.security.auth.spi.LoginModule definition code for javax.security.auth.spi.LoginModule

Constructor Summary
LoginContext sample code for javax.security.auth.login.LoginContext.LoginContext(java.lang.String) definition code for javax.security.auth.login.LoginContext.LoginContext(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  name)
          Instantiate a new LoginContext object with a name.
LoginContext sample code for javax.security.auth.login.LoginContext.LoginContext(java.lang.String, javax.security.auth.callback.CallbackHandler) definition code for javax.security.auth.login.LoginContext.LoginContext(java.lang.String, javax.security.auth.callback.CallbackHandler) (String sample code for java.lang.String definition code for java.lang.String  name, CallbackHandler sample code for javax.security.auth.callback.CallbackHandler definition code for javax.security.auth.callback.CallbackHandler  callbackHandler)
          Instantiate a new LoginContext object with a name and a CallbackHandler object.
LoginContext sample code for javax.security.auth.login.LoginContext.LoginContext(java.lang.String, javax.security.auth.Subject) definition code for javax.security.auth.login.LoginContext.LoginContext(java.lang.String, javax.security.auth.Subject) (String sample code for java.lang.String definition code for java.lang.String  name, Subject sample code for javax.security.auth.Subject definition code for javax.security.auth.Subject  subject)
          Instantiate a new LoginContext object with a name and a Subject object.
LoginContext sample code for javax.security.auth.login.LoginContext.LoginContext(java.lang.String, javax.security.auth.Subject, javax.security.auth.callback.CallbackHandler) definition code for javax.security.auth.login.LoginContext.LoginContext(java.lang.String, javax.security.auth.Subject, javax.security.auth.callback.CallbackHandler) (String sample code for java.lang.String definition code for java.lang.String  name, Subject sample code for javax.security.auth.Subject definition code for javax.security.auth.Subject  subject, CallbackHandler sample code for javax.security.auth.callback.CallbackHandler definition code for javax.security.auth.callback.CallbackHandler  callbackHandler)
          Instantiate a new LoginContext object with a name, a Subject to be authenticated, and a CallbackHandler object.
LoginContext sample code for javax.security.auth.login.LoginContext.LoginContext(java.lang.String, javax.security.auth.Subject, javax.security.auth.callback.CallbackHandler, javax.security.auth.login.Configuration) definition code for javax.security.auth.login.LoginContext.LoginContext(java.lang.String, javax.security.auth.Subject, javax.security.auth.callback.CallbackHandler, javax.security.auth.login.Configuration) (String sample code for java.lang.String definition code for java.lang.String  name, Subject sample code for javax.security.auth.Subject definition code for javax.security.auth.Subject  subject, CallbackHandler sample code for javax.security.auth.callback.CallbackHandler definition code for javax.security.auth.callback.CallbackHandler  callbackHandler, Configuration sample code for javax.security.auth.login.Configuration definition code for javax.security.auth.login.Configuration  config)
          Instantiate a new LoginContext object with a name, a Subject to be authenticated, a CallbackHandler object, and a login Configuration.
 
Method Summary
 Subject sample code for javax.security.auth.Subject definition code for javax.security.auth.Subject getSubject sample code for javax.security.auth.login.LoginContext.getSubject() definition code for javax.security.auth.login.LoginContext.getSubject() ()
          Return the authenticated Subject.
 void login sample code for javax.security.auth.login.LoginContext.login() definition code for javax.security.auth.login.LoginContext.login() ()
          Perform the authentication.
 void logout sample code for javax.security.auth.login.LoginContext.logout() definition code for javax.security.auth.login.LoginContext.logout() ()
          Logout the Subject.
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Constructor Detail

LoginContext sample code for javax.security.auth.login.LoginContext(java.lang.String) definition code for javax.security.auth.login.LoginContext(java.lang.String)

public LoginContext(String sample code for java.lang.String definition code for java.lang.String  name)
             throws LoginException sample code for javax.security.auth.login.LoginException definition code for javax.security.auth.login.LoginException 
Instantiate a new LoginContext object with a name.

Parameters:
name - the name used as the index into the Configuration.
Throws:
LoginException sample code for javax.security.auth.login.LoginException definition code for javax.security.auth.login.LoginException - if the caller-specified name does not appear in the Configuration and there is no Configuration entry for "other", or if the auth.login.defaultCallbackHandler security property was set, but the implementation class could not be loaded.

SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if a SecurityManager is set and the caller does not have AuthPermission("createLoginContext.name"), or if a configuration entry for name does not exist and the caller does not additionally have AuthPermission("createLoginContext.other")

LoginContext sample code for javax.security.auth.login.LoginContext(java.lang.String, javax.security.auth.Subject) definition code for javax.security.auth.login.LoginContext(java.lang.String, javax.security.auth.Subject)

public LoginContext(String sample code for java.lang.String definition code for java.lang.String  name,
                    Subject sample code for javax.security.auth.Subject definition code for javax.security.auth.Subject  subject)
             throws LoginException sample code for javax.security.auth.login.LoginException definition code for javax.security.auth.login.LoginException 
Instantiate a new LoginContext object with a name and a Subject object.

Parameters:
name - the name used as the index into the Configuration.

subject - the Subject to authenticate.
Throws:
LoginException sample code for javax.security.auth.login.LoginException definition code for javax.security.auth.login.LoginException - if the caller-specified name does not appear in the Configuration and there is no Configuration entry for "other", if the caller-specified subject is null, or if the auth.login.defaultCallbackHandler security property was set, but the implementation class could not be loaded.

SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if a SecurityManager is set and the caller does not have AuthPermission("createLoginContext.name"), or if a configuration entry for name does not exist and the caller does not additionally have AuthPermission("createLoginContext.other")

LoginContext sample code for javax.security.auth.login.LoginContext(java.lang.String, javax.security.auth.callback.CallbackHandler) definition code for javax.security.auth.login.LoginContext(java.lang.String, javax.security.auth.callback.CallbackHandler)

public LoginContext(String sample code for java.lang.String definition code for java.lang.String  name,
                    CallbackHandler sample code for javax.security.auth.callback.CallbackHandler definition code for javax.security.auth.callback.CallbackHandler  callbackHandler)
             throws LoginException sample code for javax.security.auth.login.LoginException definition code for javax.security.auth.login.LoginException 
Instantiate a new LoginContext object with a name and a CallbackHandler object.

Parameters:
name - the name used as the index into the Configuration.

callbackHandler - the CallbackHandler object used by LoginModules to communicate with the user.
Throws:
LoginException sample code for javax.security.auth.login.LoginException definition code for javax.security.auth.login.LoginException - if the caller-specified name does not appear in the Configuration and there is no Configuration entry for "other", or if the caller-specified callbackHandler is null.

SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if a SecurityManager is set and the caller does not have AuthPermission("createLoginContext.name"), or if a configuration entry for name does not exist and the caller does not additionally have AuthPermission("createLoginContext.other")

LoginContext sample code for javax.security.auth.login.LoginContext(java.lang.String, javax.security.auth.Subject, javax.security.auth.callback.CallbackHandler) definition code for javax.security.auth.login.LoginContext(java.lang.String, javax.security.auth.Subject, javax.security.auth.callback.CallbackHandler)

public LoginContext(String sample code for java.lang.String definition code for java.lang.String  name,
                    Subject sample code for javax.security.auth.Subject definition code for javax.security.auth.Subject  subject,
                    CallbackHandler sample code for javax.security.auth.callback.CallbackHandler definition code for javax.security.auth.callback.CallbackHandler  callbackHandler)
             throws LoginException sample code for javax.security.auth.login.LoginException definition code for javax.security.auth.login.LoginException 
Instantiate a new LoginContext object with a name, a Subject to be authenticated, and a CallbackHandler object.

Parameters:
name - the name used as the index into the Configuration.

subject - the Subject to authenticate.

callbackHandler - the CallbackHandler object used by LoginModules to communicate with the user.
Throws:
LoginException sample code for javax.security.auth.login.LoginException definition code for javax.security.auth.login.LoginException - if the caller-specified name does not appear in the Configuration and there is no Configuration entry for "other", or if the caller-specified subject is null, or if the caller-specified callbackHandler is null.

SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if a SecurityManager is set and the caller does not have AuthPermission("createLoginContext.name"), or if a configuration entry for name does not exist and the caller does not additionally have AuthPermission("createLoginContext.other")

LoginContext sample code for javax.security.auth.login.LoginContext(java.lang.String, javax.security.auth.Subject, javax.security.auth.callback.CallbackHandler, javax.security.auth.login.Configuration) definition code for javax.security.auth.login.LoginContext(java.lang.String, javax.security.auth.Subject, javax.security.auth.callback.CallbackHandler, javax.security.auth.login.Configuration)

public LoginContext(String sample code for java.lang.String definition code for java.lang.String  name,
                    Subject sample code for javax.security.auth.Subject definition code for javax.security.auth.Subject  subject,
                    CallbackHandler sample code for javax.security.auth.callback.CallbackHandler definition code for javax.security.auth.callback.CallbackHandler  callbackHandler,
                    Configuration sample code for javax.security.auth.login.Configuration definition code for javax.security.auth.login.Configuration  config)
             throws LoginException sample code for javax.security.auth.login.LoginException definition code for javax.security.auth.login.LoginException 
Instantiate a new LoginContext object with a name, a Subject to be authenticated, a CallbackHandler object, and a login Configuration.

Parameters:
name - the name used as the index into the caller-specified Configuration.

subject - the Subject to authenticate, or null.

callbackHandler - the CallbackHandler object used by LoginModules to communicate with the user, or null.

config - the Configuration that lists the login modules to be called to perform the authentication, or null.
Throws:
LoginException sample code for javax.security.auth.login.LoginException definition code for javax.security.auth.login.LoginException - if the caller-specified name does not appear in the Configuration and there is no Configuration entry for "other".

SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if a SecurityManager is set, config is null, and either the caller does not have AuthPermission("createLoginContext.name"), or if a configuration entry for name does not exist and the caller does not additionally have AuthPermission("createLoginContext.other")
Since:
1.5
Method Detail

login sample code for javax.security.auth.login.LoginContext.login() definition code for javax.security.auth.login.LoginContext.login()

public void login()
           throws LoginException sample code for javax.security.auth.login.LoginException definition code for javax.security.auth.login.LoginException 
Perform the authentication.

This method invokes the login method for each LoginModule configured for the name specified to the LoginContext constructor, as determined by the login Configuration. Each LoginModule then performs its respective type of authentication (username/password, smart card pin verification, etc.).

This method completes a 2-phase authentication process by calling each configured LoginModule's commit method if the overall authentication succeeded (the relevant REQUIRED, REQUISITE, SUFFICIENT, and OPTIONAL LoginModules succeeded), or by calling each configured LoginModule's abort method if the overall authentication failed. If authentication succeeded, each successful LoginModule's commit method associates the relevant Principals and Credentials with the Subject. If authentication failed, each LoginModule's abort method removes/destroys any previously stored state.

If the commit phase of the authentication process fails, then the overall authentication fails and this method invokes the abort method for each configured LoginModule.

If the abort phase fails for any reason, then this method propagates the original exception thrown either during the login phase or the commit phase. In either case, the overall authentication fails.

In the case where multiple LoginModules fail, this method propagates the exception raised by the first LoginModule which failed.

Note that if this method enters the abort phase (either the login or commit phase failed), this method invokes all LoginModules configured for the application regardless of their respective Configuration flag parameters. Essentially this means that Requisite and Sufficient semantics are ignored during the abort phase. This guarantees that proper cleanup and state restoration can take place.

Throws:
LoginException sample code for javax.security.auth.login.LoginException definition code for javax.security.auth.login.LoginException - if the authentication fails.

logout sample code for javax.security.auth.login.LoginContext.logout() definition code for javax.security.auth.login.LoginContext.logout()

public void logout()
            throws LoginException sample code for javax.security.auth.login.LoginException definition code for javax.security.auth.login.LoginException 
Logout the Subject.

This method invokes the logout method for each LoginModule configured for this LoginContext. Each LoginModule performs its respective logout procedure which may include removing/destroying Principal and Credential information from the Subject and state cleanup.

Note that this method invokes all LoginModules configured for the application regardless of their respective Configuration flag parameters. Essentially this means that Requisite and Sufficient semantics are ignored for this method. This guarantees that proper cleanup and state restoration can take place.

Throws:
LoginException sample code for javax.security.auth.login.LoginException definition code for javax.security.auth.login.LoginException - if the logout fails.

getSubject sample code for javax.security.auth.login.LoginContext.getSubject() definition code for javax.security.auth.login.LoginContext.getSubject()

public Subject sample code for javax.security.auth.Subject definition code for javax.security.auth.Subject  getSubject()
Return the authenticated Subject.

Returns:
the authenticated Subject. If the caller specified a Subject to this LoginContext's constructor, this method returns the caller-specified Subject. If a Subject was not specified and authentication succeeds, this method returns the Subject instantiated and used for authentication by this LoginContext. If a Subject was not specified, and authentication fails or has not been attempted, this method returns null.