java/sql-java-prihlasovani/src/cz/frantovo/jaas/sql/SQLLoginModul.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 07 Feb 2012 00:27:39 +0100
changeset 6 aff44e80f418
parent 4 c7d713d71ad3
permissions -rw-r--r--
Napojení na SQL databázi.
franta-hg@2
     1
package cz.frantovo.jaas.sql;
franta-hg@2
     2
franta-hg@2
     3
import com.sun.appserv.security.AppservPasswordLoginModule;
franta-hg@6
     4
import com.sun.enterprise.security.auth.realm.NoSuchUserException;
franta-hg@6
     5
import java.util.ArrayList;
franta-hg@4
     6
import java.util.Arrays;
franta-hg@6
     7
import java.util.Enumeration;
franta-hg@6
     8
import java.util.List;
franta-hg@4
     9
import java.util.logging.Level;
franta-hg@4
    10
import java.util.logging.Logger;
franta-hg@4
    11
import javax.security.auth.login.LoginException;
franta-hg@2
    12
franta-hg@2
    13
/**
franta-hg@4
    14
 * Přihlašovací modul pro SQL doménu.
franta-hg@6
    15
 *
franta-hg@6
    16
 * TODO: později bude potomkem
franta-hg@6
    17
 * <code>com.sun.appserv.security.AbstractLoginModule</code>
franta-hg@6
    18
 *
franta-hg@2
    19
 * @author fiki
franta-hg@2
    20
 */
franta-hg@2
    21
public class SQLLoginModul extends AppservPasswordLoginModule {
franta-hg@4
    22
franta-hg@6
    23
	/**
franta-hg@6
    24
	 * viz konfigurace v login.conf
franta-hg@6
    25
	 */
franta-hg@4
    26
	public static final String VÝCHOZÍ_JAAS_KONTEXT = "sqlRealm";
franta-hg@4
    27
	private static final Logger log = Logger.getLogger(SQLLoginModul.class.getName());
franta-hg@4
    28
franta-hg@4
    29
	@Override
franta-hg@4
    30
	protected void authenticateUser() throws LoginException {
franta-hg@6
    31
		if (_currentRealm instanceof SQLRealm) {
franta-hg@6
    32
			try {
franta-hg@6
    33
				SQLRealm sqlRealm = (SQLRealm) _currentRealm;
franta-hg@6
    34
				sqlRealm.ověřUživatele(_username, _passwd);
franta-hg@6
    35
								
franta-hg@6
    36
				String skupiny[] = poleSkupin(sqlRealm.getGroupNames(_username));
franta-hg@6
    37
				log.log(Level.INFO, "Uživatel {0} byl úspěšně ověřen a je členem těchto skupin: {1}", new Object[]{_username, Arrays.toString(skupiny)});
franta-hg@6
    38
				commitUserAuthentication(skupiny);
franta-hg@6
    39
			} catch (NoSuchUserException nue) {
franta-hg@6
    40
				log.log(Level.SEVERE, "Uživatele se podařilo ověřit, ale při pokusu o zjištění jeho skupin došlo k chybě. Uživatel: " + _username, nue);
franta-hg@6
    41
				throw new LoginException("Neexistující uživatel: " + _username);
franta-hg@6
    42
			}
franta-hg@4
    43
		} else {
franta-hg@4
    44
			throw new LoginException("Špatný realm: " + _currentRealm + " Očekávám: SQLRealm.");
franta-hg@4
    45
		}
franta-hg@4
    46
	}
franta-hg@6
    47
	
franta-hg@6
    48
	/**
franta-hg@6
    49
	 * Převede výčet na pole
franta-hg@6
    50
	 */
franta-hg@6
    51
	private static String[] poleSkupin(Enumeration<String> e) {
franta-hg@6
    52
		List<String> l = new ArrayList<>();
franta-hg@6
    53
		while (e.hasMoreElements()) {
franta-hg@6
    54
			l.add(e.nextElement());
franta-hg@6
    55
		}
franta-hg@6
    56
		return l.toArray(new String[0]);
franta-hg@6
    57
	}
franta-hg@2
    58
}