java/sql-java-prihlasovani/src/cz/frantovo/jaas/sql/SQLRealm.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 21 Jul 2011 23:40:45 +0200
changeset 4 c7d713d71ad3
parent 3 f08e57ab4480
child 6 aff44e80f418
permissions -rw-r--r--
Jde to zkompilovat a spustit. Umí přihlásit i nepřihlásit uživatele. K databázi se zatím nepřipojuje.
franta-hg@2
     1
package cz.frantovo.jaas.sql;
franta-hg@2
     2
franta-hg@2
     3
import com.sun.appserv.security.AppservRealm;
franta-hg@4
     4
import com.sun.enterprise.security.auth.realm.BadRealmException;
franta-hg@4
     5
import com.sun.enterprise.security.auth.realm.NoSuchRealmException;
franta-hg@2
     6
import com.sun.enterprise.security.auth.realm.NoSuchUserException;
franta-hg@2
     7
import java.util.Enumeration;
franta-hg@4
     8
import java.util.Properties;
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
 * Bezpečnostní doména. 
franta-hg@4
    15
 * Uživatelé jsou uloženi v SQL databázi.
franta-hg@2
    16
 * @author fiki
franta-hg@2
    17
 */
franta-hg@2
    18
public class SQLRealm extends AppservRealm {
franta-hg@2
    19
franta-hg@4
    20
	private static final String AUTH_TYPE = "Ověřuje uživatele proti SQL databázi.";
franta-hg@4
    21
	private static final Logger log = Logger.getLogger(SQLRealm.class.getName());
franta-hg@4
    22
franta-hg@2
    23
	@Override
franta-hg@4
    24
	public void init(Properties parametry) throws BadRealmException, NoSuchRealmException {
franta-hg@4
    25
		super.init(parametry);
franta-hg@4
    26
franta-hg@4
    27
		String jaasContext = parametry.getProperty(JAAS_CONTEXT_PARAM, SQLLoginModul.VÝCHOZÍ_JAAS_KONTEXT);
franta-hg@4
    28
		setProperty(JAAS_CONTEXT_PARAM, jaasContext);
franta-hg@4
    29
franta-hg@4
    30
		log.log(Level.INFO, "SQLRealm úspěšně vytvořen. JaasContext: {0}", jaasContext);
franta-hg@2
    31
	}
franta-hg@2
    32
franta-hg@2
    33
	@Override
franta-hg@4
    34
	public String getAuthType() {
franta-hg@4
    35
		return AUTH_TYPE;
franta-hg@4
    36
	}
franta-hg@4
    37
franta-hg@4
    38
	/**
franta-hg@4
    39
	 * @param uživatel přihlašovací jméno uživatele
franta-hg@4
    40
	 * @return seznam skupin, ve kterých se daný uživatel nachází
franta-hg@4
    41
	 * @throws NoSuchUserException když uživatel s tímto jménem neexistuje.
franta-hg@4
    42
	 */
franta-hg@4
    43
	@Override
franta-hg@4
    44
	public Enumeration getGroupNames(String uživatel) throws NoSuchUserException {
franta-hg@4
    45
		throw new NoSuchUserException("Metoda zatím není implementována. Uživatel: " + uživatel);
franta-hg@4
    46
	}
franta-hg@4
    47
franta-hg@4
    48
	/**
franta-hg@4
    49
	 * 
franta-hg@4
    50
	 * @param jméno uživatelské jméno
franta-hg@4
    51
	 * @param heslo heslo
franta-hg@4
    52
	 * @return seznam skupin, do kterých uživatel patří
franta-hg@4
    53
	 */
franta-hg@4
    54
	public String[] ověřUživatele(String jméno, char[] heslo) throws LoginException {
franta-hg@4
    55
franta-hg@4
    56
		// TODO: skutečně ověřovat heslo proti databázi
franta-hg@4
    57
franta-hg@4
    58
		if (heslo != null && heslo.length == 3) {
franta-hg@4
    59
			return new String[]{"bezny"};
franta-hg@4
    60
		} else {
franta-hg@4
    61
			throw new LoginException("Heslo musí mít tři znaky :-P");
franta-hg@4
    62
		}
franta-hg@2
    63
	}
franta-hg@2
    64
}