package net.java.glassfish.security.auth.realm.jdbc; import com.sun.enterprise.security.auth.login.PasswordLoginModule; import com.sun.enterprise.security.LoginException; /** * This class implement a JDBC Login module for Glassfish. The work is derivated from Sun's sample JDBC login module. * Enhancement has been done to use latest features. * sample setting in server.xml for JDBCLoginModule * @author Jean-Baptiste Bugeaud */ public class JDBCLoginModule extends PasswordLoginModule{ /** * Perform JDBC authentication. Delegates to JDBCRealm. * * @throws LoginException If login fails (JAAS login() behavior). * @todo replace I18N code with new one specific to JDBC */ protected void authenticate() throws LoginException { _logger.info("JDBC authenticating: " + _username); if (!(_currentRealm instanceof JDBCRealm)) { String msg = sm.getString("solarislm.badrealm"); throw new LoginException(msg); } final JDBCRealm jdbcRealm = (JDBCRealm) _currentRealm; // A JDBC user must have a name not null so check here. if ( (_username == null) || (_username.length() == 0) ) { String msg = sm.getString("solarislm.nulluser"); _logger.warning("JDBC Bad userName: " + _username); throw new LoginException(msg); } String[] grpList = jdbcRealm.authenticate(_username, _password); if (grpList == null) { // JAAS behavior String msg = sm.getString("solarislm.loginfail", _username); _logger.warning("JDBC No groups: " + _username); throw new LoginException(msg); } _logger.info("JDBC login succeeded for: " + _username+" groups:"+grpList); //make a copy of groupList to pass to LoginModule. This copy is the one // that will be made null there. DO NOT PASS the grpList as is - as // it will get overwritten. Resulting in logins passing only once. final String[] groupListToForward = new String[grpList.length]; System.arraycopy(grpList,0,groupListToForward,0,grpList.length); /* for (int i = 0; i< grpList.length; i++){ groupListToForward[i] = grpList[i]; }*/ commitAuthentication(_username, _password, _currentRealm, groupListToForward); jdbcRealm.setGroupNames(_username, groupListToForward); } }