Hi, I have been trying to implement form based authentication using
container managed security.
I had tried originally to use the DataSource Realm but after struggling with
that for so long I gave up because I had tried everything I could think of
as far as putting the Realm declaration in varioius places with no luck, and
I got conflicting answers between the the Apache-Tomcat docs ( which I've
read multiple times) and what I found in places like mark-mail and nabble.

So now I am trying to get it working with the JDBC realm instead. I have a
simple webapp for mountain bike and trail reviews and bicycle reviews. In
order to submit a new review you need to log on or register.  Here is my
server.xml:

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="8005" shutdown="SHUTDOWN">

  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener"
SSLEngine="on" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at
/docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- JMX Support for the Tomcat server. Documentation at
/docs/non-existent.html -->
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"
/>
  <Listener
className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
     -->

    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more
named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking &
non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
    <!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the JSSE configuration, when using APR, the
         connector should be using the OpenSSL style configuration
         described in the APR documentation -->
    <!--
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


    <!-- An Engine represents the entry point (within Catalina) that
processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes
them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Standalone" defaultHost="localhost"
jvmRoute="jvm1">
    -->
    <Engine name="Catalina" defaultHost="localhost">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- The request dumper valve dumps useful debugging information about
           the request and response data received and sent by Tomcat.
           Documentation at: /docs/config/valve.html -->
      <!--
      <Valve className="org.apache.catalina.valves.RequestDumperValve"/>
      -->

      <!-- This Realm uses the UserDatabase configured in the global JNDI
           resources under the key "UserDatabase".  Any edits
           that are performed against this UserDatabase are immediately
           available for use by the Realm.  -->


      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
             resourceName="UserDatabase"/>

      <!-- Define the default virtual host
           Note: XML Schema validation will not work with Xerces 2.2.
       -->
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">

        <!-- SingleSignOn valve, share authentication between web
applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs"
               prefix="localhost_access_log." suffix=".txt" pattern="common"
resolveHosts="false"/>
        -->
        <!--  username="buzz" password="9999"  -->
        <Context path="/blurbV1"
             docBase="blurbV1"
             debug="99"
             reloadable="true">

            <Resource name="jdbc/trailsDB" auth="Container"
                  type="javax.sql.DataSource"

                  driverClassName="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost:3306/trailsDB?user=buzz&amp;password=999999"
                       maxActive="8"/>

            <Realm className="org.apache.catalina.realm.JDBCRealm"
                   debug="99"
                   driverName="com.mysql.jdbc.Driver"

                   connectionURL="jdbc:mysql://localhost:3306/trailsDB"
                   userTable="users"
                   userNameCol="user_name"
                   userCredCol="user_pass"
                   userRoleTable="user_roles"
                   roleNameCol="role_name"
            />

              </Context>

      </Host>
    </Engine>
  </Service>
</Server>

And my web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xmlns="
http://java.sun.com/xml/ns/javaee"; xmlns:web="
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"; xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"; id="WebApp_ID"
version="2.5">
  <display-name>project-1c</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <listener>
    <listener-class>
          com.buzz.TrailsAppServletContextListener
      </listener-class>
  </listener>
  <servlet>
    <description>find trails based on category </description>
    <display-name>TrailFinderServlet</display-name>
    <servlet-name>TrailFinderServlet</servlet-name>
    <servlet-class>com.buzz.finder.TrailFinderServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>TrailFinderServlet</servlet-name>
    <url-pattern>/TrailFinderServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>TrailReviewSubmitServlet</display-name>
    <servlet-name>TrailReviewSubmitServlet</servlet-name>
    <servlet-class>com.buzz.review.TrailReviewSubmitServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>TrailReviewSubmitServlet</servlet-name>
    <url-pattern>/TrailReviewSubmitServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>TrailReviewServlet</display-name>
    <servlet-name>TrailReviewServlet</servlet-name>
    <servlet-class>com.buzz.review.TrailReviewServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>TrailReviewServlet</servlet-name>
    <url-pattern>/TrailReviewServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>bikeFinderServlet</display-name>
    <servlet-name>bikeFinderServlet</servlet-name>
    <servlet-class>com.buzz.finder.bikeFinderServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>bikeFinderServlet</servlet-name>
    <url-pattern>/bikeFinderServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>BikeFinderServlet</display-name>
    <servlet-name>BikeFinderServlet</servlet-name>
    <servlet-class>com.buzz.finder.BikeFinderServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>BikeFinderServlet</servlet-name>
    <url-pattern>/BikeFinderServlet</url-pattern>
  </servlet-mapping>
   <session-config>
      <session-timeout>10</session-timeout>
  </session-config>

  <security-role>
      <role-name>admin</role-name>
      <role-name>member</role-name>
      <role-name>guest</role-name>
  </security-role>



  <security-constraint>

    <web-resource-collection>

        <web-resource-name>UpdateTrails</web-resource-name>

        <url-pattern>/*</url-pattern>

        <http-method>GET</http-method>
        <http-method>POST</http-method>

    </web-resource-collection>

        <auth-constraint>
            <description>These are the roles who have access</description>
            <role-name>admin</role-name>
        </auth-constraint>

</security-constraint>

<login-config>
    <auth-method>FORM</auth-method>
    <realm-name>Tomcat Server Configuration Form-Based
        Authentication Area</realm-name>
    <form-login-config>
        <form-login-page>/Login.html</form-login-page>
        <form-error-page>/auth-error.html</form-error-page>
    </form-login-config>
        </login-config>


<resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/trailsDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

</web-app>

Even though it says DataSource in the above resource-ref tag, all the info I
found told me
to do that even with the JDBCRealm.

So there seems to be 2 problems. Here's what Tomcat gives me when I attempt
to login:

HTTP Status 404 - /blurbV1/auth-error.html
------------------------------

*type* Status report

*message* */blurbV1/auth-error.html*

*description* *The requested resource (/blurbV1/auth-error.html) is not
available.*
------------------------------
Apache Tomcat/6.0.16
But it should allow me to login since I have the users and the database
setup with the correct
user and role tables. Here is the tomcat-users.xml created by Tomcat:

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="member"/>
  <role rolename="admin"/>
  <role rolename="guest"/>
  <user username="bob" password="555555" roles="guest"/>
  <user username="cuco" password="222222" roles="member,guest"/>
  <user username="buzz" password="999999" roles="admin,member,guest"/>
</tomcat-users>

Any ideas why I might be getting this ?
Thanks,

Adam

Reply via email to