Class DataSourceRealm

  • All Implemented Interfaces:
    javax.management.MBeanRegistration, Contained, JmxEnabled, Lifecycle, Realm

    public class DataSourceRealm
    extends RealmBase
    Implementation of Realm that works with any JDBC JNDI DataSource. See the Realm How-To 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
    • Field Detail

      • dataSourceName

        protected java.lang.String dataSourceName
        The name of the JNDI JDBC DataSource
      • localDataSource

        protected boolean localDataSource
        Context local datasource.
      • roleNameCol

        protected java.lang.String roleNameCol
        The column in the user role table that names a role
      • userCredCol

        protected java.lang.String userCredCol
        The column in the user table that holds the user's credentials
      • userNameCol

        protected java.lang.String userNameCol
        The column in the user table that holds the user's name
      • userRoleTable

        protected java.lang.String userRoleTable
        The table that holds the relation between user's and roles
      • userTable

        protected java.lang.String userTable
        The table that holds user data.
    • Constructor Detail

      • DataSourceRealm

        public DataSourceRealm()
    • Method Detail

      • getDataSourceName

        public java.lang.String getDataSourceName()
        Returns:
        the name of the JNDI JDBC DataSource.
      • setDataSourceName

        public void setDataSourceName​(java.lang.String dataSourceName)
        Set the name of the JNDI JDBC DataSource.
        Parameters:
        dataSourceName - the name of the JNDI JDBC DataSource
      • getLocalDataSource

        public boolean getLocalDataSource()
        Returns:
        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 java.lang.String getRoleNameCol()
        Returns:
        the column in the user role table that names a role.
      • setRoleNameCol

        public void setRoleNameCol​(java.lang.String roleNameCol)
        Set the column in the user role table that names a role.
        Parameters:
        roleNameCol - The column name
      • getUserCredCol

        public java.lang.String getUserCredCol()
        Returns:
        the column in the user table that holds the user's credentials.
      • setUserCredCol

        public void setUserCredCol​(java.lang.String userCredCol)
        Set the column in the user table that holds the user's credentials.
        Parameters:
        userCredCol - The column name
      • getUserNameCol

        public java.lang.String getUserNameCol()
        Returns:
        the column in the user table that holds the user's name.
      • setUserNameCol

        public void setUserNameCol​(java.lang.String userNameCol)
        Set the column in the user table that holds the user's name.
        Parameters:
        userNameCol - The column name
      • getUserRoleTable

        public java.lang.String getUserRoleTable()
        Returns:
        the table that holds the relation between user's and roles.
      • setUserRoleTable

        public void setUserRoleTable​(java.lang.String userRoleTable)
        Set the table that holds the relation between user's and roles.
        Parameters:
        userRoleTable - The table name
      • getUserTable

        public java.lang.String getUserTable()
        Returns:
        the table that holds user data..
      • setUserTable

        public void setUserTable​(java.lang.String userTable)
        Set the table that holds user data.
        Parameters:
        userTable - The table name
      • authenticate

        public 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. 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.
      • isAvailable

        public boolean isAvailable()
        Description copied from interface: Realm
        Return the availability of the realm for authentication.
        Returns:
        true if the realm is able to perform authentication
      • authenticate

        protected java.security.Principal authenticate​(java.sql.Connection dbConnection,
                                                       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.
        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
        Returns:
        the associated principal, or null if there is none.
      • close

        protected void close​(java.sql.Connection dbConnection)
        Close the specified database connection.
        Parameters:
        dbConnection - The connection to be closed
      • open

        protected java.sql.Connection open()
        Open the specified database connection.
        Returns:
        Connection to the database
      • getPassword

        protected java.lang.String getPassword​(java.lang.String username)
        Description copied from class: RealmBase
        Get the password for the specified user.
        Specified by:
        getPassword in class RealmBase
        Parameters:
        username - The user name
        Returns:
        the password associated with the given principal's user name.
      • getPassword

        protected java.lang.String getPassword​(java.sql.Connection dbConnection,
                                               java.lang.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 java.security.Principal getPrincipal​(java.lang.String username)
        Return the Principal associated with the given user name.
        Specified by:
        getPrincipal in class RealmBase
        Parameters:
        username - the user name
        Returns:
        the principal object
      • getRoles

        protected java.util.ArrayList<java.lang.String> getRoles​(java.lang.String username)
        Return the roles associated with the given user name.
        Parameters:
        username - User name for which roles should be retrieved
        Returns:
        an array list of the role names
      • getRoles

        protected java.util.ArrayList<java.lang.String> getRoles​(java.sql.Connection dbConnection,
                                                                 java.lang.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