DataSourceRealm (Apache Tomcat 7.0.104 API Documentation)

org.apache.catalina.realm

## Class DataSourceRealm

- java.lang.Object
- - org.apache.catalina.util.LifecycleBase
  - - org.apache.catalina.util.LifecycleMBeanBase
    - - org.apache.catalina.realm.RealmBase
      - - org.apache.catalina.realm.DataSourceRealm

- All Implemented Interfaces:
    MBeanRegistration, GSSRealm, JmxEnabled, Lifecycle, Realm

      public class DataSourceRealm
      extends RealmBase

  Implementation of Realm that works with any JDBC JNDI DataSource. See the JDBCRealm.howto for more details on how to set up the database and for configuration options.
  Author:
    Glenn L. Nielsen, Craig R. McClanahan, Carson McDonald, Ignacio Ortega

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from class org.apache.catalina.realm.RealmBase

      `RealmBase.AllRolesMode`

    - ### Nested classes/interfaces inherited from interface org.apache.catalina.Lifecycle

      `Lifecycle.SingleUse`

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected String` | `dataSourceName` The name of the JNDI JDBC DataSource
    `protected static String` | `info` Descriptive information about this Realm implementation.
    `protected boolean` | `localDataSource` Context local datasource.
    `protected static String` | `name` Descriptive information about this Realm implementation.
    `protected String` | `roleNameCol` The column in the user role table that names a role
    `protected String` | `userCredCol` The column in the user table that holds the user's credentials
    `protected String` | `userNameCol` The column in the user table that holds the user's name
    `protected String` | `userRoleTable` The table that holds the relation between user's and roles
    `protected String` | `userTable` The table that holds user data.

    - ### Fields inherited from class org.apache.catalina.realm.RealmBase

      `allRolesMode, container, containerLog, digest, digestEncoding, md, md5Encoder, md5Helper, realmPath, sm, stripRealmForGss, support, validate, x509UsernameRetriever, x509UsernameRetrieverClassName`

    - ### Fields inherited from class org.apache.catalina.util.LifecycleMBeanBase

      `mserver`

    - ### Fields inherited from interface org.apache.catalina.Lifecycle

      `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 Summary

    Constructors
    Constructor and Description
    `DataSourceRealm()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected 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`.
    `Principal` | `authenticate(String username, String credentials)` Return the Principal associated with the specified username and credentials, if there is one; otherwise return `null`.
    `protected void` | `close(Connection dbConnection)` Close the specified database connection.
    `String` | `getDataSourceName()` Return the name of the JNDI JDBC DataSource.
    `String` | `getInfo()` Return descriptive information about this Realm implementation and the corresponding version number, in the format `<description>/<version>`.
    `boolean` | `getLocalDataSource()` Return if the datasource will be looked up in the webapp JNDI Context.
    `protected String` | `getName()` Return a short name for this Realm implementation.
    `protected String` | `getPassword(Connection dbConnection, String username)` Return the password associated with the given principal's user name.
    `protected String` | `getPassword(String username)` Return the password associated with the given principal's user name.
    `protected Principal` | `getPrincipal(String username)` Return the Principal associated with the given user name.
    `String` | `getRoleNameCol()` Return the column in the user role table that names a role.
    `protected ArrayList<String>` | `getRoles(Connection dbConnection, String username)` Return the roles associated with the given user name.
    `protected ArrayList<String>` | `getRoles(String username)` Return the roles associated with the given user name.
    `String` | `getUserCredCol()` Return the column in the user table that holds the user's credentials.
    `String` | `getUserNameCol()` Return the column in the user table that holds the user's name.
    `String` | `getUserRoleTable()` Return the table that holds the relation between user's and roles.
    `String` | `getUserTable()` Return the table that holds user data..
    `protected Connection` | `open()` Open the specified database connection.
    `void` | `setDataSourceName(String dataSourceName)` Set the name of the JNDI JDBC DataSource.
    `void` | `setLocalDataSource(boolean localDataSource)` Set to true to cause the datasource to be looked up in the webapp JNDI Context.
    `void` | `setRoleNameCol(String roleNameCol)` Set the column in the user role table that names a role.
    `void` | `setUserCredCol(String userCredCol)` Set the column in the user table that holds the user's credentials.
    `void` | `setUserNameCol(String userNameCol)` Set the column in the user table that holds the user's name.
    `void` | `setUserRoleTable(String userRoleTable)` Set the table that holds the relation between user's and roles.
    `void` | `setUserTable(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()`.

    - ### Methods inherited from class org.apache.catalina.realm.RealmBase

      `addPropertyChangeListener, authenticate, authenticate, authenticate, authenticate, authenticate, backgroundProcess, compareCredentials, digest, Digest, findSecurityConstraints, getAllRolesMode, getContainer, getDigest, getDigest, getDigestCharset, getDigestEncoding, getDomainInternal, getObjectNameKeyProperties, getPrincipal, getPrincipal, getPrincipal, getRealmPath, getRealmSuffix, getServer, getTransportGuaranteeRedirectStatus, getValidate, getX509UsernameRetrieverClassName, hasMessageDigest, hasResourcePermission, hasRole, hasUserDataPermission, initInternal, isStripRealmForGss, main, removePropertyChangeListener, setAllRolesMode, setContainer, setDigest, setDigestEncoding, setRealmPath, setStripRealmForGss, setTransportGuaranteeRedirectStatus, setValidate, setX509UsernameRetrieverClassName, stopInternal, toString`

    - ### Methods inherited from class org.apache.catalina.util.LifecycleMBeanBase

      `destroyInternal, getDomain, getObjectName, postDeregister, postRegister, preDeregister, preRegister, register, setDomain, unregister`

    - ### Methods inherited from class org.apache.catalina.util.LifecycleBase

      `addLifecycleListener, destroy, findLifecycleListeners, fireLifecycleEvent, getState, getStateName, getThrowOnFailure, init, removeLifecycleListener, setState, setState, setThrowOnFailure, start, stop`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### dataSourceName

          protected String dataSourceName

      The name of the JNDI JDBC DataSource

    - #### info

          protected static final String info

      Descriptive information about this Realm implementation.
      See Also:
        Constant Field Values

    - #### localDataSource

          protected boolean localDataSource

      Context local datasource.

    - #### name

          protected static final String name

      Descriptive information about this Realm implementation.
      See Also:
        Constant Field Values

    - #### roleNameCol

          protected String roleNameCol

      The column in the user role table that names a role

    - #### userCredCol

          protected String userCredCol

      The column in the user table that holds the user's credentials

    - #### 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.

  - ### Constructor Detail

    - #### DataSourceRealm

          public DataSourceRealm()

  - ### Method Detail

    - #### getDataSourceName

          public String getDataSourceName()

      Return the name of the JNDI JDBC DataSource.

    - #### setDataSourceName

          public void setDataSourceName(String dataSourceName)

      Set the name of the JNDI JDBC DataSource.
      Parameters:
        `dataSourceName` - the name of the JNDI JDBC DataSource

    - #### getLocalDataSource

          public boolean getLocalDataSource()

      Return if the datasource will be looked up in the webapp JNDI Context.

    - #### setLocalDataSource

          public void setLocalDataSource(boolean localDataSource)

      Set to true to cause the datasource to be looked up in the webapp JNDI Context.
      Parameters:
        `localDataSource` - the new flag value

    - #### getRoleNameCol

          public String getRoleNameCol()

      Return the column in the user role table that names a role.

    - #### setRoleNameCol

          public void setRoleNameCol(String roleNameCol)

      Set the column in the user role table that names a role.
      Parameters:
        `roleNameCol` - The column name

    - #### getUserCredCol

          public String getUserCredCol()

      Return the column in the user table that holds the user's credentials.

    - #### setUserCredCol

          public void setUserCredCol(String userCredCol)

      Set the column in the user table that holds the user's credentials.
      Parameters:
        `userCredCol` - The column name

    - #### getUserNameCol

          public String getUserNameCol()

      Return the column in the user table that holds the user's 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

    - #### getUserRoleTable

          public String getUserRoleTable()

      Return the table that holds the relation between user's and roles.

    - #### setUserRoleTable

          public void setUserRoleTable(String userRoleTable)

      Set the table that holds the relation between user's and roles.
      Parameters:
        `userRoleTable` - The table name

    - #### getUserTable

          public String getUserTable()

      Return the table that holds user data..

    - #### setUserTable

          public void setUserTable(String userTable)

      Set the table that holds user data.
      Parameters:
        `userTable` - The table name

    - #### getInfo

          public String getInfo()

      Return descriptive information about this Realm implementation and the corresponding version number, in the format `<description>/<version>`.
      Specified by:
        `getInfo` in interface `Realm`
      Overrides:
        `getInfo` in class `RealmBase`
      Returns:
        descriptive information about this Realm implementation and the corresponding version number, in the format `<description>/<version>`.

    - #### 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.
      Specified by:
        `authenticate` in interface `Realm`
      Overrides:
        `authenticate` in class `RealmBase`
      Parameters:
        `username` - Username of the Principal to look up
        `credentials` - Password or other credentials to use in authenticating this username
      Returns:
        the associated principal, or `null` if there is none.

    - #### authenticate

          protected 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

    - #### open

          protected Connection open()

      Open the specified database connection.
      Returns:
        Connection to the database

    - #### getName

          protected String getName()

      Return a short name for this Realm implementation.
      Specified by:
        `getName` in class `RealmBase`

    - #### getPassword

          protected String getPassword(String username)

      Return the password associated with the given principal's user name.
      Specified by:
        `getPassword` in class `RealmBase`

    - #### getPassword

          protected String getPassword(Connection dbConnection,
                           String username)

      Return the password associated with the given principal's user name.
      Parameters:
        `dbConnection` - The database connection to be used
        `username` - Username for which password should be retrieved
      Returns:
        the password for the specified user

    - #### getPrincipal

          protected Principal getPrincipal(String username)

      Return the Principal associated with the given user name.
      Specified by:
        `getPrincipal` in class `RealmBase`

    - #### getRoles

          protected ArrayList<String> getRoles(String username)

      Return the roles associated with the given user name.
      Parameters:
        `username` - Username for which roles should be retrieved

    - #### getRoles

          protected ArrayList<String> getRoles(Connection dbConnection,
                                   String username)

      Return the roles associated with the given user name.
      Parameters:
        `dbConnection` - The database connection to be used
        `username` - User name for which roles should be retrieved
      Returns:
        an array list of the role names

    - #### startInternal

          protected void startInternal()
                                throws LifecycleException

      Prepare for the beginning of active use of the public methods of this component and implement the requirements of `LifecycleBase.startInternal()`.
      Overrides:
        `startInternal` in class `RealmBase`
      Throws:
        `LifecycleException` - if this component detects a fatal error that prevents this component from being used

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