You can create a custom realm by providing a custom Java Authentication and Authorization Service (JAAS) login module class and a custom realm class. Note that client-side JAAS login modules are not suitable for use with the GlassFish Server.
To activate the custom login modules and realms, place the JAR files in the domain-dir/lib directory or the class files in the domain-dir/lib/classes directory. For more information about class loading in the GlassFish Server, see Chapter 2, Class Loaders.
JAAS is a set of APIs that enable services to authenticate and enforce access controls upon users. JAAS provides a pluggable and extensible framework for programmatic user authentication and authorization. JAAS is a core API and an underlying technology for Java EE security mechanisms. For more information about JAAS, refer to the JAAS specification for Java SDK, available at http://java.sun.com/javase/technologies/security/.
For general information about realms and login modules, see Working with Realms, Users, Groups, and Roles in The Java EE 6 Tutorial.
For Javadoc tool pages relevant to custom realms, go to https://glassfish.dev.java.net/nonav/docs/v3/api/ and click on the com.sun.appserv.security package.
Custom login modules must extend the com.sun.appserv.security.AppservPasswordLoginModule class. This class implements javax.security.auth.spi.LoginModule. Custom login modules must not implement LoginModule directly.
Custom login modules must provide an implementation for one abstract method defined in AppservPasswordLoginModule:
abstract protected void authenticateUser() throws LoginException
This method performs the actual authentication. The custom login module must not implement any of the other methods, such as login, logout, abort, commit, or initialize. Default implementations are provided in AppservPasswordLoginModule which hook into the GlassFish Server infrastructure.
The custom login module can access the following protected object fields, which it inherits from AppservPasswordLoginModule. These contain the user name and password of the user to be authenticated:
protected String _username; protected String _password;
The authenticateUser method must end with the following sequence:
String grpList; // populate grpList with the set of groups to which // _username belongs in this realm, if any commitUserAuthentication(_username, _password, _currentRealm, grpList);
public void init(Properties props) throws BadRealmException, NoSuchRealmException
This method is invoked during server startup when the realm is initially loaded. The props argument contains the properties defined for this realm. The realm can do any initialization it needs in this method. If the method returns without throwing an exception, the GlassFish Server assumes that the realm is ready to service authentication requests. If an exception is thrown, the realm is disabled.
public String getAuthType()
This method returns a descriptive string representing the type of authentication done by this realm.
public abstract Enumeration getGroupNames(String username) throws InvalidOperationException, NoSuchUserException
This method returns an Enumeration (of String objects) enumerating the groups (if any) to which the given username belongs in this realm.
Custom realms that manage users must implement the following additional methods:
public abstract boolean supportsUserManagement();
This method returns true if the realm supports user management.
public abstract Enumeration getGroupNames() throws BadRealmException;
This method returns an Enumeration of all group names.
public abstract Enumeration getUserNames() throws BadRealmException;
This method returns an Enumeration of all user names.
public abstract void refresh() throws BadRealmException;
This method refreshes the realm data so that new users and groups are visible.
public abstract void persist() throws BadRealmException;
This method persists the realm data to permanent storage.
public abstract User getUser(String name) throws NoSuchUserException, BadRealmException;
This method returns the information recorded about a particular named user.
public abstract void addUser(String name, String password, String groupList) throws BadRealmException, IASSecurityException;
This method adds a new user, who cannot already exist.
public abstract void removeUser(String name) throws NoSuchUserException, BadRealmException;
This method removes a user, who must exist.
public abstract void updateUser(String name, String newName, String password, String groups) throws NoSuchUserException, BadRealmException, IASSecurityException;
This method updates data for a user, who must exist.
The array passed to the commitUseAuthentication method should be newly created and otherwise unreferenced. This is because the group name array elements are set to null after authentication as part of cleanup. So the second time your custom realm executes it returns an array with null elements.
Ideally, your custom realm should not return member variables from the authenticate method. It should return local variables as the default JDBCRealm does. Your custom realm can create a local String array in its authenticate method, copy the values from the member variables, and return the String array. Or it can use clone on the member variables.