public class JDBCRealm extends RealmBase
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
.
RealmBase.AllRolesMode
Lifecycle.SingleUse
Modifier and Type | Field and Description |
---|---|
protected java.lang.String |
connectionName
The connection username to use when trying to connect to the database.
|
protected java.lang.String |
connectionPassword
The connection URL to use when trying to connect to the database.
|
protected java.lang.String |
connectionURL
The connection URL to use when trying to connect to the database.
|
protected java.sql.Connection |
dbConnection
The connection to the database.
|
protected java.sql.Driver |
driver
Instance of the JDBC Driver class we use as a connection factory.
|
protected java.lang.String |
driverName
The JDBC driver to use.
|
protected static java.lang.String |
name
Descriptive information about this Realm implementation.
|
protected java.sql.PreparedStatement |
preparedCredentials
The PreparedStatement to use for authenticating users.
|
protected java.sql.PreparedStatement |
preparedRoles
The PreparedStatement to use for identifying the roles for
a specified user.
|
protected java.lang.String |
roleNameCol
The column in the user role table that names a role
|
protected java.lang.String |
userCredCol
The column in the user table that holds the user's credentials
|
protected java.lang.String |
userNameCol
The column in the user table that holds the user's name
|
protected java.lang.String |
userRoleTable
The table that holds the relation between user's and roles
|
protected java.lang.String |
userTable
The table that holds user data.
|
allRolesMode, container, containerLog, digest, digestEncoding, md, md5Helper, realmPath, sm, stripRealmForGss, support, validate, x509UsernameRetriever, x509UsernameRetrieverClassName
mserver
AFTER_DESTROY_EVENT, AFTER_INIT_EVENT, AFTER_START_EVENT, AFTER_STOP_EVENT, BEFORE_DESTROY_EVENT, BEFORE_INIT_EVENT, BEFORE_START_EVENT, BEFORE_STOP_EVENT, CONFIGURE_START_EVENT, CONFIGURE_STOP_EVENT, PERIODIC_EVENT, START_EVENT, STOP_EVENT
Constructor and Description |
---|
JDBCRealm() |
Modifier and Type | Method and Description |
---|---|
java.security.Principal |
authenticate(java.sql.Connection dbConnection,
java.lang.String username,
java.lang.String credentials)
Attempt to authenticate the user with the provided credentials.
|
java.security.Principal |
authenticate(java.lang.String username,
java.lang.String credentials)
Return the Principal associated with the specified username and
credentials, if there is one; otherwise return
null . |
protected void |
close(java.sql.Connection dbConnection)
Close the specified database connection.
|
protected java.sql.PreparedStatement |
credentials(java.sql.Connection dbConnection,
java.lang.String username)
Return a PreparedStatement configured to perform the SELECT required
to retrieve user credentials for the specified username.
|
java.lang.String |
getConnectionName() |
java.lang.String |
getConnectionPassword() |
java.lang.String |
getConnectionURL() |
java.lang.String |
getDriverName() |
protected java.lang.String |
getName() |
protected java.lang.String |
getPassword(java.lang.String username)
Get the password for the specified user.
|
protected java.security.Principal |
getPrincipal(java.lang.String username)
Get the principal associated with the specified user.
|
java.lang.String |
getRoleNameCol() |
protected java.util.ArrayList<java.lang.String> |
getRoles(java.lang.String username)
Return the roles associated with the given user name.
|
java.lang.String |
getUserCredCol() |
java.lang.String |
getUserNameCol() |
java.lang.String |
getUserRoleTable() |
java.lang.String |
getUserTable() |
protected java.sql.Connection |
open()
Open (if necessary) and return a database connection for use by
this Realm.
|
protected java.sql.PreparedStatement |
roles(java.sql.Connection dbConnection,
java.lang.String username)
Return a PreparedStatement configured to perform the SELECT required
to retrieve user roles for the specified username.
|
void |
setConnectionName(java.lang.String connectionName)
Set the username to use to connect to the database.
|
void |
setConnectionPassword(java.lang.String connectionPassword)
Set the password to use to connect to the database.
|
void |
setConnectionURL(java.lang.String connectionURL)
Set the URL to use to connect to the database.
|
void |
setDriverName(java.lang.String driverName)
Set the JDBC driver that will be used.
|
void |
setRoleNameCol(java.lang.String roleNameCol)
Set the column in the user role table that names a role.
|
void |
setUserCredCol(java.lang.String userCredCol)
Set the column in the user table that holds the user's credentials.
|
void |
setUserNameCol(java.lang.String userNameCol)
Set the column in the user table that holds the user's name.
|
void |
setUserRoleTable(java.lang.String userRoleTable)
Set the table that holds the relation between user's and roles.
|
void |
setUserTable(java.lang.String userTable)
Set the table that holds user data.
|
protected void |
startInternal()
Prepare for the beginning of active use of the public methods of this
component and implement the requirements of
LifecycleBase.startInternal() . |
protected void |
stopInternal()
Gracefully terminate the active use of the public methods of this
component and implement the requirements of
LifecycleBase.stopInternal() . |
addPropertyChangeListener, authenticate, authenticate, authenticate, authenticate, backgroundProcess, compareCredentials, digest, Digest, findSecurityConstraints, getAllRolesMode, getContainer, getCredentialHandler, getDigest, getDigest, getDigestCharset, getDigestEncoding, getDomainInternal, getObjectNameKeyProperties, getPrincipal, getPrincipal, getRealmPath, getRealmSuffix, getServer, getTransportGuaranteeRedirectStatus, getValidate, getX509UsernameRetrieverClassName, hasMessageDigest, hasResourcePermission, hasRole, hasUserDataPermission, initInternal, isStripRealmForGss, main, removePropertyChangeListener, setAllRolesMode, setContainer, setCredentialHandler, setDigest, setDigestEncoding, setRealmPath, setStripRealmForGss, setTransportGuaranteeRedirectStatus, setValidate, setX509UsernameRetrieverClassName, toString
destroyInternal, getDomain, getObjectName, postDeregister, postRegister, preDeregister, preRegister, register, setDomain, unregister
addLifecycleListener, destroy, findLifecycleListeners, fireLifecycleEvent, getState, getStateName, init, removeLifecycleListener, setState, setState, start, stop
protected java.lang.String connectionName
protected java.lang.String connectionPassword
protected java.lang.String connectionURL
protected java.sql.Connection dbConnection
protected java.sql.Driver driver
protected java.lang.String driverName
protected static final java.lang.String name
protected java.sql.PreparedStatement preparedCredentials
protected java.sql.PreparedStatement preparedRoles
protected java.lang.String roleNameCol
protected java.lang.String userCredCol
protected java.lang.String userNameCol
protected java.lang.String userRoleTable
protected java.lang.String userTable
public java.lang.String getConnectionName()
public void setConnectionName(java.lang.String connectionName)
connectionName
- Usernamepublic java.lang.String getConnectionPassword()
public void setConnectionPassword(java.lang.String connectionPassword)
connectionPassword
- User passwordpublic java.lang.String getConnectionURL()
public void setConnectionURL(java.lang.String connectionURL)
connectionURL
- The new connection URLpublic java.lang.String getDriverName()
public void setDriverName(java.lang.String driverName)
driverName
- The driver namepublic java.lang.String getRoleNameCol()
public void setRoleNameCol(java.lang.String roleNameCol)
roleNameCol
- The column namepublic java.lang.String getUserCredCol()
public void setUserCredCol(java.lang.String userCredCol)
userCredCol
- The column namepublic java.lang.String getUserNameCol()
public void setUserNameCol(java.lang.String userNameCol)
userNameCol
- The column namepublic java.lang.String getUserRoleTable()
public void setUserRoleTable(java.lang.String userRoleTable)
userRoleTable
- The table namepublic java.lang.String getUserTable()
public void setUserTable(java.lang.String userTable)
userTable
- The table namepublic java.security.Principal authenticate(java.lang.String username, java.lang.String credentials)
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.authenticate
in interface Realm
authenticate
in class RealmBase
username
- Username of the Principal to look upcredentials
- Password or other credentials to use in
authenticating this usernamenull
if there is none.public java.security.Principal authenticate(java.sql.Connection dbConnection, java.lang.String username, java.lang.String credentials)
dbConnection
- The database connection to be usedusername
- Username of the Principal to look upcredentials
- Password or other credentials to use in authenticating
this usernamenull
.protected void close(java.sql.Connection dbConnection)
dbConnection
- The connection to be closedprotected java.sql.PreparedStatement credentials(java.sql.Connection dbConnection, java.lang.String username) throws java.sql.SQLException
dbConnection
- The database connection to be usedusername
- Username for which credentials should be retrievedjava.sql.SQLException
- if a database error occursprotected java.lang.String getName()
protected java.lang.String getPassword(java.lang.String username)
getPassword
in class RealmBase
username
- The user nameprotected java.security.Principal getPrincipal(java.lang.String username)
getPrincipal
in class RealmBase
username
- The user nameprotected java.util.ArrayList<java.lang.String> getRoles(java.lang.String username)
username
- The user nameprotected java.sql.Connection open() throws java.sql.SQLException
java.sql.SQLException
- if a database error occursprotected java.sql.PreparedStatement roles(java.sql.Connection dbConnection, java.lang.String username) throws java.sql.SQLException
dbConnection
- The database connection to be usedusername
- Username for which roles should be retrievedjava.sql.SQLException
- if a database error occursprotected void startInternal() throws LifecycleException
LifecycleBase.startInternal()
.startInternal
in class RealmBase
LifecycleException
- if this component detects a fatal error
that prevents this component from being usedprotected void stopInternal() throws LifecycleException
LifecycleBase.stopInternal()
.stopInternal
in class RealmBase
LifecycleException
- if this component detects a fatal error
that needs to be reportedCopyright © 2000-2018 Apache Software Foundation. All Rights Reserved.