Skip to content

MyLoginModule.java⚓︎

Overview⚓︎

The MyLoginModule class is a Java class that implements the LoginModule interface and provides methods for user authentication and authorization. It is designed to be used as part of a Java Authentication and Authorization Service (JAAS) implementation in a larger software project.

Table of Contents⚓︎

  1. Prerequisites
  2. Usage
  3. Methods
  4. Useful details

Prerequisites⚓︎

There are no external dependencies or prerequisites required to use the MyLoginModule class.

Usage⚓︎

To use the MyLoginModule class in a project, it must be instantiated and configured with a Subject and a CallbackHandler. After initialization, the login() method is called to perform user authentication, and the commit(), abort(), and logout() methods can be invoked as needed to manage the user's login session.

MyLoginModule loginModule = new MyLoginModule();
loginModule.initialize(subject, callbackHandler, sharedState, options);
if (loginModule.login()) {
    loginModule.commit();
} else {
    loginModule.abort();
}
loginModule.logout();

Methods⚓︎

  • initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options): Initializes the MyLoginModule with the provided Subject, CallbackHandler, and optional state and options.
  • login(): Performs user authentication by prompting for username and password using the CallbackHandler and validating the credentials.
  • commit(): Adds the authenticated user's Principal and associated Group to the Subject upon successful authentication.
  • abort(): Aborts the authentication process and logs out the user.
  • logout(): Logs out the user by removing the Principal and associated Group from the Subject.

Useful details⚓︎

  • The MyLoginModule class provides a simple example of user authentication and authorization using JAAS.
  • It demonstrates the use of the CallbackHandler to obtain user credentials during the login process.
  • The isValidUser(String username, String password) method can be customized to perform actual user authentication based on the project's requirements.
  • The commit() method adds a hardcoded role "SIE" to the authenticated user's Subject, which can be modified to add roles dynamically based on the user's permissions.