org.apache.catalina.realm

Class JDBCRealm

public class JDBCRealm extends RealmBase

Implmentation of Realm that works with any JDBC supported database. See the JDBCRealm.howto for more details on how to set up the database and for configuration options.

For a Realm implementation that supports connection pooling and doesn't require synchronisation of authenticate(), getPassword(), roles() and getPrincipal() or the ugly connection logic use the DataSourceRealm.

Version: $Revision: 892553 $ $Date: 2009-12-20 03:32:53 +0100 (Sun, 20 Dec 2009) $

Author: Craig R. McClanahan Carson McDonald Ignacio Ortega

Field Summary
protected StringconnectionName
The connection username to use when trying to connect to the database.
protected StringconnectionPassword
The connection URL to use when trying to connect to the database.
protected StringconnectionURL
The connection URL to use when trying to connect to the database.
protected ConnectiondbConnection
The connection to the database.
protected Driverdriver
Instance of the JDBC Driver class we use as a connection factory.
protected StringdriverName
The JDBC driver to use.
protected static Stringinfo
Descriptive information about this Realm implementation.
protected static Stringname
Descriptive information about this Realm implementation.
protected PreparedStatementpreparedCredentials
The PreparedStatement to use for authenticating users.
protected PreparedStatementpreparedRoles
The PreparedStatement to use for identifying the roles for a specified user.
protected StringroleNameCol
The column in the user role table that names a role
protected static StringManagersm
The string manager for this package.
protected StringuserCredCol
The column in the user table that holds the user's credintials
protected StringuserNameCol
The column in the user table that holds the user's name
protected StringuserRoleTable
The table that holds the relation between user's and roles
protected StringuserTable
The table that holds user data.
Method Summary
Principalauthenticate(String username, String credentials)
Return the Principal associated with the specified username and credentials, if there is one; otherwise return null.
Principalauthenticate(Connection dbConnection, String username, String credentials)
Return the Principal associated with the specified username and credentials, if there is one; otherwise return null.
protected voidclose(Connection dbConnection)
Close the specified database connection.
protected PreparedStatementcredentials(Connection dbConnection, String username)
Return a PreparedStatement configured to perform the SELECT required to retrieve user credentials for the specified username.
StringgetConnectionName()
Return the username to use to connect to the database.
StringgetConnectionPassword()
Return the password to use to connect to the database.
StringgetConnectionURL()
Return the URL to use to connect to the database.
StringgetDriverName()
Return the JDBC driver that will be used.
protected StringgetName()
Return a short name for this Realm implementation.
protected StringgetPassword(String username)
Return the password associated with the given principal's user name.
protected PrincipalgetPrincipal(String username)
Return the Principal associated with the given user name.
StringgetRoleNameCol()
Return the column in the user role table that names a role.
protected ArrayList<String>getRoles(String username)
Return the roles associated with the gven user name.
StringgetUserCredCol()
Return the column in the user table that holds the user's credentials.
StringgetUserNameCol()
Return the column in the user table that holds the user's name.
StringgetUserRoleTable()
Return the table that holds the relation between user's and roles.
StringgetUserTable()
Return the table that holds user data..
protected Connectionopen()
Open (if necessary) and return a database connection for use by this Realm.
protected voidrelease(Connection dbConnection)
Release our use of this connection so that it can be recycled.
protected PreparedStatementroles(Connection dbConnection, String username)
Return a PreparedStatement configured to perform the SELECT required to retrieve user roles for the specified username.
voidsetConnectionName(String connectionName)
Set the username to use to connect to the database.
voidsetConnectionPassword(String connectionPassword)
Set the password to use to connect to the database.
voidsetConnectionURL(String connectionURL)
Set the URL to use to connect to the database.
voidsetDriverName(String driverName)
Set the JDBC driver that will be used.
voidsetRoleNameCol(String roleNameCol)
Set the column in the user role table that names a role.
voidsetUserCredCol(String userCredCol)
Set the column in the user table that holds the user's credentials.
voidsetUserNameCol(String userNameCol)
Set the column in the user table that holds the user's name.
voidsetUserRoleTable(String userRoleTable)
Set the table that holds the relation between user's and roles.
voidsetUserTable(String userTable)
Set the table that holds user data.
voidstart()
Prepare for active use of the public methods of this Component.
voidstop()
Gracefully shut down active use of the public methods of this Component.

Field Detail

connectionName

protected String connectionName
The connection username to use when trying to connect to the database.

connectionPassword

protected String connectionPassword
The connection URL to use when trying to connect to the database.

connectionURL

protected String connectionURL
The connection URL to use when trying to connect to the database.

dbConnection

protected Connection dbConnection
The connection to the database.

driver

protected Driver driver
Instance of the JDBC Driver class we use as a connection factory.

driverName

protected String driverName
The JDBC driver to use.

info

protected static final String info
Descriptive information about this Realm implementation.

name

protected static final String name
Descriptive information about this Realm implementation.

preparedCredentials

protected PreparedStatement preparedCredentials
The PreparedStatement to use for authenticating users.

preparedRoles

protected PreparedStatement preparedRoles
The PreparedStatement to use for identifying the roles for a specified user.

roleNameCol

protected String roleNameCol
The column in the user role table that names a role

sm

protected static final StringManager sm
The string manager for this package.

userCredCol

protected String userCredCol
The column in the user table that holds the user's credintials

userNameCol

protected String userNameCol
The column in the user table that holds the user's name

userRoleTable

protected String userRoleTable
The table that holds the relation between user's and roles

userTable

protected String userTable
The table that holds user data.

Method Detail

authenticate

public Principal authenticate(String username, String credentials)
Return the Principal associated with the specified username and credentials, if there is one; otherwise return null. If there are any errors with the JDBC connection, executing the query or anything we return null (don't authenticate). This event is also logged, and the connection will be closed so that a subsequent request will automatically re-open it.

Parameters: username Username of the Principal to look up credentials Password or other credentials to use in authenticating this username

authenticate

public Principal authenticate(Connection dbConnection, String username, String credentials)
Return the Principal associated with the specified username and credentials, if there is one; otherwise return null.

Parameters: dbConnection The database connection to be used username Username of the Principal to look up credentials Password or other credentials to use in authenticating this username

close

protected void close(Connection dbConnection)
Close the specified database connection.

Parameters: dbConnection The connection to be closed

credentials

protected PreparedStatement credentials(Connection dbConnection, String username)
Return a PreparedStatement configured to perform the SELECT required to retrieve user credentials for the specified username.

Parameters: dbConnection The database connection to be used username Username for which credentials should be retrieved

Throws: SQLException if a database error occurs

getConnectionName

public String getConnectionName()
Return the username to use to connect to the database.

getConnectionPassword

public String getConnectionPassword()
Return the password to use to connect to the database.

getConnectionURL

public String getConnectionURL()
Return the URL to use to connect to the database.

getDriverName

public String getDriverName()
Return the JDBC driver that will be used.

getName

protected String getName()
Return a short name for this Realm implementation.

getPassword

protected String getPassword(String username)
Return the password associated with the given principal's user name.

getPrincipal

protected Principal getPrincipal(String username)
Return the Principal associated with the given user name.

getRoleNameCol

public String getRoleNameCol()
Return the column in the user role table that names a role.

getRoles

protected ArrayList<String> getRoles(String username)
Return the roles associated with the gven user name.

getUserCredCol

public String getUserCredCol()
Return the column in the user table that holds the user's credentials.

getUserNameCol

public String getUserNameCol()
Return the column in the user table that holds the user's name.

getUserRoleTable

public String getUserRoleTable()
Return the table that holds the relation between user's and roles.

getUserTable

public String getUserTable()
Return the table that holds user data..

open

protected Connection open()
Open (if necessary) and return a database connection for use by this Realm.

Throws: SQLException if a database error occurs

release

protected void release(Connection dbConnection)
Release our use of this connection so that it can be recycled.

Parameters: dbConnection The connection to be released

roles

protected PreparedStatement roles(Connection dbConnection, String username)
Return a PreparedStatement configured to perform the SELECT required to retrieve user roles for the specified username.

Parameters: dbConnection The database connection to be used username Username for which roles should be retrieved

Throws: SQLException if a database error occurs

setConnectionName

public void setConnectionName(String connectionName)
Set the username to use to connect to the database.

Parameters: connectionName Username

setConnectionPassword

public void setConnectionPassword(String connectionPassword)
Set the password to use to connect to the database.

Parameters: connectionPassword User password

setConnectionURL

public void setConnectionURL(String connectionURL)
Set the URL to use to connect to the database.

Parameters: connectionURL The new connection URL

setDriverName

public void setDriverName(String driverName)
Set the JDBC driver that will be used.

Parameters: driverName The driver name

setRoleNameCol

public void setRoleNameCol(String roleNameCol)
Set the column in the user role table that names a role.

Parameters: roleNameCol The column name

setUserCredCol

public void setUserCredCol(String userCredCol)
Set the column in the user table that holds the user's credentials.

Parameters: userCredCol The column name

setUserNameCol

public void setUserNameCol(String userNameCol)
Set the column in the user table that holds the user's name.

Parameters: userNameCol The column name

setUserRoleTable

public void setUserRoleTable(String userRoleTable)
Set the table that holds the relation between user's and roles.

Parameters: userRoleTable The table name

setUserTable

public void setUserTable(String userTable)
Set the table that holds user data.

Parameters: userTable The table name

start

public void start()
Prepare for active use of the public methods of this Component.

Throws: LifecycleException if this component detects a fatal error that prevents it from being started

stop

public void stop()
Gracefully shut down active use of the public methods of this Component.

Throws: LifecycleException if this component detects a fatal error that needs to be reported

Copyright © 2000-2011 Apache Software Foundation. All Rights Reserved.