Links Getting Started Administrators Application Developers Catalina Developers Jasper Developers | JNDI Resources HOW-TOIntroduction |
Tomcat 4 provides a JNDI InitialContext implementation
instance to web applications running under it, in a manner that is compatible
with those provided by a Java2 Enterprise
Edition application server. Entries in this InitialContext
are configured in the $CATALINA_HOME/conf/server.xml file, and
may be referenced by the following elements in the web application deployment
descriptor (/WEB-INF/web.xml ) of your web application:
<env-entry> - Environment entry, a
single-value parameter that can be used to configure how the application
will operate.
<resource-ref> - Resource reference,
which is typically to an object factory for resources such as a JDBC
DataSource , a JavaMail Session , or custom
object factories configured into Tomcat 4.
<resource-env-ref> - Resource
environment reference, a new variation of resource-ref
added in Servlet 2.3 that is simpler to configure for resources
that do not require authentication information.
The InitialContext is configured as a web application is
initially deployed, and is made available to web application components (for
read-only access). All configured entries and resources will be placed in
the java:comp/env portion of the JNDI namespace, so a typical
access to a resource - in this case, to a JDBC DataSource -
would look something like this:
, Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
// Obtain our environment naming context
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
// Look up our data source
DataSource ds = (DataSource)
envCtx.lookup("jdbc/EmployeeDB");
// Allocate and use a connection from the pool
Connection conn = ds.getConnection();
... use this connection to access the database ...
conn.close();
| , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
See the following Specifications for more information about programming APIs
for JNDI, and for the features supported by Java2 Enterprise Edition (J2EE)
servers, which Tomcat emulates for the services that it provides:
|
Configuring JNDI Resources |
Each available JNDI Resource is configured based on inclusion of the
following elements in the $CATALINA_HOME/conf/server.xml
file:
- <Environment> -
Configure names and values for scalar environment entries that will be
exposed to the web application through the JNDI
InitialContext (equivalent to the inclusion of an
<env-entry> element in the web application
deployment descriptor).
- <Resource> -
Configure the name and data type of a resource made available to the
application (equivalent to the inclusion of a
<resource-ref> element in the web application
deployment descriptor).
- <ResourceParams> -
Configure the Java class name of the resource factory implementation to be
used, as well as JavaBeans properties used to configure that resource
factory.
Any number of these elements may be nested inside a
<Context> element (to be associated
only with that particular web application) or inside a
<DefaultContext> element
(used to set the default configuration characteristics for automatically
deloyed applications).
In addition, the names and values of all <env-entry>
elements included in the web application deployment descriptor
(/WEB-INF/web.xml ) are configured into the initial context as
well, overriding corresponding values from conf/server.xml
only if allowed by the corresponding
<Environment> element (by setting the
override attribute to "true").
|
Tomcat Standard Resource Factories |
Tomcat 4 includes a series of standard resource factories that can
provide services to your web applications, but give you configuration
flexibility (in $CATALINA_HOME/conf/server.xml ) without
modifying the web application or the deployment descriptor. Each
subsection below details the configuration and usage of the standard
resource factories.
See Adding Custom
Resource Factories for information about how to create, install,
configure, and use your own custom resource factory classes with
Tomcat 4.
NOTE - Of the standard resource factories, only the
"JDBC Data Source" and "User Transaction" factories are mandated to
be available on other platforms, and then they are required only if
the platform implements the Java2 Enterprise Edition (J2EE) specs.
All other standard resource factories, plus custom resource factories
that you write yourself, are specific to Tomcat and cannot be assumed
to be available on other containers.
Generic JavaBean Resources |
0. Introduction
This resource factory can be used to create objects of any
Java class that conforms to standard JavaBeans naming conventions (i.e.
it has a zero-arguments constructor, and has property setters that
conform to the setFoo() naming pattern. The resource factory will
create a new instance of the appropriate bean class every time a
lookup() for this entry is made.
The steps required to use this facility are described below.
1. Create Your JavaBean Class
Create the JavaBean class which will be instantiated each time
that the resource factory is looked up. For this example, assume
you create a class com.mycompany.MyBean , which looks
like this:
, Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
package com.mycompany;
public class MyBean {
private String foo = "Default Foo";
public String getFoo() {
return (this.foo);
}
public void setFoo(String foo) {
this.foo = foo;
}
private int bar = 0;
public int getBar() {
return (this.bar);
}
public void setBar(int bar) {
this.bar = bar;
}
}
| , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
2. Declare Your Resource Requirements
Next, modify your web application deployment descriptor
(/WEB-INF/web.xml ) to declare the JNDI name under which
you will request new instances of this bean. The simplest approach is
to use a <resource-env-ref> element, like this:
, Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
<resource-env-ref>
<Description>
Object factory for MyBean instances.
</Description>
<resource-env-ref-name>
bean/MyBeanFactory
</resource-env-ref-name>
<resource-env-ref-type>
com.mycompany.MyBean
</resource-env-ref-type>
<resource-env-ref>
| , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
WARNING - Be sure you respect the element ordering
that is required by the DTD for web application deployment descriptors!
See the
Servlet
Specification for details.
3. Code Your Application's Use Of This Resource
A typical use of this resource environment reference might look
like this:
, Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
MyBean bean = (MyBean) envCtx.lookup("bean/MyBeanFactory");
writer.println("foo = " + bean.getFoo() + ", bar = " +
bean.getBar());
| , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
4. Configure Tomcat's Resource Factory
To configure Tomcat's resource factory, add an elements like this to the
$CATALINA_HOME/conf/server.xml file, nested inside the
Context element for this web application (or nested inside
a DefaultContext element for the surrounding
<Host> or <Engine> element.
, Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
<Context ...>
...
<Resource name="bean/MyBeanFactory" auth="Container"
type="com.mycompany.MyBean"/>
<ResourceParams name="bean/MyBeanFactory">
<parameter>
<name>factory</name>
<value>org.apache.naming.factory.BeanFactory</value>
</parameter>
<parameter>
<name>bar</name>
<value>23</value>
</parameter>
</ResourceParams>
...
</Context>
| , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
Note that the resource name (here, bean/MyBeanFactory
must match the value specified in the web application deployment
descriptor. We are also initializing the value of the bar
property, which will cause setBar(23) to be called before
the new bean is returned. Because we are not initializing the
foo property (although we could have), the bean will
contain whatever default value is set up by its constructor.
|
JavaMail Sessions |
0. Introduction
In many web applications, sending electronic mail messages is a
required part of the system's functionality. The
Java Mail API
makes this process relatively straightforward, but requires many
configuration details that the client application must be aware of
(including the name of the SMTP host to be used for message sending).
Tomcat 4 includes a standard resource factory that will create
javax.mail.Session session instances for you, already
connected to the SMTP server that is configured in server.xml .
In this way, the application is totally insulated from changes in the
email server configuration environment - it simply asks for, and receives,
a preconfigured session whenever needed.
The steps required for this are outlined below.
1. Declare Your Resource Requirements
The first thing you should do is modify the web application deployment
descriptor (/WEB-INF/web.xml ) to declare the JNDI name under
which you will look up preconfigured sessions. By convention, all such
names should resolve to the mail subcontext (relative to the
standard java:comp/env naming context that is the root of
all provided resource factories. A typical web.xml entry
might look like this:
, Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
<resource-ref>
<description>
Resource reference to a factory for javax.mail.Session
instances that may be used for sending electronic mail
messages, preconfigured to connect to the appropriate
SMTP server.
</description>
<res-ref-name>
mail/Session
</res-ref-name>
<res-type>
javax.mail.Session
</res-type>
<res-auth>
Container
</res-auth>
</resource-ref>
| , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
WARNING - Be sure you respect the element ordering
that is required by the DTD for web application deployment descriptors!
See the
Servlet
Specification for details.
2. Code Your Application's Use Of This Resource
A typical use of this resource reference might look like this:
, Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
Session session = (Session) envCtx.lookup("mail/Session");
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(request.getParameter("from"));
InternetAddress to[] = new InternetAddress[1];
to[0] = new InternetAddress(request.getParameter("to"));
message.setRecipients(Message.RecipientType.TO, to);
message.setSubject(request.getParameter("subject"));
message.setContent(request.getParameter("content"), "text/plain");
Transport.send(message);
| , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
Note that the application uses the same resource reference name
that was declared in the web application deployment descriptor. This
is matched up against the resource factory that is configured in
$CATALINA_HOME/conf/server.xml , as described below.
3. Configure Tomcat's Resource Factory
To configure Tomcat's resource factory, add an elements like this to the
$CATALINA_HOME/conf/server.xml file, nested inside the
Context element for this web application (or nested inside
a DefaultContext element for the surrounding
<Host> or <Engine> element.
, Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
<Context ...>
...
<Resource name="mail/Session" auth="Container"
type="javax.mail.Session"/>
<ResourceParams name="mail/Session">
<parameter>
<name>mail.smtp.host</name>
<value>localhost</value>
</parameter>
</ResourceParams>
...
</Context>
| , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
Note that the resource name (here, mail/Session ) must
match the value specified in the web application deployment descriptor.
Customize the value of the mail.smtp.host parameter to
point at the server that provides SMTP service for your network.
Example Application
The /examples application included with Tomcat contains
an example of utilizing this resource factory. It is accessed via the
"JSP Examples" link. The source code for the servlet that actually
sends the mail message is in
/WEB-INF/classes/SendMailServlet.java .
WARNING - The default configuration assumes that
there is an SMTP server listing on port 25 on localhost .
If this is not the case, edit the
$CATALINA_HOME/conf/server.xml file, and modify the
parameter value for the mail.smtp.host parameter to be
the host name of an SMTP server on your network.
|
JDBC Data Sources |
0. Introduction
Many web applications need to access a database via a JDBC driver,
to support the functionality required by that application. The J2EE
Platform Specification requires J2EE Application Servers to make
available a DataSource implementation (that is, a connection
pool for JDBC connections) for this purpose. Tomcat 4 offers exactly
the same support, so that database-based applications you develop on
Tomcat using this service will run unchanged on any J2EE server.
For information about JDBC, you should consult the following:
NOTE - The default data source support in Tomcat
supports Tyrex. However, it is possible to use any other connection pool
that implements javax.sql.DataSource , by writing your own
custom resource factory, as described
below.
1. Install Your JDBC Driver
Use of the JDBC Data Sources JNDI Resource Factory requires
that you make an appropriate JDBC driver available to both Tomcat internal
classes and to your web application. This is most easily accomplished by
installing the driver's JAR file(s) into the
$CATALINA_HOME/common/lib directory, which makes the driver
available both to the resource factory and to your application.
2. Declare Your Resource Requirements
Next, modify the web application deployment descriptor
(/WEB-INF/web.xml ) to declare the JNDI name under
which you will look up preconfigured data source. By convention, all such
names should resolve to the jdbc subcontext (relative to the
standard java:comp/env naming context that is the root of
all provided resource factories. A typical web.xml entry
might look like this:
, Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
<resource-ref>
<description>
Resource reference to a factory for java.sql.Connection
instances that may be used for talking to a particular
database that is configured in the server.xml file.
</description>
<res-ref-name>
jdbc/EmployeDB
</res-ref-name>
<res-type>
javax.sql.DataSource
</res-type>
<res-auth>
Container
</res-auth>
</resource-ref>
| , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
WARNING - Be sure you respect the element ordering
that is required by the DTD for web application deployment descriptors!
See the
Servlet
Specification for details.
3. Code Your Application's Use Of This Resource
A typical use of this resource reference might look like this:
, Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)
envCtx.lookup("jdbc/EmployeeDB");
Connection conn = ds.getConnection();
... use this connection to access the database ...
conn.close();
| , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
Note that the application uses the same resource reference name
that was declared in the web application deployment descriptor. This
is matched up against the resource factory that is configured in
$CATALINA_HOME/conf/server.xml , as described below.
4. Configure Tomcat's Resource Factory
To configure Tomcat's resource factory, add an elements like this to the
$CATALINA_HOME/conf/server.xml file, nested inside the
Context element for this web application (or nested inside
a DefaultContext element for the surrounding
<Host> or <Engine> element.
, Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
<Context ...>
...
<Resource name="jdbc/EmployeeDB" auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/EmployeeDB">
<parameter>
<name>user</name>
<value>dbusername</value>
</parameter>
<parameter>
<name>password</name>
<value>dbpassword</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>org.hsql.jdbcDriver</value>
</parameter>
<parameter>
<name>driverName</name>
<value>jdbc:HypersonicSQL:database</value>
</parameter>
</ResourceParams>
...
</Context>
| , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
Note that the resource name (here, jdbc/EmployeeDB ) must
match the value specified in the web application deployment descriptor.
Customize the value of the mail.smtp.host parameter to
point at the server that provides SMTP service for your network.
This example assumes that you are using the HypersonicSQL database
JDBC driver. Customize the driverClassName and
driverName parameters to match your actual database's
JDBC driver and connection URL.
|
|
Adding Custom Resource Factories |
If none of the standard resource factories meet your needs, you can
write your own factory and integrate it into Tomcat 4, and then configure
the use of this factory in the conf/server.xml configuration
file. In the example below, we will create a factory that only knows how
to create com.mycompany.MyBean beans, from the
Generic JavaBean Resources
example, above.
1. Write A Resource Factory Class
You must write a class that implements the JNDI service provider
javax.naming.spi.ObjectFactory inteface. Every time your
web application calls lookup() on a context entry that is
bound to this factory, the getObjectInstance() method is
called, with the following arguments:
- Object obj - The (possibly null) object containing
location or reference information that can be used in creating an
object. For Tomcat, this will always be an object of type
javax.naming.Reference , which contains the class name
of this factory class, as well as the configuration properties
(from conf/server.xml ) to use in creating objects
to be returned.
- Name name - The name to which this factory is bound
relative to
nameCtx , or null if no name
is specified.
- Context nameCtx - The context relative to which the
name parameter is specified, or null if
name is relative to the default initial context.
- Hashtable environment - The (possibly null)
environment that is used in creating this object. This is generally
ignored in Tomcat object factories.
To create a resource factory that knows how to produce MyBean
instances, you might create a class like this:
, Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
package com.mycompany;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.NamingException;
import javax.naming.RefAddr;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;
public class MyBeanFactory implements ObjectFactory {
public Object getObjectInstance(Object obj,
Name name, Context nameCtx, Hashtable environment)
throws NamingException {
// Acquire an instance of our specified bean class
MyBean bean = new MyBean();
// Customize the bean properties from our attributes
Reference ref = (Reference) obj;
Enumeration addrs = ref.getAll();
while (addrs.hasMoreElements()) {
RefAddr addr = (RefAddr) addrs.nextElement();
String name = addr.getType();
String value = (String) addr.getContent();
if (name.equals("foo")) {
bean.setFoo(value);
} else if (name.equals("bar")) {
try {
bean.setBar(Integer.parseInt(value));
} catch (NumberFormatException e) {
throw new NamingException("Invalid 'bar' value " + value);
}
}
}
// Return the customized instance
return (bean);
}
}
| , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
In this example, we are unconditionally creating a new instance of
the com.mycompany.MyBean class, and populating its properties
based on the parameters included in the <ResourceParams>
element that configures this factory (see below). You should note that any
parameter named factory should be skipped - that parameter is
used to specify the name of the factory class itself (in this case,
com.mycompany.MyBeanFactory ) rather than a property of the
bean being configured.
For more information about ObjectFactory , see the
JNDI 1.2 Service
Provider Interface (SPI) Specification.
You will need to compile this class against a class path that includes
all of the JAR files in the $CATALINA_HOME/common/lib and
$CATALINA_HOME/server/lib directories. When you are through,
place the factory class (and the corresponding bean class) unpacked under
$CATALINA_HOME/common/classes , or in a JAR file inside
$CATALINA_HOME/common/lib . In this way, the required class
files are visible to both Catalina internal resources and your web
application.
2. Declare Your Resource Requirements
Next, modify your web application deployment descriptor
(/WEB-INF/web.xml ) to declare the JNDI name under which
you will request new instances of this bean. The simplest approach is
to use a <resource-env-ref> element, like this:
, Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
<resource-env-ref>
<description>
Object factory for MyBean instances.
</description>
<resource-env-ref-name>
bean/MyBeanFactory
</resource-env-ref-name>
<resource-env-ref-type>
com.mycompany.MyBean
</resource-env-ref-type>
<resource-env-ref>
| , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
WARNING - Be sure you respect the element ordering
that is required by the DTD for web application deployment descriptors!
See the
Servlet
Specification for details.
3. Code Your Application's Use Of This Resource
A typical use of this resource environment reference might look
like this:
, Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
MyBean bean = (MyBean) envCtx.lookup("bean/MyBeanFactory");
writer.println("foo = " + bean.getFoo() + ", bar = " +
bean.getBar());
| , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
4. Configure Tomcat's Resource Factory
To configure Tomcat's resource factory, add an elements like this to the
$CATALINA_HOME/conf/server.xml file, nested inside the
Context element for this web application (or nested inside
a DefaultContext element for the surrounding
<Host> or <Engine> element.
, Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
<Context ...>
...
<Resource name="bean/MyBeanFactory" auth="Container"
type="com.mycompany.MyBean"/>
<ResourceParams name="bean/MyBeanFactory">
<parameter>
<name>factory</name>
<value>com.mycompany.MyBeanFactory</value>
</parameter>
<parameter>
<name>bar</name>
<value>23</value>
</parameter>
</ResourceParams>
...
</Context>
| , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) | , Section 12. For a more introductory level
document for web application developers as well as administrators, see
(FIXME - link to backgrounder on container managed security
to be provided).
For information about utilizing the Single Sign On feature of
Tomcat 4 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
here.
A Realm is a "database" of usernames and passwords that
identify valid users of a web application (or set of web applications), plus
an enumeration of the list of roles associated with each valid user.
You can think of roles as similar to groups in Unix-like operating
systems, because access to specific web application resources is granted to
all users possessing a particular role (rather than enumerating the list of
associated usernames). A particular user can have any number of roles
associated with their username.
Although the Servlet Specification describes a portable mechanism for
applications to declare their security requirements (in the
web.xml deployment descriptor), there is no portable API
defining the interface between a servlet container and the associated user
and role information. In many cases, however, it is desireable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat 4
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Three standard plug-ins are provided, supporting connection to three different
sources of authentication information:
JDBCRealm - Accesses authentication information
stored in a relational database, accessed via a JDBC driver.
JNDIRealm - Accesses authentication information
stored in an LDAP based directory server, accessed via a JNDI provider.
MemoryRealm - Accesses authentication
information stored in an in-memory object collection, which is initialized
from an XML document (conf/tomcat-users.xml).
It is also possible to write your own Realm implementation,
and integrate it with Tomcat 4. However, doing this is beyond the scope of
this document. See (FIXME - reference to developer stuff)
for more information.
Before getting into the details of the standard Realm implementations, it is
important to understand, in general terms, how a Realm is configured. In
general, you will be adding an XML element to your conf/server.xml
configuration file, that looks something like this:
<Realm className="... class name for this implementation"
... other attributes for this implementation .../>
The <Realm> element can be nested inside one of three
different elements, which has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
Inside an <Engine> element - This Realm will be shared
across ALL web applications on ALL virtual hosts, UNLESS it is overridden
by a Realm element nested inside a subordinate <Host>
or <Context> element.
Inside a <Host> element - This Realm will be shared across
ALL web applications for THIS virtual host, UNLESS it is overridden
by a Realm element nested inside a subordinate <Context>
element.
Inside a <Context> element - This Realm will be used ONLY
for THIS web application.
Introduction
JDBCRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a relational database
accessed via a JDBC driver. There is substantial configuration flexibility
that lets you adapt to existing table and column names, as long as your
database structure conforms to the following requirements:
There must be a table, referenced below as the users table,
that contains one row for every valid user that this Realm
should recognize.
The users table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat when the user logs in.
Password to be recognized by Tomcat when the user logs in.
This value may in cleartext or digested - see below for more
information.
There must be a table, referenced below as the user roles table,
that contains one row for every valid role that is assigned to a
particular user. It is legal for a user to have zero, one, or more than
one valid role.
The user roles table must contain at least two columns (it may
contain more if your existing applications required it):
Username to be recognized by Tomcat (same value as is specified
in the users table).
Role name of a valid role associated with this user.
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your database
that conform to the requirements described above.
Configure a database username and password for use by Tomcat, that has
at least read only access to the tables described above. (Tomcat will
never attempt to write to these tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Note that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The database username used to establish a JDBC connection.
The database password used to establish a JDBC connection.
The database URL used to establish a JDBC connection.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The fully qualified Java class name of the JDBC driver to be used.
Consult the documentation for your JDBC driver for the appropriate
value.
The name of the column, in the user roles table, that
contains the name of a role assigned to this user.
The name of the column, in the users table, that contains
the password for this user (either in clear text, or digested if the
digest attribute is set).
The name of the column, in the users and user roles
tables, that contains the username of this user.
The name of the table that contains one row for each role
assigned to a particular username. This table must include at
least the columns named by the userNameCol and
roleNameCol attributes.
The name of the table that contains one row for each username
to be recognized by Tomcat. This table must include at least the columns
named by the userNameCol and userCredCol
attributes.
Example
An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
Example Realm elements are included (commented out) in the
default $CATALINA_HOME/conf/server.xml file. Here's an example
for using a MySQL database called "authority", configured with the tables
described above, and accessed with username "dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Additional Notes
JDBCRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the users and user roles
table is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
JNDIRealm is an implementation of the Tomcat 4
Realm interface that looks up users in a directory server
accessed by a JNDI provider (typically, the standard LDAP provider that
is available with the JNDI API classes). There is substantial configuration
flexibility that lets you adapt to the existing schema inside your directory
server, as long as it conforms to the following requirements:
Each user that can be authenticated is represented by an individual
element in the top level DirContext that is accessed
via the connectionURL attribute.
The user element must have the following characteristics:
The distinguished name (dn) attribute of this element
contains the username that is presented for authentication.
There must be an attribute (identified by the userPassword
attribute of our Realm element) that contains the user's
password, either in clear text or digested (see below for more info).
Each group of users that has been assigned a particular role is
represented by an individual element in the top level
DirContext that is accessed via the
connectionURL attribute.
The user group element must have the following characteristics:
The set of all possible groups of interest can be selected by an LDAP
search pattern configured by the roleSearch attribute
of our Realm element.
The roleSearch pattern optionally includes pattern
replacements "{0}" for the distinguished name, and/or "{1} for the
username, of the authenticated user for which roles will be
retrieved.
The roleBase attribute can be set to the element that
is the base of the search for matching roles. If not specified,
the entire directory context will be searched.
The roleSubtree attribute can be set to true
if you wish to search the entire subtree of the directory context.
The default value of false requests a search of only the
current level.
The element includes an attribute (whose name is configured by the
roleName attribute of our Realm element)
containing the name of the role represented by this element.
There must be an administrator username and password that Tomcat can
use to establish a connection to the directory server, with at least
read-only access to the information described above. A future
version of Tomcat will support an option to use the user's username and
password to attempt this connection.
Quick Start
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
Make sure your directory server is configured with a schema that matches
the requirements listed above.
Configure a username and password for use by Tomcat, that has
at least read only access to the information described above. (Tomcat will
never attempt to modify this information.)
Place a copy of the JNDI driver you will be using (typically
ldap.jar available with JNDI) inside the
$CATALINA_HOME/server/lib directory (if you do not need it
visible to web applications) or $CATALINA_HOME/common/lib
(if it will be used both by Tomcat 4 and by your apps).
Set up a <Realm> element, as described below, in your
$CATALINA_HOME/conf/server.xml file.
Restart Tomcat 4 if it is already running.
Realm Element Attributes
To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.JDBCRealm" here.
The directory server username used to establish a JNDI connection.
The directory server password used to establish a JNDI connection.
The directory server URL used to establish a JNDI connection.
The fully qualified Java class name of the JNDI context factory to be
used for this connection. By default, the standard JNDI LDAP provider
is used (com.sun.jndi.ldap.LdapCtxFactory).
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
The base element for role searches. If not specified, the top level
element in the directory context will be used.
The name of the directory server attribute containing the role name.
An LDAP search pattern for selecting roles in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want roles for, and/or {1} to substitute in the username of
the user you want roles for.
Set to true if you want role searches to search subtrees
of the element selected by roleBase. The default value of
false causes only the top level element to be searched.
The name of the directory server attribute (in the user element) that
contains the cleartext or digested user password (depending on the setting
of the digest attribute).
An LDAP search pattern for selecting users in this Realm, following the
syntax supported by the java.text.MessageFormat class. Use
{0} to substitute in the distinguished name of the user you
want to select.
Example
Creation of the appropriate schema in your directory server is beyond the
scope of this document, because it is unique to each directory server
implementation. In the examples below, we will assume that you are using a
distribution of the OpenLDAP directory server (version 2.0.11 or later), which
can be downloaded from
http://www.openldap.org. Assume that
your slapd.conf file contains the following settings
(among others):
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
These settings help us identify values for the values to be specified for
connectionName, and connectionPassword, and we
will assume for connectionURL that the directory server runs on
the same machine as Tomcat. See
http://java.sun.com/products/jndi/docs.html
for more information about configuring and using the JNDI LDAP provider.
Next, assume that this directory server has been populated with elements
as shown below (in LDIF format), which define the same users and roles
as the default $CATALINA_HOME/conf/tomcat-users.xml does for
MemoryRealm:
# Define a user named 'tomcat'
dn: cn=tomcat,dc=mycompany,dc=com
cn: tomcat
userPassword: tomcat
sn: Tomcat User
objectClass: person
# Define a user named 'role1'
dn: cn=role1,dc=mycompany,dc=com
cn: role1
userPassword: tomcat
sn: Role1 User
objectClass: person
# Define a user named 'both'
dn: cn=both,dc=mycompany,dc=com
cn: both
userPassword: tomcat
sn: Both User
objectClass: person
# Define an entry to base role searches on
dn: dc=roles,dc=mycompany,dc=com
cn: roles
objectClass: person
sn: Roles Entry
# Define all members of the 'tomcat' role
dn: cn=tomcat,dc=roles,dc=mycompany,dc=com
cn: tomcat
objectClass: groupOfUniqueNames
uniqueMember: cn=tomcat,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
# Define all members of the 'role1' role
dn: cn=role1,dc=roles,dc=mycompany,dc=com
cn: role1
objectClass: groupOfUniqueNames
uniqueMember: cn=role1,dc=mycompany,dc=com
uniqueMember: cn=both,dc=mycompany,dc=com
An example Realm element for the OpenLDAP directory server
configured as described above might look like this:
<Realm className="org.apache.catalina.realm.JNDIRealm" debug="99"
connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
connectionURL="ldap://localhost:389"
roleBase="dc=roles,dc=mcclan,dc=net"
roleName="cn"
roleSearch="(uniqueMember={0})"
roleSubtree="false"
userPassword="userPassword"
userPattern="cn={0},dc=mycompany,dc=com"
/>
Additional Notes
JNDIRealm operates according to the following rules:
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser). Any changes to the database information for an
already authenticated user will not be reflected until
the next time that user logs on again.
Administering the information in the directory server
is the responsibility of your own applications. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
Introduction
MemoryRealm is a simple demonstration implementation of the
Tomcat 4 Realm interface. It is not designed for production use.
At startup time, MemoryRealm loads information about all users, and their
corresponding roles, from an XML document (by default, this document is loaded from $CATALINA_HOME/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
Realm Element Attributes
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_HOME/conf/server.xml file,
as described above. The following
attributes are supported by this implementation:
The fully qualified Java class name of this Realm implementation.
You MUST specify the value
"org.apache.catalina.realm.MemoryRealm" here.
The level of debugging detail logged by this Realm
to the associated Logger. Higher numbers
generate more detailed output. If not specified, the default
debugging detail level is zero (0).
The digest algorithm used to store passwords in non-plaintext formats.
Valid values are those accepted for the algorithm name by the
java.security.MessageDigest class. See
Digested Passwords for more
information. If not specified, passwords are stored in clear text.
Absolute or relative (to $CATALINA_HOME) pathname of the XML document
containing our valid usernames, passwords, and roles. See below for more
information on the format of this file. If not specified, the value
conf/tomcat-users.xml is used.
User File Format
The users file (by default, conf/tomcat-users.xml must be an
XML document, with a root element <tomcat-users>. Nested
inside the root element will be a <user> element for each
valid user, consisting of the following attributes:
name - Username this user must log on with.
password - Password this user must log on with (in
clear text if the digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).
roles - Comma-delimited list of the role names
associated with this user.
Example
The default installation of Tomcat 4 is configured with a MemoryRealm
nested inside the <Engine> element, so that it applies
to all virtual hosts and web applications. The default contents of the
conf/tomcat-users.xml file is:
<tomcat-users>
<user name="tomcat" password="tomcat" roles="tomcat" />
<user name="role1" password="tomcat" roles="role1" />
<user name="both" password="tomcat" roles="tomcat,role1" />
</tomcat-users>
Additional Notes
MemoryRealm operates according to the following rules:
When Tomcat first starts up, it loads all defined users and their
associated information from the users file. Changes to the data in
this file will not be recognized until Tomcat is
restarted.
When a user attempts to access a protected resource for the first time,
Tomcat 4 will call the authenticate() method of this
Realm.
Once a user has been authenticated, the user (and his or her associated
roles) are cached within Tomcat for the duration of the user's login.
(For FORM-based authentication, that means until the session times out or
is invalidated; for BASIC authentication, that means until the user
closes their browser).
Administering the information in the users file is the responsibility
of your application. Tomcat does not
provide any built-in capabilities to maintain users and roles.
Debugging and exception messages logged by this Realm will
be recorded by the Logger that is associated with our
surrounding Context, Host, or
Engine. By default, the corresponding Logger will create a
log file in the $CATALINA_HOME/logs directory.
For each of the standard Realm implementations, the user's
password (by default) is stored in clear text. In many environments, this is
undesireable because casual observers of the authentication data can collect
enough information to log on successfully, and impersonate other users.
To avoid this problem, the standard implementations support the concept of
digesting user passwords. This causes the stored version of the
passwords to be encoded (in a form that is not easily reversible), but that
the Realm implementation can still utilize for authentication.
Digested passwords are selected by specifying the digest
attribute on your <Realm> element. The value for this
attribute must be one of the digest algorithms supported by the
java.security.MessageDigest class (SHA, MD2, or MD5). When you
select this option, the contents of the password that is stored in the
Realm must be the cleartext version of the password, as digested
by the specified algorithm.
When the authenticate() method of the Realm is called, the
(cleartext) password specified by the user is itself digested by the same
algorithm, and the result is compared with the value returned by the
Realm. An equal match implies that the cleartext version of the
original password is the same as the one presented by the user, so that this
user should be authorized.
To calculate the digested value of a cleartext password, two convenience
techniques are supported:
If you are writing an application that needs to calculate digested
passowrds dynamically, call the static Digest() method of the
org.apache.catalina.realm.RealmBase class, passing the
cleartext password and the digest algorithm name as arguments. This
method will return the digested password.
If you want to execute a command line utility to calculate the digested
password, simply execute
java org.apache.catalina.realm.RealmBase \
-a {algorithm} {cleartext-password}
and the digested version of this cleartext password will be returned to
standard output.
To use either of the above techniques, the
$CATALINA_HOME/server/lib/catalina.jar file will need to be
on your class path to make the RealmBase class available.
The example application shipped with Tomcat 4 includes an area that is
protected by a security constraint, utilizing form-based login. To access it,
point your browser at
http://localhost:8080/examples/jsp/security/protected/
and log on with one of the usernames and passwords described for the default
MemoryRealm.
If you wish to use the Manager Application
to deploy and undeploy applications in a running Tomcat 4 installation, you
MUST add the "manager" role to at least one username in your selected Realm
implementation. This is because the manager web application itself uses a
security constraint that requires role "manager" to access ANY request URI
within that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager" role. Therfore,
no one will be able to utilize the features of this application until the
Tomcat administrator specifically assigns this role to one or more users.
/images/void.gif) |
Note that the resource name (here, bean/MyBeanFactory
must match the value specified in the web application deployment
descriptor. We are also initializing the value of the bar
property, which will cause setBar(23) to be called before
the new bean is returned. Because we are not initializing the
foo property (although we could have), the bean will
contain whatever default value is set up by its constructor.
You will also note that, from the application developer's perspective,
the declaration of the resource environment reference, and the programming
used to request new instances, is identical to the approach used for the
Generic JavaBean Resources example. This illustrates one of the
advantages of using JNDI resources to encapsulate functionality - you can
change the underlying implementation without necessarily having to
modify applications using the resources, as long as you maintain
compatible APIs.
|
|