<?xml version="1.0" encoding="Shift_JIS"?>
<!DOCTYPE document [
  <!ENTITY project SYSTEM "project.xml">
]>
<document url="realm-howto.html">

    &project;

    <properties>
        <author email="craigmcc@apache.org">Craig R. McClanahan</author>
        <primary><title>Realm Configuration HOW-TO</title></primary>
        <title>レルム設定方法</title>
        <translator>小山 博史</translator>
        <editor>田中良浩</editor>
    </properties>

<body>


<primary>
<section name="Table of Contents">

<p>
<a href="#Quick Start">Quick Start</a><br />
<blockquote>
<a href="#What is a Realm?">What is a Realm?</a><br />
<a href="#Configuring a Realm">Configuring a Realm</a><br />
</blockquote>
<a href="#Standard Realm Implementations">
Standard Realm Implementations</a><br />
<blockquote>
<a href="#JDBCRealm">JDBCRealm</a><br />
<a href="#DataSourceRealm">DataSourceRealm</a><br />
<a href="#JNDIRealm">JNDIRealm</a><br />
<a href="#MemoryRealm">MemoryRealm</a><br />
</blockquote>
<a href="#Common Features">Common Features</a><br />
<blockquote>
<a href="#Digested Passwords">Digested Passwords</a><br />
<a href="#Example Application">Example Application</a><br />
<a href="#Manager Application">Manager Application</a><br />
</blockquote>
</p>

</section>
</primary>

<section name="目次">

<p>
<a href="#クイックスタート">クイックスタート</a><br />
<a href="#概要">概要</a><note>原文では抜けている</note><br />
<blockquote>
<a href="#レルムとは?">レルムとは?</a><br />
<a href="#レルムの設定">レルムの設定</a><br />
</blockquote>
<a href="#標準レルム実装">標準レルム実装</a><br />
<blockquote>
<a href="#JDBCレルム">JDBCレルム</a><br />
<a href="#データソースレルム">データソースレルム</a><br />
<a href="#JNDIレルム">JNDIレルム</a><br />
<a href="#メモリレルム">メモリレルム</a><br />
</blockquote>
<a href="#共通機能">共通機能</a><br />
<blockquote>
<a href="#ダイジェストパスワード">ダイジェストパスワード</a><br />
<a href="#Example アプリケーション">Example アプリケーション</a><br />
<a href="#マネージャアプリケーション">マネージャアプリケーション</a><br />
</blockquote>
</p>

</section>


<primary>
<section name="Quick Start">

<p>This document describes how to configure Tomcat to support <em>container
managed security</em>, 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
<code>&lt;security-constraint&gt;</code> elements, and a
<code>&lt;login-config&gt;</code> element defining how users are required
to authenticate themselves.  If you are not utilizing these features, you can
safely skip this document.</p>

<p>For fundamental background information about container managed security,
see the <a href="http://java.sun.com/products/servlet/download.html">Servlet
Specification (Version 2.3)</a>, Section 12.  For a more introductory level
document for web application developers as well as administrators, see
(<strong>FIXME</strong> - link to backgrounder on container managed security
to be provided).</p>

<p>For information about utilizing the <em>Single Sign On</em> feature of
Tomcat 5 (allowing a user to authenticate themselves once across the entire
set of web applications associated with a virtual host), see
<a href="config/host.html#Single Sign On">here</a>.</p>

</section>
</primary>
<section name="クイックスタート">

<p>この文書は、ユーザ名、パスワード、ユーザロールが登録されている"データベース"へ接続して動作する、
Tomcatにおける<em>コンテナ管理のセキュリティ</em>を利用するための設定方法について説明しています。
<code>&lt;security-constraint&gt;</code> 要素や<code>&lt;login-config&gt;</code> 要素といった、
ユーザがどのように認証されるかを設定する要素を１つ以上含むWebアプリケーションを使う方だけが気をつけて読んで下さい。
これらの機能を利用しないのであれば、この文書を読み飛ばしても大丈夫です。
</p>

<p>コンテナ管理のセキュリティに関する基本的な情報については、
<a href="http://java.sun.com/products/servlet/download.html">Servlet 仕様書(バージョン2.3)</a>
の第12節を参照してください。

管理者やWebアプリケーション開発者のための入門レベルの文書は、
(<strong>未解決</strong> -コンテナ管理のセキュリティに関する情報へのリンクを提供すること)
を参照してください。</p>

<p>
Tomcat 5 における<em>統一ログイン[Single Sign On]</em> 
(バーチャルホスト内で連携するWebアプリケーション全体を一度の認証でユーザが利用できるようになる)機能の使用方法に関する情報については、
<a href="config/host.html#Single Sign On">こちら</a>を参照してください。</p>

</section>


<primary>
<section name="Overview">


<subsection name="What is a Realm?">

<p>A <strong>Realm</strong> 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 <em>roles</em> associated with each valid user.
You can think of roles as similar to <em>groups</em> 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.</p>

<p>Although the Servlet Specification describes a portable mechanism for
applications to <em>declare</em> their security requirements (in the
<code>web.xml</code> 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 5
defines a Java interface (<code>org.apache.catalina.Realm</code>) 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:</p>
<ul>
<li><a href="#DataSourceRealm">DataSourceRealm</a> - Accesses authentication
    information stored in a relational database, accessed via a named JNDI
    JDBC DataSource.</li>
<li><a href="#JDBCRealm">JDBCRealm</a> - Accesses authentication information
    stored in a relational database, accessed via a JDBC driver.</li>
<li><a href="#JNDIRealm">JNDIRealm</a> - Accesses authentication information
    stored in an LDAP based directory server, accessed via a JNDI provider.
    </li>
<li><a href="#MemoryRealm">MemoryRealm</a> - Accesses authentication
    information stored in an in-memory object collection, which is initialized
    from an XML document (<code>conf/tomcat-users.xml</code>).</li>
</ul>

<p>It is also possible to write your own <code>Realm</code> implementation,
and integrate it with Tomcat 5.  However, doing this is beyond the scope of
this document.  See (<strong>FIXME</strong> - reference to developer stuff)
for more information.</p>

</subsection>


<subsection name="Configuring a Realm">

<p>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 <code>conf/server.xml</code>
configuration file, that looks something like this:</p>

<source>
&lt;Realm className="... class name for this implementation"
       ... other attributes for this implementation .../&gt;
</source>

<p>The <code>&lt;Realm&gt;</code> 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):
</p>
<ul>
<li><em>Inside an &lt;Engine&gt; element</em> - 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 <code>&lt;Host&gt;</code>
    or <code>&lt;Context&gt;</code> element.</li>
<li><em>Inside a &lt;Host&gt; element</em> - 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 <code>&lt;Context&gt;</code>
    element.</li>
<li><em>Inside a &lt;Context&gt; element</em> - This Realm will be used ONLY
    for THIS web application.</li>
</ul>


</subsection>


</section>
</primary>
<section name="概要">


<subsection name="レルムとは?">

<p><strong>レルム</strong>とは、ユーザ名とパスワードの組み合わせのように、
Webアプリケーション(複数のWebアプリケーションのまとまりの場合もあります)
のユーザを一意に定めるための"データベース"と、
認証された各ユーザに付与されている<em>ロール</em>の一覧を列挙するものの両方を合わせたものを指します。
ロールは、Unix系のオペレーティングシステムにおける<em>グループ</em>
のようなものだと考えてもらってもいいでしょう。
特定のWebアプリケーションリソースへのアクセスは、
(リソースへ関連づけられたユーザ名の一覧を列挙することによってではなく)
ある特別なロールを持つユーザ全員に与えられます。
場合によってユーザは、ユーザ名に紐付くロールをいくつでも持つことができます。</p>

<p>
(<code>web.xml</code>、つまり配備記述子に)
セキュリティ要件を<em>宣言</em>することによるアプリケーションのための可搬性の高い機構をサーブレット仕様書では説明していますが、
サーブレットコンテナと関連付けられたユーザとロール情報との間のインタフェースを定義する可搬性の高いAPIはありません。
しかしながら多くの場合、認証用データベースまたは製品環境に付属する認証機構とサーブレットコンテナとの"接続"が必要となります。
そのためTomcat 5では、Javaのインタフェース(<code>org.apache.catalina.Realm</code>)を定義しています。
このインタフェースは、こういった接続を確立するための"プラグイン"コンポーネントに実装されれば良く、
異なる認証情報ソースへの接続をサポートする以下の4つの標準プラグインが現在提供されています:</p>
<ul>
<li><a href="#データソースレルム">DataSourceRealm</a> - 
    リレーショナルデータベースに保存されている認証情報へアクセスします。
    名前付けされた JNDI JDBCデータソースを使ってアクセスします。</li>
<li><a href="#JDBCレルム">JDBCRealm</a> - 
    リレーショナルデータベースに保存されている認証情報へアクセスします。
    JDBCドライバを使ってアクセスします。</li>
<li><a href="#JNDIレルム">JNDIRealm</a> - 
    LDAPベースのディレクトリサーバに保存されている認証情報へアクセスします。
    JNDIプロバイダを使ってアクセスします。</li>
<li><a href="#メモリレルム">MemoryRealm</a> - 
    メモリ内のオブジェクト集合に保存されている認証情報へアクセスします。
    このオブジェクト集合は、XML文書ファイル (<code>conf/tomcat-users.xml</code>)を使って初期化されます。</li>
</ul>

<p>もちろん、開発者は独自に<code>Realm</code> を実装することができますし、
それをTomcat 5 と統合することもできます。
しかしながらその方法を説明することはこの文書の範疇を越えています。
詳細については、(<strong>未解決</strong> - 開発スタッフ用リファレンス)を参照してください。</p>

</subsection>


<subsection name="レルムの設定">

<p>標準レルム実装の詳細に入る前に、レルムの設定で使われる基本的な用語について理解することが肝心です。
   通常は、<code>conf/server.xml</code>設定ファイルに次のようにXML要素を追加します:</p>

<source>
&lt;Realm className="... 実装クラス名"
       ... 実装に対応するその他の属性 .../&gt;
</source>

<p><code>&lt;Realm&gt;</code> 要素は、3種類の異なる要素内で入れ子で指定することができます
(すなわち、Webアプリケーションはいくつかの認証情報を共有します)。
ただし、そのレルムの"有効範囲"内で直接衝突が発生します:
</p>
<ul>
<li><em>&lt;Engine&gt; 要素内で指定</em> - 
    このレルムは*すべての*仮想ホスト上のWebアプリケーション*全部*において共有されます。
    ただし、従属する<code>&lt;Host&gt;</code>や<code>&lt;Context&gt;</code>要素に含まれる
入れ子のレルム要素によって無効にされる場合を*除きます*。</li>
<li><em>&lt;Host&gt; 要素内で指定</em> - 
    このレルムは、*指定された*仮想ホスト上のWebアプリケーション*全部*において共有されます。
    ただし、従属する<code>&lt;Context&gt;</code>要素に含まれる入れ子のレルム要素によって
無効にされる場合を*除きます*。</li>
<li><em>&lt;Context&gt; 要素内で指定</em> - 
    このレルムは、*指定された*Webアプリケーション*のみ*で利用できます。</li>
</ul>


</subsection>


</section>


<primary>
<section name="Standard Realm Implementations">


<subsection name="JDBCRealm">

<h3>Introduction</h3>

<p><strong>JDBCRealm</strong> is an implementation of the Tomcat 5
<code>Realm</code> 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:</p>
<ul>
<li>There must be a table, referenced below as the <em>users</em> table,
    that contains one row for every valid user that this <code>Realm</code>
    should recognize.</li>
<li>The <em>users</em> table must contain at least two columns (it may
    contain more if your existing applications required it):
    <ul>
    <li>Username to be recognized by Tomcat when the user logs in.</li>
    <li>Password to be recognized by Tomcat when the user logs in.
        This value may in cleartext or digested - see below for more
        information.</li>
    </ul></li>
<li>There must be a table, referenced below as the <em>user roles</em> 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.</li>
<li>The <em>user roles</em> table must contain at least two columns (it may
    contain more if your existing applications required it):
    <ul>
    <li>Username to be recognized by Tomcat (same value as is specified
        in the <em>users</em> table).</li>
    <li>Role name of a valid role associated with this user.</li>
    </ul></li>
</ul>

<h3>Quick Start</h3>

<p>To set up Tomcat to use JDBCRealm, you will need to follow these steps:</p>
<ol>
<li>If you have not yet done so, create tables and columns in your database
    that conform to the requirements described above.</li>
<li>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.)</li>
<li>Place a copy of the JDBC driver you will be using inside the
    <code>$CATALINA_HOME/server/lib</code> directory (if you do not need it
    visible to web applications) or <code>$CATALINA_HOME/common/lib</code>
    (if it will be used both by Tomcat 5 <em>and</em> by your apps).
    Note that <strong>only</strong> JAR files are recognized!</li>
<li>Set up a <code>&lt;Realm&gt;</code> element, as described below, in your
    <code>$CATALINA_HOME/conf/server.xml</code> file.</li>
<li>Restart Tomcat 5 if it is already running.</li>
</ol>

<h3>Realm Element Attributes</h3>

<p>To configure JDBCRealm, you will create a <code>&lt;Realm&gt;</code>
element and nest it in your <code>$CATALINA_HOME/conf/server.xml</code> file,
as described <a href="#Configuring a Realm">above</a>.  The following
attributes are supported by this implementation:</p>

<attributes>

  <attribute name="className" required="true">
    <p>The fully qualified Java class name of this Realm implementation.
    You <strong>MUST</strong> specify the value
    "<code>org.apache.catalina.realm.JDBCRealm</code>" here.</p>
  </attribute>

  <attribute name="connectionName" required="true">
    <p>The database username used to establish a JDBC connection.</p>
  </attribute>

  <attribute name="connectionPassword" required="true">
    <p>The database password used to establish a JDBC connection.</p>
  </attribute>

  <attribute name="connectionURL" required="true">
    <p>The database URL used to establish a JDBC connection.</p>
  </attribute>

  <attribute name="debug" required="false">
    <p>The level of debugging detail logged by this Realm
    to the associated <a href="config/logger.html">Logger</a>.  Higher numbers
    generate more detailed output.  If not specified, the default
    debugging detail level is zero (0).</p>
  </attribute>

  <attribute name="digest" required="false">
    <p>The digest algorithm used to store passwords in non-plaintext formats.
    Valid values are those accepted for the algorithm name by the
    <code>java.security.MessageDigest</code> class.  See
    <a href="#Digested Passwords">Digested Passwords</a> for more
    information.  If not specified, passwords are stored in clear text.</p>
  </attribute>

  <attribute name="driverName" required="true">
    <p>The fully qualified Java class name of the JDBC driver to be used.
    Consult the documentation for your JDBC driver for the appropriate
    value.</p>
  </attribute>

  <attribute name="roleNameCol" required="true">
    <p>The name of the column, in the <em>user roles</em> table, that
    contains the name of a role assigned to this user.</p>
  </attribute>

  <attribute name="userCredCol" required="true">
    <p>The name of the column, in the <em>users</em> table, that contains
    the password for this user (either in clear text, or digested if the
    <code>digest</code> attribute is set).</p>
  </attribute>

  <attribute name="userNameCol" required="true">
    <p>The name of the column, in the <em>users</em> and <em>user roles</em>
    tables, that contains the username of this user.</p>
  </attribute>

  <attribute name="userRoleTable" required="true">
    <p>The name of the table that contains one row for each <em>role</em>
    assigned to a particular <em>username</em>.  This table must include at
    least the columns named by the <code>userNameCol</code> and
    <code>roleNameCol</code> attributes.</p>
  </attribute>

  <attribute name="userTable" required="true">
    <p>The name of the table that contains one row for each <em>username</em>
    to be recognized by Tomcat.  This table must include at least the columns
    named by the <code>userNameCol</code> and <code>userCredCol</code>
    attributes.</p>
  </attribute>

</attributes>

<h3>Example</h3>

<p>An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):</p>
<source>
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)
);
</source>

<p>Example <code>Realm</code> elements are included (commented out) in the
default <code>$CATALINA_HOME/conf/server.xml</code> 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":</p>
<source>
&lt;Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
      driverName="org.gjt.mm.mysql.Driver"
   connectionURL="jdbc:mysql://localhost/authority?user=dbuser&amp;amp;password=dbpass"
       userTable="users" userNameCol="user_name" userCredCol="user_pass"
   userRoleTable="user_roles" roleNameCol="role_name"/&gt;
</source>

<h3>Additional Notes</h3>

<p>JDBCRealm operates according to the following rules:</p>
<ul>
<li>When a user attempts to access a protected resource for the first time,
    Tomcat 5 will call the <code>authenticate()</code> method of this
    <code>Realm</code>.  Thus, any changes you have made to the database
    directly (new users, changed passwords or roles, etc.) will be immediately
    reflected.</li>
<li>Once a user has been authenticated, the user (and his or her associated
    roles) are cached within Tomcat for the duration of the user's login.
    (For FORM-based authentication, that means until the session times out or
    is invalidated; for BASIC authentication, that means until the user
    closes their browser).  Any changes to the database information for an
    already authenticated user will <strong>not</strong> be reflected until
    the next time that user logs on again.</li>
<li>Administering the information in the <em>users</em> and <em>user roles</em>
    table is the responsibility of your own applications.  Tomcat does not
    provide any built-in capabilities to maintain users and roles.</li>
<li>Debugging and exception messages logged by this <code>Realm</code> will
    be recorded by the <code>Logger</code> that is associated with our
    surrounding <code>Context</code>, <code>Host</code>, or
    <code>Engine</code>.  By default, the corresponding Logger will create a
    log file in the <code>$CATALINA_HOME/logs</code> directory.</li>
</ul>

</subsection>


<subsection name="DataSourceRealm">

<h3>Introduction</h3>

<p><strong>DataSourceRealm</strong> is an implementation of the Tomcat 4
<code>Realm</code> 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:</p>
<ul>
<li>There must be a table, referenced below as the <em>users</em> table,
    that contains one row for every valid user that this <code>Realm</code>
    should recognize.</li>
<li>The <em>users</em> table must contain at least two columns (it may
    contain more if your existing applications required it):
    <ul>
    <li>Username to be recognized by Tomcat when the user logs in.</li>
    <li>Password to be recognized by Tomcat when the user logs in.
        This value may in cleartext or digested - see below for more
        information.</li>
    </ul></li>    
<li>There must be a table, referenced below as the <em>user roles</em> 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.</li>
<li>The <em>user roles</em> table must contain at least two columns (it may
    contain more if your existing applications required it):
    <ul>
    <li>Username to be recognized by Tomcat (same value as is specified
        in the <em>users</em> table).</li>
    <li>Role name of a valid role associated with this user.</li>
    </ul></li>    
</ul>

<h3>Quick Start</h3>
                  
<p>To set up Tomcat to use DataSourceRealm, you will need to follow these steps:</p>
<ol>              
<li>If you have not yet done so, create tables and columns in your database
    that conform to the requirements described above.</li>
<li>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.)</li>
<li>Configure a JNDI named JDBC DataSource for your database.  Refer to the
    <a href="jndi-datasource-examples-howto.html">JNDI DataSource Example HOW-TO</a>
    for information on how to configure a JNDI named JDBC DataSource.</li>
<li>Set up a <code>&lt;Realm&gt;</code> element, as described below, in your
    <code>$CATALINA_HOME/conf/server.xml</code> file.</li>
<li>Restart Tomcat 4 if it is already running.</li>
</ol>

<h3>Realm Element Attributes</h3>

<p>To configure DataSourceRealm, you will create a <code>&lt;Realm&gt;</code>
element and nest it in your <code>$CATALINA_HOME/conf/server.xml</code> file,
as described <a href="#Configuring a Realm">above</a>.  The following
attributes are supported by this implementation:</p>

<attributes>

  <attribute name="className" required="true">
    <p>The fully qualified Java class name of this Realm implementation.
    You <strong>MUST</strong> specify the value
    "<code>org.apache.catalina.realm.DataSourceRealm</code>" here.</p>
  </attribute>

  <attribute name="dataSourceName" required="true">
    <p>The JNDI named JDBC DataSource for your database.</p>
  </attribute>

  <attribute name="debug" required="false">
    <p>The level of debugging detail logged by this Realm
    to the associated <a href="config/logger.html">Logger</a>.  Higher numbers
    generate more detailed output.  If not specified, the default
    debugging detail level is zero (0).</p>
  </attribute>
    
  <attribute name="digest" required="false">
    <p>The digest algorithm used to store passwords in non-plaintext formats.
    Valid values are those accepted for the algorithm name by the
    <code>java.security.MessageDigest</code> class.  See
    <a href="#Digested Passwords">Digested Passwords</a> for more
    information.  If not specified, passwords are stored in clear text.</p>
  </attribute>
    
  <attribute name="roleNameCol" required="true">
    <p>The name of the column, in the <em>user roles</em> table, that
    contains the name of a role assigned to this user.</p>
  </attribute>
    
  <attribute name="userCredCol" required="true">
    <p>The name of the column, in the <em>users</em> table, that contains
    the password for this user (either in clear text, or digested if the
    <code>digest</code> attribute is set).</p>
  </attribute>
    
  <attribute name="userNameCol" required="true">
    <p>The name of the column, in the <em>users</em> and <em>user roles</em>
    tables, that contains the username of this user.</p>
  </attribute>

  <attribute name="userRoleTable" required="true">
    <p>The name of the table that contains one row for each <em>role</em>
    assigned to a particular <em>username</em>.  This table must include at
    least the columns named by the <code>userNameCol</code> and
    <code>roleNameCol</code> attributes.</p>
  </attribute>

  <attribute name="userTable" required="true">
    <p>The name of the table that contains one row for each <em>username</em>
    to be recognized by Tomcat.  This table must include at least the columns
    named by the <code>userNameCol</code> and <code>userCredCol</code>
    attributes.</p>
  </attribute>

</attributes>

<h3>Example</h3>

<p>An example SQL script to create the needed tables might look something
like this (adapt the syntax as required for your particular database):</p>
<source>
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)
);
</source>

<p>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".</p>
<source>
&lt;Realm className="org.apache.catalina.realm.DataSourceRealm" debug="99"
   dataSourceName="java:/comp/env/jdbc/authority"
   userTable="users" userNameCol="user_name" userCredCol="user_pass"
   userRoleTable="user_roles" roleNameCol="role_name"/&gt;
</source>

<h3>Additional Notes</h3>

<p>DataSourceRealm operates according to the following rules:</p>
<ul>
<li>When a user attempts to access a protected resource for the first time,
    Tomcat 4 will call the <code>authenticate()</code> method of this
    <code>Realm</code>.  Thus, any changes you have made to the database
    directly (new users, changed passwords or roles, etc.) will be immediately
    reflected.</li>
<li>Once a user has been authenticated, the user (and his or her associated
    roles) are cached within Tomcat for the duration of the user's login.
    (For FORM-based authentication, that means until the session times out or
    is invalidated; for BASIC authentication, that means until the user
    closes their browser).  Any changes to the database information for an
    already authenticated user will <strong>not</strong> be reflected until
    the next time that user logs on again.</li>
<li>Administering the information in the <em>users</em> and <em>user roles</em>
    table is the responsibility of your own applications.  Tomcat does not
    provide any built-in capabilities to maintain users and roles.</li>
<li>Debugging and exception messages logged by this <code>Realm</code> will
    be recorded by the <code>Logger</code> that is associated with our
    surrounding <code>Context</code>, <code>Host</code>, or
    <code>Engine</code>.  By default, the corresponding Logger will create a
    log file in the <code>$CATALINA_HOME/logs</code> directory.</li>
</ul>

</subsection>


<subsection name="JNDIRealm">

<h3>Introduction</h3>

<p><strong>JNDIRealm</strong> is an implementation of the Tomcat 5
<code>Realm</code> 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.</p>

<h4>Connecting to the directory</h4>

<p>The realm's connection to the directory is defined by the
<strong>connectionURL</strong> 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.</p>

<p>If you have more than one provider you can configure an
<strong>alternateURL</strong>.  If a socket connection can not be
made to the provider at the <strong>connectionURL</strong> an
attempt will be made to use the <strong>alternateURL</strong>.</p>

<p>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
<strong>connectionName</strong> and
<strong>connectionPassword</strong> properties. If these properties
are not specified the connection is anonymous. This is sufficient in
many cases.
</p>


<h4>Selecting the user's directory entry</h4>

<p>Each user that can be authenticated must be represented in the
directory by an individual entry that corresponds to an element in the
initial <code>DirContext</code> defined by the
<strong>connectionURL</strong> attribute. This user entry must have an
attribute containing the username that is presented for
authentication.</p>

<p>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 <strong>userPattern</strong> attribute may
be used to specify the DN, with "{0}" marking where
the username should be substituted.</p>

<p>Otherwise the realm must search the directory to find a unique entry
containing the username. The following attributes configure this
search:

     <ul>
     <li><strong>userBase</strong> - the entry that is the base of
         the subtree containing users.  If not specified, the search
         base is the top-level context.</li>

     <li><strong>userSubtree</strong> - the search scope. Set to
         <code>true</code> if you wish to search the entire subtree
         rooted at the <strong>userBase</strong> entry. The default value
         of <code>false</code> requests a single-level search
         including only the top level.</li>

     <li><strong>userSearch</strong> - pattern specifying the LDAP
         search filter to use after substitution of the username.</li>

    </ul>
</p>


<h4>Authenticating the user</h4>

<ul>
<li>
<p><b>Bind mode</b></p>

<p>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.</p>

<p>For security reasons a directory may store a digest of the user's
password rather than the clear text version (see <a href="#Digested
Passwords">Digested Passwords</a> 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
<strong>digest</strong> attribute is not used, and will be ignored if
set.</p>
</li>

<li>
<p><b>Comparison mode</b></p>
<p>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
<strong>userPassword</strong> attribute to the name of a directory
attribute in the user's entry that contains the password.</p>

<p>Comparison mode has some disadvantages. First, the
<strong>connectionName</strong> and
<strong>connectionPassword</strong> 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).
</p>
</li>
</ul>

<h4>Assigning roles to the user</h4>

<p>The directory realm supports two approaches to the representation
of roles in the directory:</p>

<ul>
<li>
<p><b>Roles as explicit directory entries</b></p>

<p>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:</p>

<ul>
<li><strong>roleBase</strong> - the base entry for the role search.
    If not specified, the search base is the top-level directory
    context.</li>

<li><strong>roleSubtree</strong> - the search
    scope. Set to <code>true</code> if you wish to search the entire
    subtree rooted at the <code>roleBase</code> entry. The default
    value of <code>false</code> requests a single-level search
    including the top level only.</li>

<li><strong>roleSearch</strong> - the LDAP search filter for
    selecting role entries. It optionally includes pattern
    replacements "{0}" for the distinguished name and/or "{1}" for the
    username of the authenticated user.</li>

<li><strong>roleName</strong> - the attribute in a role entry
     containing the name of that role.</li>

</ul>

</li>
</ul>

<ul>
<li>
<p><b>Roles as an attribute of the user entry</b></p>

<p>Role names may also be held as the values of an attribute in the
user's directory entry. Use <strong>userRoleName</strong> to specify
the name of this attribute.</p>

</li>
</ul>
<p>A combination of both approaches to role representation may be used.</p>

<h3>Quick Start</h3>

<p>To set up Tomcat to use JNDIRealm, you will need to follow these steps:</p>
<ol>
<li>Make sure your directory server is configured with a schema that matches
    the requirements listed above.</li>
<li>If required, configure a username and password for use by Tomcat, that has
    read only access to the information described above.  (Tomcat will
    never attempt to modify this information.)</li>
<li>Place a copy of the JNDI driver you will be using (typically
    <code>ldap.jar</code> available with JNDI) inside the
    <code>$CATALINA_HOME/server/lib</code> directory (if you do not need it
    visible to web applications) or <code>$CATALINA_HOME/common/lib</code>
    (if it will be used both by Tomcat 5 <em>and</em> by your apps).</li>
<li>Set up a <code>&lt;Realm&gt;</code> element, as described below, in your
    <code>$CATALINA_HOME/conf/server.xml</code> file.</li>
<li>Restart Tomcat 5 if it is already running.</li>
</ol>

<h3>Realm Element Attributes</h3>

<p>To configure JNDIRealm, you will create a <code>&lt;Realm&gt;</code>
element and nest it in your <code>$CATALINA_HOME/conf/server.xml</code> file,
as described <a href="#Configuring a Realm">above</a>.  The following
attributes are supported by this implementation:</p>

<attributes>
  <attribute name="className" required="true">
    <p>The fully qualified Java class name of this Realm implementation.
    You <strong>MUST</strong> specify the value
    "<code>org.apache.catalina.realm.JNDIRealm</code>" here.</p>
  </attribute>


      <attribute name="connectionName" required="false">
        <p>The directory username to use when establishing a
        connection to the directory for LDAP search operations. If not
        specified an anonymous connection is made, which is often
        sufficient unless you specify the <code>userPassword</code>
        property.</p>
      </attribute>

      <attribute name="connectionPassword" required="false">
        <p>The directory password to use when establishing a
        connection to the directory for LDAP search operations. If not
        specified an anonymous connection is made, which is often
        sufficient unless you specify the <code>userPassword</code>
        property.</p>
      </attribute>

      <attribute name="connectionURL" required="true">
        <p>The connection URL to be passed to the JNDI driver when
        establishing a connection to the directory.</p>
      </attribute>

      <attribute name="contextFactory" required="false">
        <p>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
        (<code>com.sun.jndi.ldap.LdapCtxFactory</code>).</p>
      </attribute>

      <attribute name="debug" required="false">
      <p>The level of debugging detail logged by this Realm to the
      associated <a href="config/logger.html">Logger</a>.  Higher
      numbers generate more detailed output.  If not specified, the
      default debugging detail level is zero (0).</p>
      </attribute>

      <attribute name="digest" required="false">
        <p>The digest algorithm to apply to the plaintext password offered
        by the user before comparing it with the value retrieved from the
        directory.  Valid values are those accepted for the algorithm name
        by the <code>java.security.MessageDigest</code> class.  See <a
        href="#Digested Passwords">Digested Passwords</a> for more
        information. If not specified the plaintext password is assumed to
        be retrieved. Not required unless <code>userPassword</code> is
        specified</p>
      </attribute>

      <attribute name="roleBase" required="false">
        <p>The base directory entry for performing role searches. If
        not specified, the top level element in the directory context
        will be used.</p>
      </attribute>

      <attribute name="roleName" required="false">
        <p>The name of the attribute that contains role names in the
        directory entries found by a role search. In addition you can
        use the <code>userRoleName</code> property to specify the name
        of an attribute, in the user's entry, containing additional
        role names.  If <code>roleName</code> is not specified a role
        search does not take place, and roles are taken only from the
        user's entry.</p>
      </attribute>

      <attribute name="roleSearch" required="false">
        <p>The LDAP filter expression used for performing role
        searches, following the syntax supported by the
        <code>java.text.MessageFormat</code> class.  Use
        <code>{0}</code> to substitute the distinguished name (DN) of
        the user, and/or <code>{1}</code> to substitute the
        username. If not specified a role search does not take place
        and roles are taken only from the attribute in the user's
        entry specified by the <code>userRoleName</code> property.</p>
      </attribute>

      <attribute name="roleSubtree" required="false">
        <p>Set to <code>true</code> if you want to search the entire
        subtree of the element specified by the <code>roleBase</code>
        property for role entries associated with the user. The
        default value of <code>false</code> causes only the top level
        to be searched.</p>
      </attribute>

      <attribute name="userBase" required="false">
        <p>The base element for user searches performed using the
        <code>userSearch</code> expression.  If not specified, the top
        level element in the directory context will be used. Not used
        if you are using the <code>userPattern</code> expression.</p>
      </attribute>

      <attribute name="userPassword" required="false">
        <p>Name of the attribute in the user's entry containing the
        user's password.  If you specify this value, JNDIRealm will
        bind to the directory using the values specified by
        <code>connectionName</code> and
        <code>connectionPassword</code> properties, and retrieve the
        corresponding attribute for comparison to the value specified
        by the user being authenticated.  If the <code>digest</code>
        attribute is set, the specified digest algorithm is applied to
        the password offered by the user before comparing it with the
        value retrieved from the directory.  If you do
        <strong>not</strong> specify this value, JNDIRealm will
        attempt a simple bind to the directory using the DN of the
        user's entry and password specified by the user, with a
        successful bind being interpreted as an authenticated
        user.</p>
      </attribute>

      <attribute name="userPattern" required="false">
        <p>A pattern for the distinguished name (DN) of the user's
        directory entry, following the syntax supported by the
        <code>java.text.MessageFormat</code> class with
        <code>{0}</code> marking where the actual username should be
        inserted. You can use this property instead of
        <code>userSearch</code>, <code>userSubtree</code> and
        <code>userBase</code> when the distinguished name contains the
        username and is otherwise the same for all users.</p>
      </attribute>

      <attribute name="userRoleName" required="false">
        <p>The name of an attribute in the user's directory entry
        containing zero or more values for the names of roles assigned
        to this user.  In addition you can use the
        <code>roleName</code> property to specify the name of an
        attribute to be retrieved from individual role entries found
        by searching the directory. If <code>userRoleName</code> is
        not specified all the roles for a user derive from the role
        search.</p>
      </attribute>

      <attribute name="userSearch" required="false">
        <p>The LDAP filter expression to use when searching for a
        user's directory entry, with <code>{0}</code> marking where
        the actual username should be inserted.  Use this property
        (along with the <code>userBase</code> and
        <code>userSubtree</code> properties) instead of
        <code>userPattern</code> to search the directory for the
        user's entry.</p>
      </attribute>

      <attribute name="userSubtree" required="false">
        <p>Set to <code>true</code> if you want to search the entire
        subtree of the element specified by the <code>userBase</code>
        property for the user's entry. The default value of
        <code>false</code> causes only the top level to be searched.
        Not used if you are using the <code>userPattern</code>
        expression.</p>
      </attribute>

</attributes>

<h3>Example</h3>

<p>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
<a href="http://www.openldap.org">http://www.openldap.org</a>.  Assume that
your <code>slapd.conf</code> file contains the following settings
(among others):</p>
<source>
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
</source>

<p>We will assume for <code>connectionURL</code> that the directory
server runs on the same machine as Tomcat.  See <a
href="http://java.sun.com/products/jndi/docs.html">http://java.sun.com/products/jndi/docs.html</a>
for more information about configuring and using the JNDI LDAP
provider.</p>

<p>Next, assume that this directory server has been populated with elements
as shown below (in LDIF format):</p>

<source>

# Define top-level entry
dn: dc=mycompany,dc=com
objectClass: dcObject
dc:mycompany

# Define an entry to contain people
# searches for users are based on this entry
dn: ou=people,dc=mycompany,dc=com
objectClass: organizationalUnit
ou: people

# Define a user entry for Janet Jones
dn: uid=jjones,ou=people,dc=mycompany,dc=com
objectClass: inetOrgPerson
uid: jjones
sn: jones
cn: janet jones
mail: j.jones@mycompany.com
userPassword: janet

# Define a user entry for Fred Bloggs
dn: uid=fbloggs,ou=people,dc=mycompany,dc=com
objectClass: inetOrgPerson
uid: fbloggs
sn: bloggs
cn: fred bloggs
mail: f.bloggs@mycompany.com
userPassword: fred

# Define an entry to contain LDAP groups
# searches for roles are based on this entry
dn: ou=groups,dc=mycompany,dc=com
objectClass: organizationalUnit
ou: groups

# Define an entry for the "tomcat" role
dn: cn=tomcat,ou=groups,dc=mycompany,dc=com
objectClass: groupOfUniqueNames
cn: tomcat
uniqueMember: uid=jjones,ou=people,dc=mycompany,dc=com
uniqueMember: uid=fbloggs,ou=people,dc=mycompany,dc=com

# Define an entry for the "role1" role
dn: cn=role1,ou=groups,dc=mycompany,dc=com
objectClass: groupOfUniqueNames
cn: role1
uniqueMember: uid=fbloggs,ou=people,dc=mycompany,dc=com
</source>

<p>An example <code>Realm</code> 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:</p>

<source>
&lt;Realm   className="org.apache.catalina.realm.JNDIRealm" debug="99"
     connectionURL="ldap://localhost:389"
       userPattern="uid={0},ou=people,dc=mycompany,dc=com"
          roleBase="ou=groups,dc=mycompany,dc=com"
          roleName="cn"
        roleSearch="(uniqueMember={0})"
/&gt;
</source>

<p>With this configuration, the realm will determine the user's
distinguished name by substituting the username into the
<code>userPattern</code>, 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.</p>

<p>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).</p>

<p>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:</p>

<source>
dn: uid=jjones,ou=people,dc=mycompany,dc=com
objectClass: inetOrgPerson
uid: jjones
sn: jones
cn: janet jones
mail: j.jones@mycompany.com
memberOf: role2
memberOf: role3
userPassword: janet
</source>

<p> This realm configuration would satisfy the new requirements:</p>

<source>
&lt;Realm   className="org.apache.catalina.realm.JNDIRealm" debug="99"
     connectionURL="ldap://localhost:389"
          userBase="ou=people,dc=mycompany,dc=com"
        userSearch="(mail={0})"
      userRoleName="memberOf"
          roleBase="ou=groups,dc=mycompany,dc=com"
          roleName="cn"
        roleSearch="(uniqueMember={0})"
/&gt;
</source>

<p>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
<code>uid=jjones,ou=people,dc=mycompany,dc=com</code> 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.</p>

<p>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:</p>

<source>
&lt;Realm   className="org.apache.catalina.realm.JNDIRealm" debug="99"
    connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
     connectionURL="ldap://localhost:389"
      userPassword="userPassword"
       userPattern="uid={0},ou=people,dc=mycompany,dc=com"
          roleBase="ou=groups,dc=mycompany,dc=com"
          roleName="cn"
        roleSearch="(uniqueMember={0})"
/&gt;
</source>

<p>However, as discussed above, the default bind mode for
authentication is usually to be preferred.</p>

<h3>Additional Notes</h3>

<p>JNDIRealm operates according to the following rules:</p>
<ul>
<li>When a user attempts to access a protected resource for the first time,
    Tomcat 5 will call the <code>authenticate()</code> method of this
    <code>Realm</code>.  Thus, any changes you have made to the directory
    (new users, changed passwords or roles, etc.) will be immediately
    reflected.</li>
<li>Once a user has been authenticated, the user (and his or her associated
    roles) are cached within Tomcat for the duration of the user's login.
    (For FORM-based authentication, that means until the session times out or
    is invalidated; for BASIC authentication, that means until the user
    closes their browser).  Any changes to the directory information for an
    already authenticated user will <strong>not</strong> be reflected until
    the next time that user logs on again.</li>
<li>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.</li>
<li>Debugging and exception messages logged by this <code>Realm</code> will
    be recorded by the <code>Logger</code> that is associated with our
    surrounding <code>Context</code>, <code>Host</code>, or
    <code>Engine</code>.  By default, the corresponding Logger will create a
    log file in the <code>$CATALINA_HOME/logs</code> directory.</li>
</ul>

</subsection>


<subsection name="MemoryRealm">

<h3>Introduction</h3>

<p><strong>MemoryRealm</strong> is a simple demonstration implementation of the
Tomcat 5 <code>Realm</code> 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 <code>$CATALINA_HOME/conf/tomcat-users.xml</code>).  Changes to the data
in this file are not recognized until Tomcat is restarted.</p>

<h3>Realm Element Attributes</h3>

<p>To configure MemoryRealm, you will create a <code>&lt;Realm&gt;</code>
element and nest it in your <code>$CATALINA_HOME/conf/server.xml</code> file,
as described <a href="#Configuring a Realm">above</a>.  The following
attributes are supported by this implementation:</p>

<attributes>

  <attribute name="className" required="true">
    <p>The fully qualified Java class name of this Realm implementation.
    You <strong>MUST</strong> specify the value
    "<code>org.apache.catalina.realm.MemoryRealm</code>" here.</p>
  </attribute>

  <attribute name="debug" required="false">
    <p>The level of debugging detail logged by this Realm
    to the associated <a href="config/logger.html">Logger</a>.  Higher numbers
    generate more detailed output.  If not specified, the default
    debugging detail level is zero (0).</p>
  </attribute>

  <attribute name="digest" required="false">
    <p>The digest algorithm used to store passwords in non-plaintext formats.
    Valid values are those accepted for the algorithm name by the
    <code>java.security.MessageDigest</code> class.  See
    <a href="#Digested Passwords">Digested Passwords</a> for more
    information.  If not specified, passwords are stored in clear text.</p>
  </attribute>

  <attribute name="pathname" required="false">
    <p>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
    <code>conf/tomcat-users.xml</code> is used.</p>
  </attribute>

</attributes>

<h3>User File Format</h3>

<p>The users file (by default, <code>conf/tomcat-users.xml</code> must be an
XML document, with a root element <code>&lt;tomcat-users&gt;</code>.  Nested
inside the root element will be a <code>&lt;user&gt;</code> element for each
valid user, consisting of the following attributes:</p>
<ul>
<li><strong>name</strong> - Username this user must log on with.</li>
<li><strong>password</strong> - Password this user must log on with (in
    clear text if the <code>digest</code> attribute was not set on the
    <code>&lt;Realm&gt;</code> element, or digested appropriately as
    described <a href="#Digested Passwords">here</a> otherwise).</li>
<li><strong>roles</strong> - Comma-delimited list of the role names
    associated with this user.</li>
</ul>

<h3>Example</h3>

<p>The default installation of Tomcat 5 is configured with a MemoryRealm
nested inside the <code>&lt;Engine&gt;</code> element, so that it applies
to all virtual hosts and web applications.  The default contents of the
<code>conf/tomcat-users.xml</code> file is:</p>
<source>
&lt;tomcat-users&gt;
  &lt;user name="tomcat" password="tomcat" roles="tomcat" /&gt;
  &lt;user name="role1"  password="tomcat" roles="role1"  /&gt;
  &lt;user name="both"   password="tomcat" roles="tomcat,role1" /&gt;
&lt;/tomcat-users&gt;
</source>

<h3>Additional Notes</h3>

<p>MemoryRealm operates according to the following rules:</p>
<ul>
<li>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 <strong>not</strong> be recognized until Tomcat is
    restarted.</li>
<li>When a user attempts to access a protected resource for the first time,
    Tomcat 5 will call the <code>authenticate()</code> method of this
    <code>Realm</code>.</li>
<li>Once a user has been authenticated, the user (and his or her associated
    roles) are cached within Tomcat for the duration of the user's login.
    (For FORM-based authentication, that means until the session times out or
    is invalidated; for BASIC authentication, that means until the user
    closes their browser).</li>
<li>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.</li>
<li>Debugging and exception messages logged by this <code>Realm</code> will
    be recorded by the <code>Logger</code> that is associated with our
    surrounding <code>Context</code>, <code>Host</code>, or
    <code>Engine</code>.  By default, the corresponding Logger will create a
    log file in the <code>$CATALINA_HOME/logs</code> directory.</li>
</ul>


</subsection>


</section>
</primary>

<section name="標準レルム実装">


<subsection name="JDBCレルム">

<h3>はじめに</h3>

<p><strong>JDBCレルム</strong> は、Tomcat 5 の
<code>Realm</code> インタフェースの実装のうちの1つです。
この実装は、JDBCドライバがアクセスするリレーショナルデータベースからユーザを見つけ出します。
データベース構造が次のような要件を満足しさえすれば、
既存の表の列名との対応づけにより、
十分柔軟な設定をすることができます:</p>
<ul>
<li>以下の<em>ユーザ</em>表のように参照可能な表が必要です。
この表には、この<code>Realm</code>が認識すべき有効な各ユーザに対して1つの行を持ちます。</li>
<li><em>ユーザ</em>表は少なくとも2つの列を持たなければなりません。
(アプリケーションに必要な他の列があってもかまいません):
    <ul>
    <li>ユーザログイン時にTomcatがユーザ名として認識する列</li>
    <li>ユーザログイン時にTomcatがパスワードとして認識する列。
        この値は、平文あるいはダイジェスト暗号化されたものを指定してください。 - 後述の情報を参考にしてください</li>
    </ul></li>
<li><em>ユーザロール</em>表として参照可能な表が必要です。
    この表は、特定のユーザに割り当てられる有効な各ロールに対して1つの行を持ちます。
    あるユーザが有効なロールを持たなくても、1つだけ持っても、1つ以上持ってもかまいません。</li>
<li><em>ユーザロール</em>表は少なくとも2つの列を持たなければなりません。
(アプリケーションに必要な他の列があってもかまいません):
    <ul>
    <li>Tomcatがユーザ名と認識する列 (
        <em>ユーザ</em> 表の対応する列と同じ値を持つ)。</li>
    <li>ユーザに付与された有効なロール名を持つ列</li>
    </ul></li>
</ul>

<h3>クイックスタート</h3>

<p>JDBCレルムを使用するようにTomcatを設定するためには次の手順に従ってください:</p>
<ol>
<li>データベースに表をまだ作成していない場合は、前述した条件を満たす列を持つ表を作成します。</li>
<li>Tomcatが利用するデータベースのユーザ名とパスワードを設定します。
    少なくとも、前述した表への検索アクセスが可能でなければなりません。
   (これらの表の更新をTomcatがすることはありません。)</li>
<li>JDBCドライバのコピーを<code>$CATALINA_HOME/server/lib</code>ディレクトリ内へ置きます
    (もしWebアプリケーションへ見せる必要がない場合)。
    あるいは<code>$CATALINA_HOME/common/lib</code>へ置きます
    (Tomcat 5 <em>と</em>自作アプリケーションの両方によって使用したい場合)。
    JARファイル<strong>だけ</strong>が有効となることに注意してください。</li>
<li><code>$CATALINA_HOME/conf/server.xml</code>ファイルに<code>&lt;Realm&gt;</code>
    要素を追加してください。内容は、以下の記述に従ってください。</li>
<li>Tomcat 5 が既に動作している場合はTomcat 5 の再起動をしてください。</li>
</ol>

<h3>Realm 要素の属性</h3>

<p>JDBCレルムを設定するためには、<a href="#レルムの設定">前述</a>の通りに
<code>$CATALINA_HOME/conf/server.xml</code>ファイルへ
<code>&lt;Realm&gt;</code>要素を記述します。この実装では次の属性がサポートされています:</p>

<attributes>

  <attribute name="className" required="true">
    <p>このレルムを実装するJavaの完全修飾クラス名。
    ここには、"<code>org.apache.catalina.realm.JDBCRealm</code>"
    を指定<strong>しなければなりません</strong>。
    </p>
  </attribute>

  <attribute name="connectionName" required="true">
    <p>JDBC接続をするために使用するデータベースユーザ名</p>
  </attribute>

  <attribute name="connectionPassword" required="true">
    <p>JDBC接続をするために使用するデータベースパスワード</p>
  </attribute>

  <attribute name="connectionURL" required="true">
    <p>JDBC接続をするために使用するデータベースのURL</p>
  </attribute>

  <attribute name="debug" required="false">
    <p>関連する<a href="config/logger.html">ロガー</a>によってこのレルムで記録される詳細デバッグレベル。
      数値が高いほどより詳細な出力を生成します。
      指定がない場合の詳細デバッグレベルの初期値は零(0)です。</p>
  </attribute>

  <attribute name="digest" required="false">
    <p>平文でないパスワードを保存するために使用するダイジェストアルゴリズム。
    <code>java.security.MessageDigest</code>クラスが受け付けるアルゴリズム名が有効です。
    詳細については、
    <a href="#ダイジェストパスワード">ダイジェストパスワード</a> を参照してください。
    指定されない場合、パスワードは平文で保存されているとみなします。</p>
  </attribute>

  <attribute name="driverName" required="true">
    <p>使用されるJDBCドライバの完全修飾クラス名。
    JDBCドライバによって特有の値なので、そのマニュアルをよく調べてください。</p>
  </attribute>

  <attribute name="roleNameCol" required="true">
    <p><em>ユーザロール</em>表の列名。
    この列には、ユーザに付与されているロール名が入っている必要があります。</p>
  </attribute>

  <attribute name="userCredCol" required="true">
    <p><em>ユーザ</em>表の列名。この列には、
    対応するユーザのパスワードが入っている必要があります(平文、
    もしくは<code>digest</code> 属性が設定されている場合はダイジェストされた値)。</p>
  </attribute>

  <attribute name="userNameCol" required="true">
    <p><em>ユーザ</em>表と<em>ユーザロール</em>表の列名。
    このユーザのユーザ名が入っている必要があります。</p>
  </attribute>

  <attribute name="userRoleTable" required="true">
    <p>特定の<em>ユーザ名</em>と対応する<em>ロール</em>を各行に保持する表の名前。
    この表は少なくとも、
    <code>userNameCol</code> と<code>roleNameCol</code> 属性で指定された列を持っている必要があります。</p>
  </attribute>

  <attribute name="userTable" required="true">
    <p>Tomcatによって認証される<em>ユーザ名</em>を各行に含む表名。
    この表は少なくとも、<code>userNameCol</code> と<code>userCredCol</code>属性で指定された列を持っている必要があります。</p>
  </attribute>

</attributes>

<h3>例</h3>

<p>必要な表を作成するSQL スクリプトの例はここで示すようなものになります。
(使用するデータベースによって文法が若干異なります):</p>
<source>
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)
);
</source>

<p> デフォルトの<code>$CATALINA_HOME/conf/server.xml</code> ファイルに、
(コメントアウトされていますが)<code>Realm</code>要素の例が記述されています。
ここでは、"authority"というMySQLのデータベースを使った一例を示します。
前述の通りに設定した表を用意し、ユーザ名"dbuser" とパスワード"dbpass"でアクセスしています:</p>
<source>
&lt;Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
      driverName="org.gjt.mm.mysql.Driver"
   connectionURL="jdbc:mysql://localhost/authority?user=dbuser&amp;amp;password=dbpass"
       userTable="users" userNameCol="user_name" userCredCol="user_pass"
   userRoleTable="user_roles" roleNameCol="role_name"/&gt;
</source>

<h3>さらなる注意点</h3>

<p>JDBCレルムは次の規則に従って処理をします:</p>
<ul>
<li>保護されたリソースへのアクセスをユーザが初めて試みたときに、
    Tomcat 5 はこの<code>Realm</code>の<code>authenticate()</code> メソッドを呼び出します。
    従って、データベースディレクトリに対する変更(新しいユーザを追加したりパスワードやロールを変更するなど)は、
    どんなものでも即座に反映されます。</li>
<li>ユーザログインの持続のためユーザ(個人だけでなく付与されているロールも含めて)情報は、
    一度認証されるとTomcatにキャッシュされます。
   (FORM-based 認証においては、セッションの期限切れや無効化されるまでを意味します。
    BASIC 認証においては、ユーザがブラウザを閉じるまでを意味します。)
    すでに認証されたユーザに関するデータベースの情報に対するいかなる変更も、
    ユーザが再度ログインするときまで反映<strong>されません</strong>。</li>
<li><em>ユーザ</em>表と<em>ユーザロール</em>表に関する情報の管理については、
    使用しているアプリケーションの責務になります。
    ユーザとロールの保守をするための組み込み機能をTomcat は一切提供しません。</li>
<li>この<code>Realm</code> によって記録されるデバッグ用途と例外のメッセージは、
    <code>Context</code>, <code>Host</code>, や<code>Engine</code>で囲まれて関連づけられた
    <code>Logger</code>によって行われます。
    デフォルトでは、連携しているロガーは<code>$CATALINA_HOME/logs</code>
    ディレクトリにログファイルを作成します。</li>
</ul>

</subsection>


<subsection name="データソースレルム">

<h3>はじめに</h3>

<p><strong>データソースレルム</strong> はTomcat 5 の
<code>Realm</code> インタフェースの実装のうちの１つです。
この実装は、JNDI名を持つJDBCデータソースへのアクセスによって
リレーショナルデータベースからユーザを見つけ出します。
データベース構造が次のような要件を満足しさえすれば、
既存表の列名との対応づけにより十分柔軟な設定をすることができます:</p>
<ul>
<li><em>ユーザ</em>表として参照可能な表が必要です。
この表には、この<code>Realm</code>が認識可能で有効な各ユーザに対して1つの行を持ちます。</li>
<li><em>ユーザ</em> 表は少なくとも2つの列を持たなければなりません。
(アプリケーションに必要な他の列があってもかまいません):
    <ul>
    <li>ユーザログイン時にTomcatがユーザ名として認識する列</li>
    <li>ユーザログイン時にTomcatがパスワードとして認識する列。
        この値は、平文かダイジェスト暗号化されたものを指定してください。 - 後述の情報を参照にしてください</li>
    </ul>
</li>
<li><em>ユーザロール</em> 表として参照可能な表が必要です。
    この表は、特定のユーザに割り当てられる有効な各ロールに対して1つの行を持ちます。
    あるユーザが有効なロールを持たなくても、1つだけ持っても、1つ以上持ってもかまいません。</li>
<li><em>ユーザロール</em> 表は少なくとも2つの列を持たなければなりません。
(アプリケーションに必要な他の列があってもかまいません):
    <ul>
    <li>Tomcatがユーザ名と認識する列 (
        <em>ユーザ</em> 表の対応する列と同じ値を持つ)。</li>
    <li>ユーザに付与された有効なロール名を持つ列</li>
    </ul></li>
</ul>

<h3>クイックスタート</h3>
                  
<p>データソースレルムを使用するようにTomcatを設定するためには、次のステップに従ってください:</p>
<ol>              
<li>まだデータベースに表を作成していない場合は、前述した条件を満たす列を持つ表を作成します。</li>
<li>Tomcatが利用するデータベースのユーザ名とパスワードを設定します。
    少なくとも、前述した表への検索アクセスが可能でなければなりません。
   (これらの表の更新をTomcatがすることはありません。)</li>
<li>JNDI名を持つJDBCデータソースとして対象のデータベースを設定します。
    設定に関する詳細については、
    <a href="jndi-datasource-examples-howto.html">JDBCデータソース</a>を参照してください。</li>
<li><code>$CATALINA_HOME/conf/server.xml</code>ファイルに
    <code>&lt;Realm&gt;</code>要素を追加してください。
    内容は、以下に記述するようにしてください。</li>
<li>Tomcat 5 が既に動作している場合は再起動をしてください。</li>
</ol>

<h3>Realm 要素の属性</h3>

<p>データソースレルムを設定するためには、<a href="#レルムの設定">前述</a>の通りに
<code>$CATALINA_HOME/conf/server.xml</code>ファイルへ
<code>&lt;Realm&gt;</code>要素を記述します。この実装では次の属性がサポートされています:</p>

<attributes>

  <attribute name="className" required="true">
    <p>このレルムを実装するJavaの完全修飾クラス名。
    ここには、"<code>org.apache.catalina.realm.DataSourceRealm</code>"
    を指定<strong>しなければなりません</strong>。
    </p>
  </attribute>

  <attribute name="dataSourceName" required="true">
    <p>使用するデータベースに対するJNDIのJDBCデータソース名。</p>
  </attribute>

  <attribute name="debug" required="false">
    <p>関連する<a href="config/logger.html">ロガー</a>によってこのレルムで記録される詳細デバッグレベル。
      数値が高いほどより詳細な出力を生成します。
    指定がない場合の詳細デバッグレベルの初期値は零(0)です。</p>
  </attribute>
    
  <attribute name="digest" required="false">
    <p>平文でないパスワードを保存するために使用するダイジェストアルゴリズム。
    <code>java.security.MessageDigest</code>クラスが受け付けるアルゴリズム名が有効です。
    詳細については、
    <a href="#ダイジェストパスワード">ダイジェストパスワード</a> を参照してください。
    指定されない場合、パスワードは平文で保存されているとみなします。</p>
  </attribute>
    
  <attribute name="roleNameCol" required="true">
    <p><em>ユーザロール</em>表の列名。
    この列には、ユーザに付与されているロール名が入っている必要があります。</p>
  </attribute>
    
  <attribute name="userCredCol" required="true">
    <p><em>ユーザ</em>表の列名。この列には、
    対応するユーザのパスワードが入っている必要があります(平文、
    もしくは<code>digest</code> 属性が設定されている場合はダイジェストされた値)。</p>
  </attribute>
    
  <attribute name="userNameCol" required="true">
    <p><em>ユーザ</em>表と<em>ユーザロール</em>表の列名。
    このユーザのユーザ名が入っている必要があります。</p>
  </attribute>

  <attribute name="userRoleTable" required="true">
    <p>特定の<em>ユーザ名</em>と対応する<em>ロール</em>を各行に保持する表名。
    この表は少なくとも、
    <code>userNameCol</code> と<code>roleNameCol</code> 属性で指定された列を持っている必要があります。</p>
  </attribute>

  <attribute name="userTable" required="true">
    <p>Tomcatによって認証される<em>ユーザ名</em>を各行に含む表名。
    この表は少なくとも<code>userNameCol</code> と<code>userCredCol</code>属性で指定された列を持っている必要があります。</p>
  </attribute>

</attributes>

<h3>例</h3>

<p>必要な表を作成するSQL スクリプトの例はここで示すようなものになります。
(使用するデータベースによって文法が若干異なります):</p>
<source>
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)
);
</source>

<p>ここでは、"authority"というMySQLのデータベースを使った一例を示します。
前述の通りに設定した表を用意して、
JNDI JDBC データソース名が"java:/comp/env/jdbc/authority"でアクセスしています:</p>
<source>
&lt;Realm className="org.apache.catalina.realm.DataSourceRealm" debug="99"
   dataSourceName="java:/comp/env/jdbc/authority"
   userTable="users" userNameCol="user_name" userCredCol="user_pass"
   userRoleTable="user_roles" roleNameCol="role_name"/&gt;
</source>

<h3>さらなる注意点</h3>

<p>データソースレルムは次の規則に従って処理をします:</p>
<ul>
<li>保護されたリソースへのアクセスをユーザが初めて試みたときに、
    Tomcat 5 はこの<code>Realm</code>の<code>authenticate()</code> メソッドを呼び出します。
    従って、データベースディレクトリに対する変更(新しいユーザを追加したりパスワードやロールを変更するなど)は、
    どんなものでも即座に反映されます。</li>
<li>ユーザログインの持続のためユーザ(個人だけでなく付与されているロールも含めて)情報は、
    一度認証されるとTomcatにキャッシュされます。
   (FORM-based 認証においては、セッションの期限切れや無効化されるまでを意味します。
    BASIC 認証においては、ユーザがブラウザを閉じるまでを意味します。)
    すでに認証されたユーザに関するデータベースの情報に対するいかなる変更も、
    ユーザが再度ログインするときまで反映<strong>されません</strong>。</li>
<li><em>ユーザ</em>表と<em>ユーザロール</em>表に関する情報の管理については、
    使用しているアプリケーションの責務になります。
    ユーザとロールの保守をするための組み込み機能をTomcat は一切提供しません。</li>
<li>この<code>Realm</code> によって記録されるデバッグ用途と例外のメッセージは、
    <code>Context</code>, <code>Host</code>, や<code>Engine</code>で囲まれて関連づけられた
    <code>Logger</code>によって行われます。
    デフォルトでは、連携しているロガーは<code>$CATALINA_HOME/logs</code>
    ディレクトリにログファイルを作成します。</li>
</ul>

</subsection>


<subsection name="JNDIレルム">

<h3>はじめに</h3>

<p><strong>JNDIレルム</strong> はTomcat 5 の
<code>Realm</code> インタフェースの実装のうちの１つです。
この実装は、JNDIプロバイダ(典型的には、
JNDI API クラスで有効な標準LDAPプロバイダ)
によるアクセスによって、
LDAPディレクトリサーバからユーザを見つけ出します。
認証でディレクトリを利用するために、
このレルムは様々なアプローチをサポートします:</p>

<h4>ディレクトリへの接続</h4>

<p>レルムのディレクトリ接続は<strong>connectionURL</strong> 設定属性によって定義されます。
JNDIプロバイダによって定義されるフォーマットのURLをここに指定します。
これは、ディレクトリサーバへ接続するためのドメイン名指定に通常使われるLDAP URL です。
ポート番号と、コンテキスト名となるルートの識別名(DN)もオプションで指定できます。</p>

<p>1つ以上のプロバイダがある場合は<strong>alternateURL</strong>を設定します。
<strong>connectionURL</strong>で指定されたプロバイダへのソケット接続ができない場合は、
<strong>alternateURL</strong>を使っての接続を試みます。</p>

<p>ディレクトリを検索してユーザとロールを取り出すための接続ができたら、
<strong>connectionName</strong>と<strong>connectionPassword</strong>
属性で指定されたユーザ名とパスワードを使ってディレクトリの認証をレルムは受けます。
これらの属性が指定されていない場合は匿名(anonymous)で接続が行われます。
ほとんどの場合、匿名接続で十分です。
</p>


<h4>ユーザのディレクトリエントリの選択</h4>

<p>認証され得るそれぞれのユーザは、
個々のエントリによってディレクトリ内で表されなければなりません。
このエントリは、<strong>connectionURL</strong>
属性によって定義された初期<code>DirContext</code>の要素と一致します。
このユーザエントリは、
認証のために使われるユーザ名を含む属性を持っていなければなりません。</p>

<p>ユーザのエントリに対応する識別名(DN)は認証のために使われるユーザ名を含んでいますが、
すべてのユーザにとってユーザ名以外は同じである場合がほとんどです。
こういった場合は、
ユーザ名を表す部分を"{0}"とする特別な識別名(DN)を<strong>userPattern</strong>属性に指定できます。</p>

<p>その他の場合レルムはディレクトリを検索し、
ユーザ名を持った唯一のエントリを見つけなければなりません。

次の属性はこの検索を制御します:
     <ul>
     <li><strong>userBase</strong> - 
         ユーザを含む部分木の開始点となるエントリ。
         指定されなかった場合、
         検索の出発点は最上位のコンテキストとなります。</li>

     <li><strong>userSubtree</strong> - 検索範囲。
         <strong>userBase</strong>エントリを根とする部分木に対して検索したい場合に
         <code>true</code>を指定します。初期値は<code>false</code>で、
         この場合は最上位のみを対象とする単一階層の検索を要求したことになります。</li>

     <li><strong>userSearch</strong> - 
         LDAP検索フィルターのパターンを指定します。
         このフィルターはユーザ名の後方一致で使用されます。</li>

    </ul>
</p>


<h4>ユーザの認証</h4>

<ul>
<li>
<p><b>バインドモード</b></p>

<p>初期設定では、ユーザに対応するエントリーの識別名(DN)のディレクトリと、
ユーザが入力したパスワードとをバインドすることによってレルムはユーザを認証します。
この単純なバインドが成功した場合ユーザは認証されたとみなされます。</p>

<p>セキュリティ確保のため、平文よりも、
ダイジェスト化されたユーザパスワードをディレクトリへ保存するようにして下さい
(詳細は<a href="#ダイジェストパスワード">ダイジェストパスワード</a>を参照のこと)。
この場合、ユーザが入力した平文パスワードの正しいダイジェストを、
単純なバインド操作の一部としてディレクトリは自動的に計算します。
保存された値に対して検査をする前にこの計算は行われます。
このためバインドモードでは、レルムはダイジェスト処理に含まれません。
<strong>ダイジェスト</strong> 属性は使われず、設定しても無視されます。</p>
</li>

<li>
<p><b>比較モード</b></p>
<p>他の手段として、保存されたパスワードをディレクトリからレルムが検索し、
それとユーザが入力した値とを比較する方法があります。このモードは、
<strong>userPassword</strong>属性を指定することによって制御できます。
<strong>userPassword</strong>属性には、
パスワードを入れておくユーザエントリのディレクトリ属性の名前を指定します。</p>

<p>比較モードにはいくつか不便な点があります。
1つめは、ユーザのパスワードをディレクトリにおいてレルムが読み取るれように、
<strong>connectionName</strong>属性と<strong>connectionPassword</strong>
属性を設定する必要があることです。
セキュリティ確保のため、通常はこの指定をしません。代わりに多くのディレクトリ実装は、
ディレクトリ管理者であってもこれらのパスワードを読み込めないようにしています。
さらにレルムは、使用される様々なアルゴリズムとパスワードをハッシュする方法をディレクトリ内部に用意し、
パスワードダイジェスト化を自前で行わなければなりません。
しかしながら、保存されたパスワードへのアクセスが必要なときもレルムにはあるでしょう。
例えば、HTTPダイジェストアクセス認証(RFC 2069)のサポートのためといった場合です。
(ダイジェスト化されたパスワードをユーザ情報向けのリポジトリに保存しておく上述の方式とHTTPダイジェスト認証とは異なることに注意)
</p>
</li>
</ul>

<h4>ユーザへのロールの割り当て</h4>

<p>ディレクトリ内でロールを表す2つの方法をディレクトリレルムはサポートします:</p>

<ul>
<li>
<p><b>明示的なディレクトリエントリとしてのロール</b></p>

<p>明示的なディレクトリエントリによってロールは表されます。
ロールエントリは通常、2つの属性を持つLDAP グループエントリです。
この2つの属性とは、1つはロールの名前を含む属性、
もう1つは、ユーザの識別名(DN)かユーザ名をそのロールで持っている属性のことです。
次に示す属性は、認証されたユーザと関連があるロール名を見つけるためのディレクトリ検索の設定で使います:</p>

<ul>
<li><strong>roleBase</strong> - ロール検索のための出発点となるエントリ。
    指定されなかった場合、検索の出発点は最上位のコンテキストとなります。</li>

<li><strong>roleSubtree</strong> - 検索範囲。
     <strong>roleBase </strong>エントリを根とする部分木に対して検索したい場合に
     <code>true</code>を指定します。初期値は<code>false</code>で、
     この場合は最上位のみを対象とする単一階層の検索を要求したことになります。</li>

<li><strong>roleSearch</strong> -
    ロールエントリの選択のためのLDAP検索フィルターのパターンを指定します。
    オプションとして、識別名(DN)で置き換える"{0}"と、
    認証されたユーザのユーザ名で置き換える"{1}"のパターンを含みます。</li>

<li><strong>roleName</strong> - ロール名を含むロールエントリの属性。</li>

</ul>

</li>
</ul>

<ul>
<li>
<p><b>ユーザエントリの属性としてのロール</b></p>

<p>ユーザのディレクトリエントリの属性値としてロール名を持っているかもしれません。
この属性の名前を指定するためには<strong>userRoleName</strong> を使ってください。</p>

</li>
</ul>
<p>ロールを指定する方式は両方とも組み合わせて使われます。</p>


<h3>クイックスタート</h3>

<p>JNDIレルムを使用するようにTomcatを設定するためには次のステップに従ってください:</p>
<ol>
<li>上述した要件に合致する内容でディレクトリサーバが設定されているかを確認してください。</li>
<li>必要なら、Tomcatが使うユーザ名とパスワードを設定します。
    前述したように読み込むためだけにその情報へアクセスします。
    (Tomcat はこの情報を決して変更しようとしません。)</li>
<li>使用するJNDIドライバ(典型的にはJNDIを有効にする<code>ldap.jar</code>)のコピーを、
    (もしWebアプリケーションへ見せる必要がない場合は)
    <code>$CATALINA_HOME/server/lib</code> ディレクトリ内へ置きます。
    または、(Tomcat 5 <em>と</em>自作アプリケーションの両方によって使用したい場合は)
    <code>$CATALINA_HOME/common/lib</code> ディレクトリ内へ置きます。</li>
<li><code>$CATALINA_HOME/conf/server.xml</code>ファイルへ、
    <code>&lt;Realm&gt;</code>要素を追加してください。
    内容は、以下に記述されたように設定してください。</li>
<li>Tomcat 5 が既に動作している場合は再起動をしてください。</li>
</ol>

<h3>Realm 要素の属性</h3>

<p>JNDIレルムを設定するためには、<a href="#レルムの設定">前述</a>の通りに
<code>$CATALINA_HOME/conf/server.xml</code>ファイルへ
<code>&lt;Realm&gt;</code>要素を記述します。
この実装では次の属性がサポートされています:</p>

<attributes>
      <attribute name="className" required="true">
        <p>このレルムを実装するJavaの完全修飾クラス名。
        ここには、"<code>org.apache.catalina.realm.JNDIRealm</code>"
        を指定<strong>しなければなりません</strong>。
        </p>
      </attribute>

      <attribute name="connectionName" required="false">
        <p>LDAP検索操作のためのディレクトリ接続を確立するときに使うディレクトリユーザ名。
        指定されていない場合は匿名接続になります。
        <code>userPassword</code>属性を指定しない限りは匿名接続で十分です。</p>
      </attribute>

      <attribute name="connectionPassword" required="false">
        <p>LDAP検索操作のためのディレクトリ接続を確立するときに使うディレクトリパスワード。
        指定されていない場合は、匿名接続になります。
        <code>userPassword</code>属性を指定しない限りは匿名接続で十分です。</p>
      </attribute>

      <attribute name="connectionURL" required="true">
        <p>ディレクトリへの接続を確立するときにJNDIドライバに渡される接続URL</p>
      </attribute>

      <attribute name="contextFactory" required="false">
        <p>この接続で使用されるJNDIコンテキストファクトリのJavaの完全修飾クラス名。
        初期設定では、標準のJNDI LDAP プロバイダ
        (<code>com.sun.jndi.ldap.LdapCtxFactory</code>)が使われます。</p>
      </attribute>

      <attribute name="debug" required="false">
        <p>関連する<a href="config/logger.html">ロガー</a>によってこのレルムで記録される詳細デバッグレベル。
           数値が高いほどより詳細な出力を生成します。
           指定がない場合の詳細デバッグレベルの初期値は零(0)です。</p>
     </attribute>

      <attribute name="digest" required="false">
        <p>ユーザによって示された平文のパスワードへ適用されるダイジェストアルゴリズム。
        ディレクトリから検索される値との比較より前に適用されます。
        <code>java.security.MessageDigest</code>クラスが受け付けるアルゴリズム名が有効です。
        詳細については、<a href="#ダイジェストパスワード">ダイジェストパスワード</a> を参照してください。
        指定されない場合は平文パスワードを検索するものとみなします。
        <code>userPassword</code> が指定されない限り必要ありません。</p>
      </attribute>

      <attribute name="roleBase" required="false">
        <p>ロール検索実行の出発点となるディレクトリエントリです。
        指定されない場合は、ディレクトリコンテキストの最上位の要素が使われます。</p>
      </attribute>

      <attribute name="roleName" required="false">
        <p>ロール検索によって見つけらるディレクトリエントリ内のロール名を持つ属性名です。
        加えて、<code>userRoleName</code> プロパティも使えます。このプロパティは、
        追加のロール名を持ったユーザエントリ内の属性名を指定します。
        <code>roleName</code>が指定されない場合ロール検索は行われず、
        ユーザエントリからのみロールを取得することになります。</p>
      </attribute>

      <attribute name="roleSearch" required="false">
        <p>ロール検索の実行で使われるLDAPフィルタの式です。
        <code>java.text.MessageFormat</code>クラスによってサポートされる構文に従います。
        ユーザの識別子名(DN)を置き換えるには<code>{0}</code>を、
        ユーザ名を置き換えるには<code>{1}</code>を使います。
        指定されなかった場合ロール検索は行われず、
        <code>userRoleName</code>属性によって指定されたユーザエントリにある属性からのみロールを取得します。</p>
      </attribute>

      <attribute name="roleSubtree" required="false">
        <p>ユーザに割り当てられたロールエントリを<code>roleBase</code>
        属性によって指定された要素の部分木から検索したい場合は
        <code>true</code>を指定します。
        初期設定値である<code>false</code>は最上位からしか検索しないことになります。</p>
      </attribute>

      <attribute name="userBase" required="false">
        <p><code>userSearch</code>式を使って実行されるユーザ検索のための出発点です。
        指定されない場合は、ディレクトリコンテキストの最上位の要素が使われます。
        <code>userPattern</code>式を使っている場合は指定しないでください。</p>
      </attribute>

      <attribute name="userPassword" required="false">
        <p>ユーザのパスワードを保存するユーザエントリの属性名。
        この値が指定されない場合は、<code>connectionName</code>と<code>connectionPassword</code>
        属性によって指定された値を使ってJNDIレルムはディレクトリをバインドします。
        そして、認証中のユーザが指定したパスワード値と比較するため、
        対応する属性を検索します。
        <code>digest</code>属性が設定されている場合は、
        指定されたダイジェストアルゴリズがユーザの入力したパスワードに適用されます。
        これは、入力されたパスワードとディレクトリから検索された値との比較が行われる前に適用されます。
        この値を指定<strong>しない</strong>場合JNDIレルムは、
        ユーザのエントリの識別名(DN)とユーザによって指定されたパスワードを使ってディレクトリを単純にバインドしようとします。
        認証されたユーザとして解釈されればバインドは成功します。</p>
      </attribute>

      <attribute name="userPattern" required="false">
        <p>ユーザのディレクトリエントリの識別名(DN)のためのパターン。
        <code>java.text.MessageFormat</code>クラスによってサポートされる構文に従います。
        実際のユーザ名が挿入される位置の印としては<code>{0}</code>を使います。
        識別名(DN)がユーザ名を含み、かつ、識別名(DN)のうちユーザ名以外の部分が同じ場合は、
        この属性を<code>userSearch</code>、<code>userSubtree</code>、
        <code>userBase</code>の代わりに使うことができます。</p>
      </attribute>

      <attribute name="userRoleName" required="false">
        <p>ユーザに割り当てられたロール名を0個以上持つユーザディレクトリエントリの属性名。
        ディレクトリ検索によって見つけられた個々のロールエントリから検索される属性名を指定するために、
        <code>roleName</code>属性も一緒に使うことができます。
        <code>userRoleName</code>が指定されない場合は、
        ユーザに割り当てられているロールすべてがロール検索から得られます。</p>
      </attribute>

      <attribute name="userSearch" required="false">
        <p>ユーザのディレクトリエントリに対する検索において使われるLDAPフィルター式。
        実際のユーザ名が挿入される位置には<code>{0}</code>で印をつけます。
        この属性は、ディレクトリからユーザエントリを検索するために、
        <code>userPattern</code>の代わりに(<code>userBase</code>と
        <code>userSubtree</code>属性と一緒に)使います。</p>
      </attribute>

      <attribute name="userSubtree" required="false">
        <p><code>userBase</code>属性によって指定された要素の部分木からユーザエントリを検索したい場合に
        <code>true</code>と指定します。初期設定値である
        <code>false</code> は最上位からしか検索しないことになります。
        <code>userPattern</code>式を使用する場合は指定しないでください。</p>
      </attribute>

</attributes>

<h3>例</h3>

<p>
ディレクトリサーバで特定のスキーマを作成することはこの文書の範囲を越えています。
なぜなら、それはディレクトリサーバの実装に依存するからです。
以下の例では、<a href="http://www.openldap.org">http://www.openldap.org</a>
からダウンロード可能な、OpenLDAPディレクトリサーバ(2.0.11版以降)を使うことを前提とします。
また、(特に)<code>slapd.conf</code> ファイルは次のように設定されているものとします:</p>
<source>
database ldbm
suffix dc="mycompany",dc="com"
rootdn "cn=Manager,dc=mycompany,dc=com"
rootpw secret
</source>

<p><code>connectionURL</code>の指定にあたっては、
Tomcatが動作しているマシンと同じマシンでディレクトリサーバが動作しているものとします。
設定とJNDI LDAP プロバイダの使用に関する詳細については、<a
href="http://java.sun.com/products/jndi/docs.html">http://java.sun.com/products/jndi/docs.html</a>
を参照してください。</p>

<p>次に、このディレクトリサーバは、
(LDIFフォーマット形式で)次に示すような要素を実装しているものとします:</p>

<source>

# Define top-level entry
dn: dc=mycompany,dc=com
objectClass: dcObject
dc:mycompany

# Define an entry to contain people
# searches for users are based on this entry
dn: ou=people,dc=mycompany,dc=com
objectClass: organizationalUnit
ou: people

# Define a user entry for Janet Jones
dn: uid=jjones,ou=people,dc=mycompany,dc=com
objectClass: inetOrgPerson
uid: jjones
sn: jones
cn: janet jones
mail: j.jones@mycompany.com
userPassword: janet

# Define a user entry for Fred Bloggs
dn: uid=fbloggs,ou=people,dc=mycompany,dc=com
objectClass: inetOrgPerson
uid: fbloggs
sn: bloggs
cn: fred bloggs
mail: f.bloggs@mycompany.com
userPassword: fred

# Define an entry to contain LDAP groups
# searches for roles are based on this entry
dn: ou=groups,dc=mycompany,dc=com
objectClass: organizationalUnit
ou: groups

# Define an entry for the "tomcat" role
dn: cn=tomcat,ou=groups,dc=mycompany,dc=com
objectClass: groupOfUniqueNames
cn: tomcat
uniqueMember: uid=jjones,ou=people,dc=mycompany,dc=com
uniqueMember: uid=fbloggs,ou=people,dc=mycompany,dc=com

# Define an entry for the "role1" role
dn: cn=role1,ou=groups,dc=mycompany,dc=com
objectClass: groupOfUniqueNames
cn: role1
uniqueMember: uid=fbloggs,ou=people,dc=mycompany,dc=com
</source>

<p>上記で説明した通りに設定されたOpenLDAP ディレクトリサーバのための<code>Realm</code>
要素の例は次のようになります。
ここでは、ユーザはuid(例 jjones)を使ってアプリケーションへログインし、
ディレクトリ検索とロール情報の検索をするのには匿名接続で十分であるものとします:</p>

<source>
&lt;Realm   className="org.apache.catalina.realm.JNDIRealm" debug="99"
     connectionURL="ldap://localhost:389"
       userPattern="uid={0},ou=people,dc=mycompany,dc=com"
          roleBase="ou=groups,dc=mycompany,dc=com"
          roleName="cn"
        roleSearch="(uniqueMember={0})"
/&gt;
</source>

<p>この設定によりレルムは、<code>userPattern</code>をユーザ名へ置き換えて、
ユーザの識別名(DN)を決定します。
そして、この識別名(DN)とユーザから受け取ったパスワードをディレクトリへ渡して認証を行い、
ユーザのロールを確定するためにディレクトリを検索します。</p>

<p>ユーザは最近、ユーザIDよりもeメールアドレスの入力をログイン時に求められることが多くあります。
この場合レルムは、ユーザのエントリに対してディレクトリを検索しなければなりません。
(異なる組織単位や会社住所といった複数の部分木にユーザエントリがある場合も検索は必要です。)</p>

<p>そしてさらにグループエントリに加えて、
ロールを固定するためのユーザエントリの属性を使いたい場合もあるでしょう。
Janet Jones のエントリーは次のようになっているはずです:</p>

<source>
dn: uid=jjones,ou=people,dc=mycompany,dc=com
objectClass: inetOrgPerson
uid: jjones
sn: jones
cn: janet jones
mail: j.jones@mycompany.com
memberOf: role2
memberOf: role3
userPassword: janet
</source>

<p> 次のレルム設定は新しい要求を満たしています:</p>

<source>
&lt;Realm   className="org.apache.catalina.realm.JNDIRealm" debug="99"
     connectionURL="ldap://localhost:389"
          userBase="ou=people,dc=mycompany,dc=com"
        userSearch="(mail={0})"
      userRoleName="memberOf"
          roleBase="ou=groups,dc=mycompany,dc=com"
          roleName="cn"
        roleSearch="(uniqueMember={0})"
/&gt;
</source>

<p>ここで、Janet Jones が"j.jones@mycompany.com"でログインするとき、
これと同じmail属性の値を持つ唯一のエントリーをディレクトリからレルムが検索します。
そして、<code>uid=jjones,ou=people,dc=mycompany,dc=com</code> とパスワードでディレクトリの確定を試みます。
もし認証が成功したら、彼女には3つのロールが割り当てられます:
彼女のディレクトリエントリの"memberOf"属性の値である"role2" と"role3"、
そして、彼女が所属するグループのエントリにある"cn"属性の値である"tomcat"です。
</p>

<p>最後に、ディレクトリからパスワードを検索し、
レルム内で内部的に比較を行うことによってユーザを認証するためには次のようにレルム設定を使います:</p>

<source>
&lt;Realm   className="org.apache.catalina.realm.JNDIRealm" debug="99"
    connectionName="cn=Manager,dc=mycompany,dc=com"
connectionPassword="secret"
     connectionURL="ldap://localhost:389"
      userPassword="userPassword"
       userPattern="uid={0},ou=people,dc=mycompany,dc=com"
          roleBase="ou=groups,dc=mycompany,dc=com"
          roleName="cn"
        roleSearch="(uniqueMember={0})"
/&gt;
</source>

<p>しかしながらこれまで説明してきた通り、
初期設定されている認証のためのバインドモードを通常使います。</p>

<h3>追加の注意点</h3>

<p>JNDIレルム は次の規則に従って処理をします:</p>
<ul>
<li>保護されたリソースへのアクセスをユーザが初めて試みたときに、
    Tomcat 5 はこの<code>Realm</code>の<code>authenticate()</code> メソッドを呼び出します。
    ディレクトリに対する変更(新しいユーザを追加したりパスワードやロールを変更するなど)は、
    どんなものでもこれで即座に反映されます。</li>
<li>ユーザログインの持続のために、
    一度認証されるとユーザ(個人だけでなく付与されているロールも含めて)情報はTomcatにキャッシュされます。
   (FORM-based 認証においては、セッションの期限切れや無効化されるまでを意味します。
     ;BASIC 認証においては、ユーザがブラウザを閉じるまでを意味します。)
    すでに認証されたユーザに関するデータベースの情報に対するいかなる変更も、
    ユーザが再度ログインするときまで反映<strong>されません</strong>。</li>
<li>ディレクトリサーバー内の情報管理については使用しているアプリケーションの責務になります。
    ユーザとロールの保守をするための組み込み機能をTomcat は一切提供しません。</li>
<li>この<code>Realm</code> によって記録されるデバッグ用途と例外のメッセージは、
    <code>Context</code>, <code>Host</code>, や<code>Engine</code>
    で囲まれて関連づけられた<code>Logger</code> によって行われます。
    デフォルトでは、連携しているロガーは<code>$CATALINA_HOME/logs</code>
    ディレクトリにログファイルを作成します。</li>
</ul>

</subsection>


<subsection name="メモリレルム">

<h3>はじめに</h3>
<p><strong>メモリレルム</strong> は、
Tomcat 5 の<code>Realm</code> インタフェースの実装のうちの1つで、
簡単なデモンストレーションには利用できますが、製品での利用は想定されていません。
メモリレルムは、すべてのユーザに関する情報とユーザに対応するロールをXML文書から起動時に読み込みます
(デフォルトでは、この文書は<code>$CATALINA_HOME/conf/tomcat-users.xml</code>から読み込まれます)。
このファイルを変更しても、Tomcatを再起動するまでは反映されません。</p>

<h3>Realm 要素の属性</h3>

<p>メモリレルムを設定するためには、
<a href="#レルムの設定">前述</a>の通りに
<code>$CATALINA_HOME/conf/server.xml</code>ファイルへ
<code>&lt;Realm&gt;</code>要素を記述します。この実装では次の属性がサポートされています:</p>

<attributes>

  <attribute name="className" required="true">
    <p>このレルムを実装するJavaの完全修飾クラス名。
    ここには、"<code>org.apache.catalina.realm.MemoryRealm</code>"
    を指定<strong>しなければなりません</strong>。</p>
  </attribute>

  <attribute name="debug" required="false">
    <p>関連する<a href="config/logger.html">ロガー</a>によってこのレルムで記録される詳細デバッグレベル。
       数値が高いほどより詳細な出力を生成します。
       指定がない場合の詳細デバッグレベルの初期値は零(0)です。</p>
  </attribute>

  <attribute name="digest" required="false">
    <p>平文でないパスワードを保存するために使用するダイジェストアルゴリズム。
    <code>java.security.MessageDigest</code>クラスが受け付けるアルゴリズム名が有効です。
    詳細については、
    <a href="#ダイジェストパスワード">ダイジェストパスワード</a> を参照してください。
    指定されない場合、パスワードは平文で保存されているとみなされます。</p>
  </attribute>

  <attribute name="pathname" required="false">
    <p>有効なユーザ名とパスワードとロールを含むXML文書の絶対パス名または、
    ($CATALINA_HOMEに対する) 相対パス名。このファイルの形式に関する詳細は以下を参照して下さい。
    指定されなかった場合は、<code>conf/tomcat-users.xml</code> が使われます。</p>
  </attribute>

</attributes>

<h3>ユーザファイル形式</h3>

<p>
ユーザファイル(デフォルトは, <code>conf/tomcat-users.xml</code>)は、
<code>&lt;tomcat-users&gt;</code>をルート要素とするXML文書でなければなりません。
ルート要素には、有効なユーザそれぞれに対応する<code>&lt;user&gt;</code>
要素を入れ子で指定できます。user要素は次の属性から構成されます。
<note>
The users file (by default, <code>conf/tomcat-users.xml</code> must be an は、
The users file (by default, <code>conf/tomcat-users.xml</code>) must be an の間違いだろう。
</note>
</p>
<ul>
<li><strong>name</strong> - ユーザがログオンで使うユーザ名。</li>
<li><strong>password</strong> - ユーザがログオンで使うパスワード
    (<code>&lt;Realm&gt;</code> 要素に<code>digest</code>が設定されていない場合は平文、
     その他の場合は<a href="#ダイジェストパスワード">ここ</a>
     で説明されているようなダイジェストされた値)。</li>
<li><strong>roles</strong> - このユーザに付与されたロール名一覧をカンマで区切って指定。</li>
</ul>

<h3>例</h3>

<p>Tomcat 5 をインストールした直後はメモリレルムを使って設定されています。
このメモリレルムは<code>&lt;Engine&gt;</code> 要素内で指定しているので、
すべての仮想ホストとWebアプリケーションで有効です。
<code>conf/tomcat-users.xml</code> ファイルの初期内容は次の通りです:</p>
<source>
&lt;tomcat-users&gt;
  &lt;user name="tomcat" password="tomcat" roles="tomcat" /&gt;
  &lt;user name="role1"  password="tomcat" roles="role1"  /&gt;
  &lt;user name="both"   password="tomcat" roles="tomcat,role1" /&gt;
&lt;/tomcat-users&gt;
</source>

<h3>追加の注意点</h3>

<p>メモリレルムは次の規則に従って処理をします:</p>
<ul>
<li>起動したときTomcatはまず、定義されているユーザと関連する情報全てをユーザファイルから読み込みます。
    ファイル内のデータを変更しても、Tomcatが再起動されるまで反映され<strong>ません</strong>。</li>
<li>保護されたリソースへのアクセスをユーザが初めて試みたときに、
    この<code>Realm</code>の<code>authenticate()</code> メソッドをTomcat 5 は呼び出します。</li>
<li>ユーザログインの持続のためユーザ(個人だけでなく付与されているロールも含めて)情報は、
    一度認証されるとTomcatにキャッシュされます。
   (FORM-based 認証においては、セッションの期限切れや無効化されるまでを意味します。
    ;BASIC 認証においては、ユーザがブラウザを閉じるまでを意味します。)</li>
<li>ユーザファイル内の情報管理については使用しているアプリケーションの責務になります。
    ユーザとロールの保守をするための組み込み機能をTomcat は一切提供しません。</li>
<li>この<code>Realm</code> によって記録されるデバッグ用途と例外のメッセージは、
    <code>Context</code>, <code>Host</code>, や<code>Engine</code>
    で囲まれて関連づけられた<code>Logger</code> によって行われます。
    デフォルトでは、連携しているロガーは<code>$CATALINA_HOME/logs</code>
    ディレクトリにログファイルを作成します。</li>
</ul>


</subsection>


</section>

<primary>
<section name="Common Features">


<subsection name="Digested Passwords">

<p>For each of the standard <code>Realm</code> 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 <em>digesting</em>
user passwords.  This allows the stored version of the passwords to be
encoded (in a form that is not easily reversible), but that the
<code>Realm</code> implementation can still utilize for
authentication.</p>

<p>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 <code>digest</code>
attribute on your <code>&lt;Realm&gt;</code> element.  The value for
this attribute must be one of the digest algorithms supported by the
<code>java.security.MessageDigest</code> class (SHA, MD2, or MD5).
When you select this option, the contents of the password that is
stored in the <code>Realm</code> must be the cleartext version of the
password, as digested by the specified algorithm.</p>

<p>When the <code>authenticate()</code> 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
<code>Realm</code>.  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.</p>

<p>To calculate the digested value of a cleartext password, two convenience
techniques are supported:</p>
<ul>
<li>If you are writing an application that needs to calculate digested
    passowrds dynamically, call the static <code>Digest()</code> method of the
    <code>org.apache.catalina.realm.RealmBase</code> class, passing the
    cleartext password and the digest algorithm name as arguments.  This
    method will return the digested password.</li>
<li>If you want to execute a command line utility to calculate the digested
    password, simply execute
<source>
java org.apache.catalina.realm.RealmBase \
    -a {algorithm} {cleartext-password}
</source>
    and the digested version of this cleartext password will be returned to
    standard output.</li>
</ul>

<p>To use either of the above techniques, the
<code>$CATALINA_HOME/server/lib/catalina.jar</code> file will need to be
on your class path to make the <code>RealmBase</code> class available.</p>

</subsection>


<subsection name="Example Application">

<p>The example application shipped with Tomcat 5 includes an area that is
protected by a security constraint, utilizing form-based login.  To access it,
point your browser at
<a href="http://localhost:8080/examples/jsp/security/protected/">http://localhost:8080/examples/jsp/security/protected/</a>
and log on with one of the usernames and passwords described for the default
<a href="#MemoryRealm">MemoryRealm</a>.</p>

</subsection>


<subsection name="Manager Application">

<p>If you wish to use the <a href="manager-howto.html">Manager Application</a>
to deploy and undeploy applications in a running Tomcat 5 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.</p>

<p>For security reasons, no username in the default Realm (i.e. using
<code>conf/tomcat-users.xml</code> 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.</p>

</subsection>


</section>
</primary>


<section name="共通機能">


<subsection name="ダイジェストパスワード">

<p>標準<code>Realm</code> 実装はいずれも、
(初期設定では)ユーザパスワードを平文で保存します。
多くの環境でこれは望ましくありません。
認証データを偶然見た人が、
ログオンを成功させるための十分な情報を集め、
他のユーザになりすますことができてしまいます。
この問題を回避するための一般解は、ユーザパスワードの<em>ダイジェスティング(digesting)</em>
という概念を実装することです。
これは、(解読困難な形式で)コード化されたパスワードを保存することを可能にします。
また、コード化されてはいますが、<code>Realm</code>実装は認証のためにそれを利用できます。</p>

<p>保存されているパスワードを取り出し、
そのパスワードの値とユーザから提示された値との比較によって標準レルムが認証をするとき、
<code>&lt;Realm&gt;</code>要素の<code>digest</code>属性を指定することでダイジェストパスワードを使うことができます。
この属性値は、
<code>java.security.MessageDigest</code>
クラスでサポートされるダイジェストアルゴリズム
(SHA, MD2, または MD5)のどれかでなければなりません。
このオプションを選択した場合<code>Realm</code>に保存されるパスワードの中身は、
指定したアルゴリズムによってダイジェスト化されたパスワードでなければなりません。</p>

<p>Realmの<code>authenticate()</code>メソッドが呼ばれたとき、
ユーザによって指定された(平文の)パスワードは同じアルゴリズムによってダイジェスト化され、
<code>Realm</code>によって返された値とその結果を比較します。
この値が一致する場合は、
オリジナルパスワードの平文版はユーザによって提供されたものと同じである、
ということを意味します。
その結果、このユーザは認証されたことになります。
</p>

<p>平文のパスワードからダイジェスト値を計算するために、2つの便利なテクニックがサポートされています:</p>
<ul>
<li>ダイジェストパスワードを動的に計算する必要のあるアプリケーションを作成している場合は、
    <code>org.apache.catalina.realm.RealmBase</code>クラスの
    staticメソッドである<code>Digest()</code>に平文のパスワードとダイジェストアルゴリズム名を引数として渡して呼び出します。
    このメソッドはダイジェストパスワードを返します。</li>
<li>コマンドラインからダイジェストパスワードを計算したい場合には次のコマンドを単純に実行します。
<source>
java org.apache.catalina.realm.RealmBase \
    -a {algorithm} {cleartext-password}
</source>
<annotation>
<strong>\</strong>は入力不要です。これは、実際の入力が1行であることを意味しています。
</annotation>
    すると、この平文パスワードに対するダイジェスト値が標準出力に返ってきます。</li>
</ul>

<p>上記のテクニックのどちらを使うにしても、
<code>$CATALINA_HOME/server/lib/catalina.jar</code> ファイルが必要です。
<code>RealmBase</code>クラスを使用できるように、このファイルをクラスパスへ追加してください。</p>

</subsection>


<subsection name="Example アプリケーション">

<p>Example アプリケーションは、
form-based ログインを使ってセキュリティ制約で保護するように設定されたページを含んでいます。
そのページへアクセスするためには、
<a href="http://localhost:8080/examples/jsp/security/protected/">
http://localhost:8080/examples/jsp/security/protected/</a>
をブラウザで指定し、初期設定されている<a href="#メモリレルム">メモリレルム</a>
に記述されたユーザ名とパスワードの1つを使ってログオンします。</p>

</subsection>


<subsection name="マネージャアプリケーション">

<p>インストールしたTomcat 5 の実行時に<a href="manager-howto.html">マネージャアプリケーション</a>
を使ってアプリケーションの配備と配備解除をしたい場合は、
選択したレルム実装において、
少なくとも1つのユーザ名に対して"manager" ロールを付与しなければ*なりません*。
これは、マネージャWebアプリケーション自身がセキュリティ制約を使用するからです。
そのセキュリティ制約とは、
マネージャWebアプリケーション内の*どの*リクエストURIへのアクセスに対しても"manager"ロールを要求する、
というものです。</p>

<p>セキュリティ確保の理由から、初期設定されている
(すなわち、<code>conf/tomcat-users.xml</code>を使う)
レルムには、"manager"ロールが付与されたユーザ名は1つもありません。
したがって、最低1人のユーザに対してTomcat管理者がこのロールを特別に付与するまでは、
このアプリケーションの機能を誰も使うことができません。
<note> (i.e. using <code>conf/tomcat-users.xml</code> is assigned the "manager" role.は、
 (i.e. using <code>conf/tomcat-users.xml</code>) is assigned the "manager" role.の間違いだろう</note>
</p>

</subsection>


</section>


</body>

</document>
