Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: null

...

Using a JNDI JDBC data source resource requires the JDBC driver to be visible to Tomcat. Thus, the JDBC driver needs to be placed in $CATALINA_HOMEBASE/lib (for Tomcat 6). Once this is done, do not put this driver in the application's WEB-INF/lib directory.

...

  • Everything in META-INF/context.xml which provides an application - specific configuration
  • GlobalNamingResources and META-INF/context.xml which provides for multiple applications selectively using authentication
  • Everything in $CATALINA_HOMEBASE/conf/server.xml which provides a global Host or Engine configuration

...

  1. Add security constraints and information to WEB-INF/web.xml
  2. Add Resource element to META-INF/context.xml
  3. Add Realm element to META-INF/context.xml

Resource in $CATALINA_

...

BASE/conf/server.xml and Realm in META-INF/context.xml

This configuration can be appropriate when multiple applications need to use the same authentication and authorization database. The JNDI resource is described in the GlobalNamingResources element of $CATALINA_HOMEBASE/conf/server.xml. Each application that requires authentication and authorization via this resource should a Realm definition in META-INF/context.xml referencing the global name.

...

Adding the authentication and authorization resource to the above default implementation creates the following GlobalNamingResources element in $CATALINA_HOMEBASE/conf/server.xml.

No Format
  <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
    <Resource
        name="jdbc/auth"
        description="Sample authentication"
        type="javax.sql.DataSource"
        auth="Container"
        driverClassName="org.apache.derby.jdbc.ClientDriver"
        maxActive="10" maxIdle="3"
        maxWait="10000"
        password="PASSWORD"
        url="jdbc:derby://localhost:1527/authorize"
        validationQuery="values(1)"
        username="USER"/>
  </GlobalNamingResources>

...

NOTE: In order to make the new Resource available, Tomcat will have to be restarted once the $CATALINA_HOMEBASE/conf/server.xml file has been modified.

...

  • localDataSource="true" is no longer present, since the Resource is no longer local.
  • dataSourceName refers to the name of the Resource element in $CATALINA_HOMEBASE/conf/server.xml

Completed META-INF/context.xml

...

  1. Add security constraints and information to WEB-INF/web.xml
  2. Modify $CATALINA_HOMEBASE/conf/server.xml
    1. Add the Resource sub-element to GlobalNamingResources
    2. Restart Tomcat to make the new Resource availale
  3. Add the Realm element to each META-INF/context.xml that requires authentication and authorization

Resource and Realm in $CATALINA_

...

BASE/conf/server.xml

Sometimes every sub-element under a particular element requires the same set of authentication and authorization resources. Rather than duplicating the configuration for multiple resources, it may make sense to place both the Resource and Realm in $CATALINA_HOMEBASE/conf/server.xml. Possible scenarios are listed below.

...

Each web application that wishes to make use of the $CATALINA_HOMEBASE/conf/server.xml - defined Realm must still obviously have security constraints configured in WEB-INF/web.xml.* *

...

  • Realm definition in the Engine element of $CATALINA_HOMEBASE/conf/server.xml
    • Would be overridden by a Realm definition in a Host sub-element of the Engine element
    • Would be overridden by a Realm definition in the META-INF/context.xml for a particular application
  • Realm definition in the Host element of $CATALINA_HOMEBASE/conf/server.xml
    • Would be overridden by a Realm definition in the META-INF/context.xml for a particular application

...

One way to manage multiple Realms in $CATALINA_HOMEBASE/conf/server.xml is to use a CombinedRealm. The CombinedRealm provides a container for other Realms (sub-Realms). These Realms are tried in the order configured, until an authentication match is is made or all sub-Realms are tried.

...

The following steps can be used to configure a DataSource Realm in $CATALINA_BASE/conf/server.xml using a CombinedRealm.

...

Add the required Resource element to the GlobalNamingResources element in $CATALINA_HOMEBASE/conf/server.xml. Below is the default GlobalNamingResources element (without comments) as shipped with Tomcat 6.

...

Adding the authentication and authorization resource to the above default implementation creates the following GlobalNamingResources element in $CATALINA_HOMEBASE/conf/server.xml.

No Format
  <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
    <Resource
        name="jdbc/auth"
        description="Sample authentication"
        type="javax.sql.DataSource"
        auth="Container"
        driverClassName="org.apache.derby.jdbc.ClientDriver"
        maxActive="10" maxIdle="3"
        maxWait="10000"
        password="PASSWORD"
        url="jdbc:derby://localhost:1527/authorize"
        validationQuery="values(1)"
        username="USER"/>
  </GlobalNamingResources>

...

NOTE: In order to make the new Resource available, Tomcat will have to be restarted once the $CATALINA_HOMEBASE/conf/server.xml file has been modified.

...

In order to avoid overriding the existing Engine-level Realm element in Tomcat's default $CATALINA_HOMEBASE/conf/server.xml, a CombinedRealm container will be used.

...

Surround this Realm element with another Realm element defining the CombinedRealm. Within that element place both the default Tomcat UserDatabaseRealm and the DataSourceRealm. The resulting section of $CATALINA_BASE/conf/server.xml will look like the following.

...

NOTE: With both Realm and Resource information in $CATALINA_HOMEBASE/conf/server.xml, no Realm or Resource elements pertaining to authorization and authentication should appear in META-INF/context.xml. An application may require other Resource elements, but any Realm element in META-INF/context.xml will override that provided in $CATALINA_HOMEBASE/conf/server.xml.

Summary for Resource and Realm in $CATALINA_

...

BASE/conf/server.xml

  1. Add security constraints to the application's WEB-INF/web.xml
  2. Add the JNDI resource to GlobalNamingResources in $CATALINA_HOMEBASE/conf/server.xml
  3. Create a CombinedRealm at the appropriate level in $CATALINA_HOMEBASE/conf/server.xml (Engine is used in this example)
    1. Add the existing UserDatabaseRealm to the CombinedRealm as a sub-Realm
    2. Add the DataSourceRealm to the CombinedRealm as a sub-Realm
  4. Restart Tomcat to read the configuration changes in $CATALINA_HOMEBASE/conf/server.xml

Summary

The following outline summarizes the three approaches discussed above.

  1. Everything in META-INF/context.xml
    1. Add the Resource element describing the JNDI datasource
    2. Add the DataSourceRealm element
      1. add localDataSource="true" to reference the local JNDI datasource
  2. Resource in $CATALINA_HOMEBASE/conf/server.xml and Realm in META-INF/context.xml
    1. Add the Resource element describing the JNDI datasource to GlobalNamingResources in $CATALINA_HOMEBASE/conf/server.xml
      1. Restart Tomcat to read the new Resource
    2. Add the DataSourceRealm element to the application's META-INF/context.xml
  3. Resource and Realm in $CATALINA_HOMEBASE/conf/server.xml
    1. Add the Resource element describing the JNDI datasource to GlobalNamingResources in $CATALINA_HOMEBASE/conf/server.xml
    2. Add a CombinedRealm Realm element the the Engine element of $CATALINA_HOMEBASE/conf/server.xml
      1. Place the exisitng UserDatabaseRealm inside this CombinedRealm Realm element
      2. Place the application's DataSourceRealm inside this CombinedRealm Realm element
    3. Restart Tomcat to read the new $CATALINA_HOMEBASE/conf/server.xml
    4. Make sure that no overriding Realms or Resources are present in the application's META-INF/context.xml file

...

CategoryFAQ