Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2011, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors. 
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.jboss.security.auth.callback;
 
 import java.util.Map;
 
 
 import  org.jboss.logging.Logger;
 import  org.jboss.security.ErrorCodes;
 import  org.jboss.security.vault.SecurityVaultException;
 import  org.jboss.security.vault.SecurityVaultUtil;

A CallbackHandler that uses a DB.

Configuration: There are two ways to specify the configuration.

  1. Using the #setConfiguration(Map) method, which uses String based key/value pair.
  2. Using the methods #setConnectionUrl(String), #setDbDriverName(String)etc

Either you can specify the connection url, driver class name or you can provide the jndi name of the DataSource.

Author(s):
Anil Saldhana
Since:
Oct 31, 2011
 
 public class DatabaseCallbackHandler extends AbstractCallbackHandler implements CallbackHandler 
 {
 	protected static Logger log = Logger.getLogger(DatabaseCallbackHandler.class);
 	protected boolean trace = .isTraceEnabled();
 
 	public static final String CONNECTION_URL = "connectionURL";
 	public static final String DS_JNDI_NAME = "dsJndiName";
 	public static final String DB_DRIVERNAME = "dbDriverName";
 	public static final String DB_USERNAME = "dbUserName";
 	public static final String DB_USERPASS = "dbUserPass";
 	public static final String PRINCIPALS_QUERY = "principalsQuery";

A DB specific connection url
 
 	protected String connectionUrl;
JNDI Name of the Datasource
 
 	protected String dsJndiName;
A DB username to connect
 
 	protected String dsUserName;
A DB password to connect
 
 	protected String dsUserPass;
A DB Driver Class Name
 
 	protected String dbDriverName;

The sql query to obtain the user password
 
 	protected String principalsQuery = "select Password from Principals where PrincipalID=?";
	{	
	}

Get the DB specific connection URL Eg: "jdbc:hsqldb:mem:unit_test"

Returns:
	{
	public void setConnectionUrl(String connectionUrl
	{
		this. = connectionUrl;


Get the JNDI name of the SQL Datasource

Returns:
	public String getDsJndiName() 
	{
		return ;
	public void setDsJndiName(String dsJndiName
	{
		this. = dsJndiName;
	}

Get the DB user name

Returns:
	public String getDsUserName() 
	{
		return ;
	}
	public void setDsUserName(String dsUserName
	{
		this. = dsUserName;
	}

Get the DB user pass

Returns:
	public String getDsUserPass() 
	{
		return ;
	}
	public void setDsUserPass(String dsUserPass
	{
		this. = dsUserPass;
	}

Get the fully qualified name of sql driver class Eg: org.hsqldb.jdbc.JDBCDriver

Returns:
	{
		return ;
	}
	public void setDbDriverName(String dbDriverName
	{
		this. = dbDriverName;
	}
	{
	}
	public void setPrincipalsQuery(String principalsQuery) {
		this. = principalsQuery;
	}
 
	public String getUserName() {
		return ;
	}
	public void setUserName(String theUserName)
	{
		if(theUserName == null)
		{
			throw new IllegalArgumentException(ErrorCodes.NULL_ARGUMENT + "un");
		}
		 = theUserName;
	}

Set a Map that contains keys that are strings and values that are strings

Parameters:
config
	public void setConfiguration(Map<String,Stringconfig)
	{
		String tmp = null;
		if( == null || .length() == 0)
		{
		}
		if( != null)
		{
			if(SecurityVaultUtil.isVaultFormat())
			{
				try 
				{
					 = SecurityVaultUtil.getValueAsString();
				catch (SecurityVaultException e
				{
					throw new RuntimeException(e);
				}
			}
		}
		tmp = config.get();
		if(tmp != null)
		{
		}
	}
	/*
	 * @see javax.security.auth.callback.CallbackHandler#handle(javax.security.auth.callback.Callback[])
	 */
	public void handle(Callback[] callbacksthrows IOException,
	{
		if( == null)
		{
			 = getUserName(callbacks);
		}
		for (int i = 0; i < callbacks.lengthi++)
		{
			Callback callback = callbacks[i];
			this.handleCallBackcallback ); 
		}
	}

Handle a Callback

Parameters:
c callback
Throws:
UnsupportedCallbackException If the callback is not supported by this handler
IOException
		if(c instanceof VerifyPasswordCallback)
		{
			try 
			{
			catch (CredentialException e
			{
				throw new IOException(e);
			}
		}
		if(c instanceof PasswordCallback == false)
			return;
		PasswordCallback passwdCallback = (PasswordCallbackc;
		passwdCallback.setPassword(getPassword().toCharArray());
	}
	{
		String userPass = vpc.getValue();
		String passwordFromDB = getPassword();
		if(userPass.equals(passwordFromDB))
		{
			vpc.setVerified(true);
		}
		else
		{
			throw new CredentialException(ErrorCodes.ACCESS_DENIED + "Passwords don't match");
		}
	}
	private String getPassword()
	{
		String password = null;
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try 
		{
		    conn = getConnection();
			rs = ps.executeQuery();
			ifrs.next() == false )
			{
					.trace("Query returned no matches from db");
				throw new RuntimeException(ErrorCodes.PROCESSING_FAILED + "No matching username found in Principals:" + );
			}
			password = rs.getString(1);
		catch (Exception e
			throw new RuntimeException(e);
		}
		finally
		{
			safeClose(conn);
		}
		return password;
	}
	{
		Connection conn = null;
		if( != null)
		{
			try 
			{
			{
				throw new RuntimeException(ErrorCodes.PROCESSING_FAILED,e);
			}
		}
		if( != null)
		{
			if( != null)
			{
				conn = DriverManager.getConnection();
			}
			else
			{
				conn = DriverManager.getConnection();
			}
		}
		else
		{
			if( == null)
			{
				throw new RuntimeException(ErrorCodes.NULL_VALUE + "dsJndiName is null");
			}
			if(ds != null)
			{
				conn = ds.getConnection();
			}
		}
		return conn;
	}
	protected void safeClose(ResultSet rs)
	{
		ifrs != null)
		{
			try 
			{
				rs.close();
			catch (SQLException e
			{	
			}
		}
	}
	protected void safeClose(Connection conn)
	{
		ifconn != null)
		{
			try 
			{
				conn.close();
			catch (SQLException e
			{	
			}
		}
	}
	protected void safeClose(Statement stat)
	{
		ifstat != null)
		{
			try 
			{
				stat.close();
			catch (SQLException e
			{	
			}
		}
	}
New to GrepCode? Check out our FAQ X