views:

377

answers:

1

Hi,

I want to configure tomcat / my web application to use a JDBC realm for container managed security. I specified the realm inside tomcat's server.xml file like this:

<Realm className="org.apache.catalina.realm.JDBCRealm" driverName="net.sourceforge.jtds.jdbc.Driver" connectionURL="jdbc:jtds:sqlserver://hostname:1433/intranet;user=sa;password=sa04jT14;instance=instanceName" userTable="users" userNameCol="username" userCredCol="password" userRoleTable="roles" roleNameCol="role" />

I created the database and the tables. I created a login-page and added the following code to the web.xml:

<login-config>
    <auth-method>FORM</auth-method>
    <form-login-config>
        <form-login-page>/login.xhtml</form-login-page>
        <form-error-page>/login.xhtml</form-error-page>
    </form-login-config>
</login-config>

But how does Tomcat know which realm he has to use for authentication? Do I have to add an element to the element? But what should be the value then?

Thanks in advance.

A: 

It's fine so. It depends on the location of the <Realm> declaration which one Tomcat will find and use for your webapp. It's also explicitly mentioned in Tomcat's Realm Configuration HOWTO:

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):

  • Inside an <Engine> element - This Realm will be shared across ALL web applications on ALL virtual hosts, UNLESS it is overridden by a Realm element nested inside a subordinate <Host> or <Context> element.

  • Inside a <Host> element - This Realm will be shared across ALL web applications for THIS virtual host, UNLESS it is overridden by a Realm element nested inside a subordinate <Context> element.

  • Inside a <Context> element - This Realm will be used ONLY for THIS web application.

The <Engine> and <Host> elements are normally declared in /conf/server.xml. The <Context> element can be declared in any of the locations as per the list at the bottom of the introductory text of this documentation. If you for example intend to define this realm in a webapp-specific <Context> which you'd like to deploy together with your webapplication without hassling with the server config, then the best place would then be Webapp/META-INF/context.xml.

Hope this helps.

BalusC