java/sql-java-prihlasovani/src/cz/frantovo/jaas/sql/SQLRealm.java
changeset 4 c7d713d71ad3
parent 3 f08e57ab4480
child 6 aff44e80f418
     1.1 --- a/java/sql-java-prihlasovani/src/cz/frantovo/jaas/sql/SQLRealm.java	Thu Jul 21 20:35:23 2011 +0200
     1.2 +++ b/java/sql-java-prihlasovani/src/cz/frantovo/jaas/sql/SQLRealm.java	Thu Jul 21 23:40:45 2011 +0200
     1.3 @@ -1,22 +1,64 @@
     1.4  package cz.frantovo.jaas.sql;
     1.5  
     1.6  import com.sun.appserv.security.AppservRealm;
     1.7 -import com.sun.enterprise.security.auth.realm.InvalidOperationException;
     1.8 +import com.sun.enterprise.security.auth.realm.BadRealmException;
     1.9 +import com.sun.enterprise.security.auth.realm.NoSuchRealmException;
    1.10  import com.sun.enterprise.security.auth.realm.NoSuchUserException;
    1.11  import java.util.Enumeration;
    1.12 +import java.util.Properties;
    1.13 +import java.util.logging.Level;
    1.14 +import java.util.logging.Logger;
    1.15 +import javax.security.auth.login.LoginException;
    1.16  
    1.17  /**
    1.18 + * Bezpečnostní doména. 
    1.19 + * Uživatelé jsou uloženi v SQL databázi.
    1.20   * @author fiki
    1.21   */
    1.22  public class SQLRealm extends AppservRealm {
    1.23  
    1.24 +	private static final String AUTH_TYPE = "Ověřuje uživatele proti SQL databázi.";
    1.25 +	private static final Logger log = Logger.getLogger(SQLRealm.class.getName());
    1.26 +
    1.27  	@Override
    1.28 -	public String getAuthType() {
    1.29 -		throw new UnsupportedOperationException("Not supported yet.");
    1.30 +	public void init(Properties parametry) throws BadRealmException, NoSuchRealmException {
    1.31 +		super.init(parametry);
    1.32 +
    1.33 +		String jaasContext = parametry.getProperty(JAAS_CONTEXT_PARAM, SQLLoginModul.VÝCHOZÍ_JAAS_KONTEXT);
    1.34 +		setProperty(JAAS_CONTEXT_PARAM, jaasContext);
    1.35 +
    1.36 +		log.log(Level.INFO, "SQLRealm úspěšně vytvořen. JaasContext: {0}", jaasContext);
    1.37  	}
    1.38  
    1.39  	@Override
    1.40 -	public Enumeration getGroupNames(String string) throws InvalidOperationException, NoSuchUserException {
    1.41 -		throw new UnsupportedOperationException("Not supported yet.");
    1.42 +	public String getAuthType() {
    1.43 +		return AUTH_TYPE;
    1.44 +	}
    1.45 +
    1.46 +	/**
    1.47 +	 * @param uživatel přihlašovací jméno uživatele
    1.48 +	 * @return seznam skupin, ve kterých se daný uživatel nachází
    1.49 +	 * @throws NoSuchUserException když uživatel s tímto jménem neexistuje.
    1.50 +	 */
    1.51 +	@Override
    1.52 +	public Enumeration getGroupNames(String uživatel) throws NoSuchUserException {
    1.53 +		throw new NoSuchUserException("Metoda zatím není implementována. Uživatel: " + uživatel);
    1.54 +	}
    1.55 +
    1.56 +	/**
    1.57 +	 * 
    1.58 +	 * @param jméno uživatelské jméno
    1.59 +	 * @param heslo heslo
    1.60 +	 * @return seznam skupin, do kterých uživatel patří
    1.61 +	 */
    1.62 +	public String[] ověřUživatele(String jméno, char[] heslo) throws LoginException {
    1.63 +
    1.64 +		// TODO: skutečně ověřovat heslo proti databázi
    1.65 +
    1.66 +		if (heslo != null && heslo.length == 3) {
    1.67 +			return new String[]{"bezny"};
    1.68 +		} else {
    1.69 +			throw new LoginException("Heslo musí mít tři znaky :-P");
    1.70 +		}
    1.71  	}
    1.72  }