]>
This document describes how to configure Tomcat to support container
managed security, by connecting to an existing "database" of usernames,
passwords, and user roles. You only need to care about this if you are using
a web application that includes one or more
For fundamental background information about container managed security,
see the Servlet
Specification (Version 2.4), Section 12. For information about utilizing the Single Sign On feature of
Tomcat (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
It is also possible to write your own <security-constraint> elements, and a
<login-config> element defining how users are required
to authenticate themselves. If you are not utilizing these features, you can
safely skip this document.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 desirable to "connect"
a servlet container to some existing authentication database or mechanism
that already exists in the production environment. Therefore, Tomcat
defines a Java interface (org.apache.catalina.Realm) that
can be implemented by "plug in" components to establish this connection.
Five standard plug-ins are provided, supporting connections to various
sources of authentication information:
conf/tomcat-users.xml).conf/tomcat-users.xml).Realm implementation,
and integrate it with Tomcat. To do so, you need to:
org.apache.catalina.Realm,
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:
The <Realm> element can be nested inside any one of
of the following Container elements. The location of the
Realm element has a direct impact on the "scope" of that Realm
(i.e. which web applications will share the same authentication information):
<Host>
or <Context> element.<Context>
element.For each of the standard Realm implementations, the
user's password (by default) is stored in clear text. In many
environments, this is undesirable 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 allows 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.
When a standard realm authenticates by retrieving the stored
password and comparing it with the value presented by the user, you
can select digested passwords 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:
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 using digested passwords with DIGEST authentication, the cleartext used
to generate the digest is different. In the examples above
{cleartext-password} must be replaced with
{username}:{realm}:{cleartext-password}. For example, in a
development environment this might take the form
testUser:Authentication required:testPassword. The value for
{realm} is taken from the <realm-name>
element of the web application's <login-config>. If
not specified in web.xml, the default value of Authentication
required is used.
Non-ASCII usernames and/or passwords are supported using
{input}:{digest}. If the input appears
corrupted in the return, the digest will be invalid.
The example application shipped with Tomcat 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 UserDatabaseRealm.
If you wish to use the Manager Application to deploy and undeploy applications in a running Tomcat installation, you MUST add the "manager-gui" 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-gui" to access ANY request URI within the HTML interface of that application.
For security reasons, no username in the default Realm (i.e. using
conf/tomcat-users.xml is assigned the "manager-gui" role.
Therefore, 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.
Debugging and exception messages logged by a Realm will
be recorded by the logging configuration associated with the container
for the realm: its surrounding Context,
Host, or
Engine.
JDBCRealm is an implementation of the Tomcat
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:
Realm
should recognize.To set up Tomcat to use JDBCRealm, you will need to follow these steps:
$CATALINA_HOME/lib directory.
Note that only JAR files are recognized!<Realm> element, as described below, in your
$CATALINA_BASE/conf/server.xml file.To configure JDBCRealm, you will create a <Realm>
element and nest it in your $CATALINA_BASE/conf/server.xml file,
as described above. The attributes for the
JDBCRealm are defined in the Realm configuration
documentation.
An example SQL script to create the needed tables might look something like this (adapt the syntax as required for your particular database):
Example Realm elements are included (commented out) in the
default $CATALINA_BASE/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":
JDBCRealm operates according to the following rules:
authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.DataSourceRealm is an implementation of the Tomcat
Realm interface that looks up users in a relational database
accessed via a JNDI named JDBC DataSource. 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:
Realm
should recognize.To set up Tomcat to use DataSourceRealm, you will need to follow these steps:
<Realm> element, as described below, in your
$CATALINA_BASE/conf/server.xml file.To configure DataSourceRealm, you will create a <Realm>
element and nest it in your $CATALINA_BASE/conf/server.xml file,
as described above. The attributes for the
DataSourceRealm are defined in the Realm
configuration documentation.
An example SQL script to create the needed tables might look something like this (adapt the syntax as required for your particular database):
Here is an example for using a MySQL database called "authority", configured with the tables described above, and accessed with the JNDI JDBC DataSource with name "java:/comp/env/jdbc/authority".
DataSourceRealm operates according to the following rules:
authenticate() method of this
Realm. Thus, any changes you have made to the database
directly (new users, changed passwords or roles, etc.) will be immediately
reflected.JNDIRealm is an implementation of the Tomcat
Realm interface that looks up users in an LDAP directory
server accessed by a JNDI provider (typically, the standard LDAP
provider that is available with the JNDI API classes). The realm
supports a variety of approaches to using a directory for
authentication.
The realm's connection to the directory is defined by the connectionURL configuration attribute. This is a URL whose format is defined by the JNDI provider. It is usually an LDAP URL that specifies the domain name of the directory server to connect to, and optionally the port number and distinguished name (DN) of the required root naming context.
If you have more than one provider you can configure an alternateURL. If a socket connection can not be made to the provider at the connectionURL an attempt will be made to use the alternateURL.
When making a connection in order to search the directory and retrieve user and role information, the realm authenticates itself to the directory with the username and password specified by the connectionName and connectionPassword properties. If these properties are not specified the connection is anonymous. This is sufficient in many cases.
Each user that can be authenticated must be represented in the
directory by an individual entry that corresponds to an element in the
initial DirContext defined by the
connectionURL attribute. This user entry must have an
attribute containing the username that is presented for
authentication.
Often the distinguished name of the user's entry contains the username presented for authentication but is otherwise the same for all users. In this case the userPattern attribute may be used to specify the DN, with "{0}" marking where the username should be substituted.
Otherwise the realm must search the directory to find a unique entry containing the username. The following attributes configure this search:
true if you wish to search the entire subtree
rooted at the userBase entry. The default value
of false requests a single-level search
including only the top level.Bind mode
By default the realm authenticates a user by binding to the directory with the DN of the entry for that user and the password presented by the user. If this simple bind succeeds the user is considered to be authenticated.
For security reasons a directory may store a digest of the user's password rather than the clear text version (see Digested Passwords for more information). In that case, as part of the simple bind operation the directory automatically computes the correct digest of the plaintext password presented by the user before validating it against the stored value. In bind mode, therefore, the realm is not involved in digest processing. The digest attribute is not used, and will be ignored if set.
Comparison mode
Alternatively, the realm may retrieve the stored password from the directory and compare it explicitly with the value presented by the user. This mode is configured by setting the userPassword attribute to the name of a directory attribute in the user's entry that contains the password.
Comparison mode has some disadvantages. First, the connectionName and connectionPassword attributes must be configured to allow the realm to read users' passwords in the directory. For security reasons this is generally undesirable; indeed many directory implementations will not allow even the directory manager to read these passwords. In addition, the realm must handle password digests itself, including variations in the algorithms used and ways of representing password hashes in the directory. However, the realm may sometimes need access to the stored password, for example to support HTTP Digest Access Authentication (RFC 2069). (Note that HTTP digest authentication is different from the storage of password digests in the repository for user information as discussed above).
The directory realm supports two approaches to the representation of roles in the directory:
Roles as explicit directory entries
Roles may be represented by explicit directory entries. A role entry is usually an LDAP group entry with one attribute containing the name of the role and another whose values are the distinguished names or usernames of the users in that role. The following attributes configure a directory search to find the names of roles associated with the authenticated user:
true if you wish to search the entire
subtree rooted at the roleBase entry. The default
value of false requests a single-level search
including the top level only.true if you want to nest roles in roles. If configured
every newly found roleName and distinguished
Name will be recursively tried for a new role search.
The default value is false.Roles as an attribute of the user entry
Role names may also be held as the values of an attribute in the user's directory entry. Use userRoleName to specify the name of this attribute.
A combination of both approaches to role representation may be used.
To set up Tomcat to use JNDIRealm, you will need to follow these steps:
ldap.jar available with JNDI) inside the
$CATALINA_HOME/lib directory.<Realm> element, as described below, in your
$CATALINA_BASE/conf/server.xml file.To configure JNDIRealm, you will create a <Realm>
element and nest it in your $CATALINA_BASE/conf/server.xml file,
as described above. The attributes for the
JNDIRealm are defined in the Realm configuration
documentation.
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):
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):
An example Realm element for the OpenLDAP directory
server configured as described above might look like this, assuming
that users use their uid (e.g. jjones) to login to the
application and that an anonymous connection is sufficient to search
the directory and retrieve role information:
With this configuration, the realm will determine the user's
distinguished name by substituting the username into the
userPattern, authenticate by binding to the directory
with this DN and the password received from the user, and search the
directory to find the user's roles.
Now suppose that users are expected to enter their email address rather than their userid when logging in. In this case the realm must search the directory for the user's entry. (A search is also necessary when user entries are held in multiple subtrees corresponding perhaps to different organizational units or company locations).
Further, suppose that in addition to the group entries you want to use an attribute of the user's entry to hold roles. Now the entry for Janet Jones might read as follows:
This realm configuration would satisfy the new requirements:
Now when Janet Jones logs in as "j.jones@mycompany.com", the realm
searches the directory for a unique entry with that value as its mail
attribute and attempts to bind to the directory as
uid=jjones,ou=people,dc=mycompany,dc=com with the given
password. If authentication succeeds, she is assigned three roles:
"role2" and "role3", the values of the "memberOf" attribute in her
directory entry, and "tomcat", the value of the "cn" attribute in the
only group entry of which she is a member.
Finally, to authenticate the user by retrieving the password from the directory and making a local comparison in the realm, you might use a realm configuration like this:
However, as discussed above, the default bind mode for authentication is usually to be preferred.
JNDIRealm operates according to the following rules:
authenticate() method of this
Realm. Thus, any changes you have made to the directory
(new users, changed passwords or roles, etc.) will be immediately
reflected.UserDatabaseRealm is an implementation of the Tomcat
Realm interface that uses a JNDI resource to store user
information. By default, the JNDI resource is backed by an XML file. It is not
designed for large-scale production use. At startup time, the UserDatabaseRealm
loads information about all users, and their corresponding roles, from an XML
document (by default, this document is loaded from
$CATALINA_BASE/conf/tomcat-users.xml). The users, their passwords
and their roles may all be editing dynamically, typically via JMX. Changes may
be saved and will be reflected in the XML file.
To configure UserDatabaseRealm, you will create a <Realm>
element and nest it in your $CATALINA_BASE/conf/server.xml file,
as described above. The attributes for the
UserDatabaseRealm are defined in the Realm
configuration documentation.
The users file uses the same format as the MemoryRealm.
The default installation of Tomcat is configured with a UserDatabaseRealm
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:
UserDatabaseRealm operates according to the following rules:
authenticate() method of this
Realm.MemoryRealm is a simple demonstration implementation of the
Tomcat 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_BASE/conf/tomcat-users.xml). Changes to the data
in this file are not recognized until Tomcat is restarted.
To configure MemoryRealm, you will create a <Realm>
element and nest it in your $CATALINA_BASE/conf/server.xml file,
as described above. The attributes for the
MemoryRealm are defined in the Realm
configuration documentation.
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:
digest attribute was not set on the
<Realm> element, or digested appropriately as
described here otherwise).MemoryRealm operates according to the following rules:
authenticate() method of this
Realm.JAASRealm is an implementation of the Tomcat
6 Realm interface that authenticates users through the Java
Authentication & Authorization Service (JAAS) framework which is now
provided as part of the standard Java SE API.
Using JAASRealm gives the developer the ability to combine practically any conceivable security realm with Tomcat's CMA.
JAASRealm is prototype for Tomcat of the JAAS-based J2EE authentication framework for J2EE v1.4, based on the JCP Specification Request 196 to enhance container-managed security and promote 'pluggable' authentication mechanisms whose implementations would be container-independent.
Based on the JAAS login module and principal (see javax.security.auth.spi.LoginModule
and javax.security.Principal), you can develop your own
security mechanism or wrap another third-party mechanism for
integration with the CMA as implemented by Tomcat.
To set up Tomcat to use JAASRealm with your own JAAS login module, you will need to follow these steps:
javax.security.auth.login.LoginContext)
When developing your LoginModule, note that JAASRealm's built-in CallbackHandler
only recognizes the NameCallback and PasswordCallback at present.
javax.security.Principal,
so that Tomcat can tell which Principals returned from your login
module are users and which are roles (see org.apache.catalina.realm.JAASRealm).
Regardless, the first Principal returned is always treated as the user Principal.
JAVA_OPTS=$JAVA_OPTS -Djava.security.auth.login.config==$CATALINA_BASE/conf/jaas.configTo configure JAASRealm as for step 6 above, you create
a <Realm> element and nest it in your
$CATALINA_BASE/conf/server.xml
file within your <Engine> node. The attributes for the
JAASRealm are defined in the Realm
configuration documentation.
Here is an example of how your server.xml snippet should look.
It is the responsibility of your login module to create and save User and
Role objects representing Principals for the user
(javax.security.auth.Subject). If your login module doesn't
create a user object but also doesn't throw a login exception, then the
Tomcat CMA will break and you will be left at the
http://localhost:8080/myapp/j_security_check URI or at some other
unspecified location.
The flexibility of the JAAS approach is two-fold:
authenticate()
method of this Realm. Thus, any changes you have made in
the security mechanism directly (new users, changed passwords or
roles, etc.) will be immediately reflected.Realm implementations, digested passwords
are supported if the <Realm> element in server.xml
contains a digest attribute; JAASRealm's CallbackHandler
will digest the password prior to passing it back to the LoginModuleCombinedRealm is an implementation of the Tomcat
Realm interface that authenticates users through one or more
sub-Realms.
Using CombinedRealm gives the developer the ability to combine multiple Realms of the same or different types. This can be used to authenticate against different sources, provide fall back in case one Realm fails or for any other purpose that requires multiple Realms.
Sub-realms are defined by nesting Realm elements inside the
Realm element that defines the CombinedRealm. Authentication
will be attempted against each Realm in the order they are
listed. Authentication against any Realm will be sufficient to authenticate
the user.
To configure a CombinedRealm, you create a <Realm>
element and nest it in your $CATALINA_BASE/conf/server.xml
file within your <Engine> or <Host>.
You can also nest inside a <Context> node in a
context.xml file.
Here is an example of how your server.xml snippet should look to use a UserDatabase Realm and a DataSource Realm.
LockOutRealm is an implementation of the Tomcat
Realm interface that extends the CombinedRealm to provide lock
out functionality to provide a user lock out mechanism if there are too many
failed authentication attempts in a given period of time.
To ensure correct operation, there is a reasonable degree of synchronisation in this Realm.
This Realm does not require modification to the underlying Realms or the associated user storage mecahisms. It achieves this by recording all failed logins, including those for users that do not exist. To prevent a DOS by deliberating making requests with invalid users (and hence causing this cache to grow) the size of the list of users that have failed authentication is limited.
Sub-realms are defined by nesting Realm elements inside the
Realm element that defines the LockOutRealm. Authentication
will be attempted against each Realm in the order they are
listed. Authentication against any Realm will be sufficient to authenticate
the user.
To configure a LockOutRealm, you create a <Realm>
element and nest it in your $CATALINA_BASE/conf/server.xml
file within your <Engine> or <Host>.
You can also nest inside a <Context> node in a
context.xml file. The attributes for the
LockOutRealm are defined in the Realm
configuration documentation.
Here is an example of how your server.xml snippet should look to add lock out functionality to a UserDatabase Realm.