Links Getting Started Administrators Application Developers Catalina Developers Jasper Developers | SSL Configuration HOW-TOIntroduction to SSL |
SSL, or Secure Socket Layer, is a technology which allows web browsers and
web servers to communicate over a secured connection. This means that the data
being sent is encrypted by one side, transmitted, then decrypted by the other
side before processing. This is a two-way process, meaning that both the
server AND the browser encrypt all traffic before sending out data.
Another important aspect of the SSL protocol is Authentication. This means
that during your initial attempt to communicate with a web server over a secure
connection, that server will present your web browser with a set of
credentials, in the form of a "Certificate", as proof the site is who and what
it claims to be. In certain cases, the server may also request a Certificate
from your web browser, asking for proof that you are who you claim
to be. This is known as "Client Authentication," although in practice this is
used more for business-to-business (B2B) transactions than with individual
users. Most SSL-enabled web servers do not request Client Authentication.
|
SSL and Tomcat |
It is important to note that configuring Tomcat to take advantage of
secure sockets is usually only necessary when running it as a stand-alone
web server. When running Tomcat primarily as a Servlet/JSP container behind
another web server, such as Apache or Microsoft IIS, it is usually necessary
to configure the primary web server to handle the SSL connections from users.
Typically, this server will negotiate all SSL-related functionality, then
pass on any requests destined for the Tomcat container only after decrypting
those requests. Likewise, Tomcat will return cleartext responses, that will
be encrypted before being returned to the user's browser. In this environment,
Tomcat knows that communications between the primary web server and the
client are taking place over a secure connection (because your application
needs to be able to ask about this), but it does not participate in the
encryption or decryption itself.
|
Certificates |
In order to implement SSL, a web server must have an associated Certificate
for each external interface (IP address) that accepts secure connections.
The theory behind this design is that a server should provide some kind of
reasonable assurance that its owner is who you think it is, particularly
before receiving any sensitive information. While a broader explanation of
Certificates is beyond the scope of this document, think of a Certificate
as a "digital driver's license" for an Internet address. It states what
company the site is associated with, along with some basic contact
information about the site owner or administrator.
This "driver's license" is cryptographically signed by its owner, and is
therefore extremely difficult for anyone else to forge. For sites involved
in e-commerce, or any other business transaction in which authentication of
identity is important, a Certificate is typically purchased from a well-known
Certificate Authority (CA) such as VeriSign or Thawte. Such
certificates can be electronically verified -- in effect, the Certificate
Authority will vouch for the authenticity of the certificates that it grants,
so you can believe that that Certificate is valid if you trust the Certificate
Authority that granted it.
In many cases, however, authentication is not really a concern. An
administrator may simply want to ensure that the data being transmitted and
received by the server is private and cannot be snooped by anyone who may be
eavesdropping on the connection. Fortunately, Java provides a relatively
simple command-line tool, called keytool , which can easily create
a "self-signed" Certificate. Self-signed Certificates are simply user
generated Certificates which have not been officially registered with any
well-known CA, and are therefore not really guaranteed to be authentic at all.
Again, this may or may not even be important, depending on your needs.
|
General Tips on Running SSL |
The first time a user attempts to access a secured page on your site,
he or she is typically presented with a dialog containing the details of
the certificate (such as the company and contact name), and asked if he or she
wishes to accept the Certificate as valid and continue with the transaction.
Some browsers will provide an option for permanently accepting a given
Certificate as valid, in which case the user will not be bothered with a
prompt each time they visit your site. Other browsers do not provide this
option. Once approved by the user, a Certificate will be considered valid
for at least the entire browser session.
Also, while the SSL protocol was designed to be as efficient as securely
possible, encryption/decryption is a computationally expensive process from
a performance standpoint. It is not strictly necessary to run an entire
web application over SSL, and indeed a developer can pick and choose which
pages require a secure connection and which do not. For a reasonably busy
site, it is customary to only run certain pages under SSL, namely those
pages where sensitive information could possibly be exchanged. This would
include things like login pages, personal information pages, and shopping
cart checkouts, where credit card information could possibly be transmitted.
Any page within an application can be requested over a secure socket by
simply prefixing the address with https: instead of
http: . Any pages which absolutely require
a secure connection should check the protocol type associated with the
page request and take the appropriate action of https is not
specified.
Finally, using name-based virtual hosts on a secured connection can be
problematic. This is a design limitation of the SSL protocol itself. The SSL
handshake, where the client browser accepts the server certificate, must occur
before the HTTP request is accessed. As a result, the request information
containing the virtual host name cannot be determined prior to authentication,
and it is therefore not possible to assign multiple certificates to a single
IP address. If all virtual hosts on a single IP address need to authenticate
against the same certificate, the addition of multiple virtual hosts should not
interfere with normal SSL operations on the server. Be aware, however, that
most client browsers will compare the server's domain name against the domain
name listed in the certificate, if any (applicable primarily to official,
CA-signed certificates). If the domain names do not match, these browsers will
display a warning to the client user. In general, only address-based virtual
hosts are commonly used with SSL in a production environment.
|
Configuration |
Download and Install JSSE |
Download the Java Secure Socket Extensions (JSSE) package,
version 1.0.2 or later, from
http://java.sun.com/products/jsse/.
If you built Tomcat from source, you have probably already downloaded this
package. If you are running JDK 1.4 (currently in beta), these classes have
been integrated directly into the JDK, so you can skip this entire step.
After expanding the package, there are two ways to make it available to
Tomcat (choose one or the other):
- Make JSSE an installed extension by copying all three JAR files
(
jcert.jar , jnet.jar , and jsse.jar )
into your $JAVA_HOME/jre/lib/ext directory.
- Create a new environment variable
JSSE_HOME that contains
the absolute path to the directory into which you unpacked the
JSSE binary distribution.
|
Prepare the Certificate Keystore |
Tomcat currently operates only on JKS format keystores. This
is Java's standard "Java KeyStore" format, and is the format created by the
keytool command-line utility. This tool is included in the JDK.
To import an existing certificate into a JKS keystore, please read the
documentation (in your JDK documentation package) about keytool .
To create a new keystore from scratch, containing a single self-signed
Certificate, execute the following from a terminal command line:
Windows:
, 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) |
%JAVA_HOME%\bin\keytool -genkey -alias tomcat -keyalg RSA
| , 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) |
Unix:
, 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) |
$JAVA_HOME/bin/keytool -genkey -alias tomcat -keyalg RSA
| , 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) |
(The RSA algorithm should be preferred as a secure algorithm, and this
also ensures general compatibility with other servers and components.)
This command will create a new file, in the home directory of the user
under which you run it, named ".keystore ". To specify a
different location or filename, add the -keystore parameter,
followed by the complete pathname to your keystore file,
to the keytool command shown above. You will also need to
reflect this new location in the server.xml configuration file,
as described later. For example:
Windows:
, 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) |
%JAVA_HOME%\bin\keytool -genkey -alias tomcat -keyalg RSA \
-keystore \path\to\my\keystore
| , 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) |
Unix:
, 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) |
$JAVA_HOME/bin/keytool -genkey -alias tomcat -keyalg RSA \
-keystore /path/to/my/keystore
| , 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) |
After executing this command, you will first be prompted for the keystore
password. The default password used by Tomcat is "changeit "
(all lower case), although you can specify a custom password if you like.
You will also need to specify the custom password in the
server.xml configuration file, as described later.
Next, you will be prompted for general information about this Certificate,
such as company, contact name, and so on. This information will be displayed
to users who attempt to access a secure page in your application, so make
sure that the information provided here matches what they will expect.
Finally, you will be prompted for the key password, which is the
password specifically for this Certificate (as opposed to any other
Certificates stored in the same keystore file). You MUST
use the same password here as was used for the keystore password itself.
(Currently, the keytool prompt will tell you that pressing the
ENTER key does this for you automatically.)
If everything was successful, you now have a keystore file with a
Certificate that can be used by your server.
|
Edit the Tomcat Configuration File |
The final step is to configure your secure socket in the
$CATALINA_HOME/conf/server.xml file, where
$CATALINA_HOME represents the directory into which you
installed Tomcat 4. An example <Connector> element
for an SSL connector is included in the default server.xml
file installed with Tomcat. It will 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) |
<-- Define an SSL HTTP/1.1 Connector on port 8443 -->
<!--
<Connector className="org.apache.catalina.connector.http.HttpConnector"
port="8443" minProcessors="5" maxProcessors="75"
enableLookups="true"
acceptCount="10" debug="0" scheme="https" secure="true">
<Factory className="org.apache.catalina.net.SSLServerSocketFactory"
clientAuth="false" protocol="TLS"/>
</Connector>
-->
| , 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) |
You will note that the Connector element itself is commented out by default,
so you will need to remove the comment tags around it. Then, you can
customize the specified attributes as necessary. For detailed information
about the various options, consult the
Server Configuration Reference. The
following discussion covers only those attributes of most interest when
setting up SSL communication.
The port attribute (default value is 8443) is the TCP/IP
port number on which Tomcat will listen for secure connections. You can
change this to any port number you wish (such as to the default port for
https communications, which is 443). However, special setup
(outside the scope of this document) is necessary to run Tomcat on port
numbers lower than 1024 on many operating systems.
If you change the port number here, you should also change the
value specified for the redirectPort attribute on the
non-SSL connector. This allows Tomcat to automatically redirect
users who attempt to access a page with a security constraint specifying
that SSL is required, as required by the Servlet 2.3 Specification.
You will notice a Factory element nested inside the
Connector element. This is where the "socket factory" used
by Tomcat, whenever it needs a socket on the corresponding port number,
is configured. You may need to add or change the following attribute
values, depending on how you configured your keystore earlier:
Attribute |
Description |
className |
The fully qualified class name of the Java class that implements
this socket factory. Do not change the default value. |
clientAuth |
Set this value to true if you want Tomcat to require
all SSL clients to present a client Certificate in order to use
this socket. |
keystoreFile |
Add this attribute if the keystore file you created is not in
the default place that Tomcat expects (a file named
.keystore in the user home directory under
which Tomcat is running). You can specify an absolute pathname,
or a relative pathname that is resolved against the
$CATALINA_BASE environment variable. |
keystorePass |
Add this element if you used a different keystore (and Certificate)
password than the one Tomcat expects (changeit ). |
protocol |
The encryption/decryption protocol to be used on this socket.
Do not change the default value. |
After completing these configuration changes, you must restart Tomcat as
you normally do, and you should be in business. You should be able to access
any web application supported by Tomcat via SSL. For example, try:
and you should see the usual Tomcat splash page (unless you have modified
the ROOT web application). If this does not work, the following section
contains some troubleshooting tips.
|
|
Troubleshooting |
Here is a list of common problems that you may encounter when setting up
SSL communications, and what to do about them.
- I get "java.security.NoSuchAlgorithmException" errors in my
log files.
The JVM cannot find the JSSE JAR files. Follow all of the directions to
download and install JSSE.
- When Tomcat starts up, I get an exception like
"java.io.FileNotFoundException: {some-directory}/{some-file} not found".
A likely explanation is that Tomcat cannot find the keystore file
where it is looking. By default, Tomcat expects the keystore file to
be named .keystore in the user home directory under which
Tomcat is running (which may or may not be the same as yours :-). If
the keystore file is anywhere else, you will need to add a
keystoreFile attribute to the <Factory>
element in the Tomcat
configuration file.
- When Tomcat starts up, I get an exception like
"java.io.FileNotFoundException: Keystore was tampered with, or
password was incorrect".
Assuming that someone has not actually tampered with
your keystore file, the most likely cause is that Tomcat is using
a different password than the one you used when you created the
keystore file. To fix this, you can either go back and
recreate the keystore
file, or you can add or update the keystorePass
attribute on the <Factory> element in the
Tomcat configuration
file. REMINDER - Passwords are case sensitive!
If you are still having problems, a good source of information is the
TOMCAT-USER mailing list. You can find pointers to archives
of previous messages on this list, as well as subscription and unsubscription
information, at
http://jakarta.apache.org/site/mail.html".
|
|