Using Tomcat 4 Security Realms
Pages: 1, 2
Now let's actually look at how your newly defined realm affects the /onjava web application. Point your browser at the following URL:
http://localhost:8080/onjava/login.jsp
If everything went according to plan you should see a dialog box similar to the one in Figure 1.
|
Go ahead and enter bob for the User Name, password for the Password, and press "OK." Again, if everything goes according to plan, you should see the login page of the /onjava web application. You now have a Web application that is protected by a security realm that uses the Basic Authentication method to authenticate its users.
JDBC Realms
The second Realm implementation provided with Tomcat is a JDBC realm. The class that implements the JDBC realm is org.apache.cataline.realm.JDBCRealm. The JDBCRealm class is much like the MemoryRealm discussed in the previous section, with the exception of where it stores its collection of users. A JDBCRealm stores all of its users in a user-defined, JDBC-compliant database. There are several steps involved when setting up a JDBC realm, but once it is configured it is really simple to manage.
Defining the Users Database
Before you begin configuring Tomcat to use a JDBCRealm, you must first create a database to hold your collection of users. Our user database is going to contain three tables. The first table is the users table. The users table contains the user name and password for each of our users. Table 2 contains the description of the users table.
Table 2. The users Table Definition | |
| Column | Description |
user_name |
The user_name column contains a string representing the username that will be used in the login form. The user_name has a type of varchar(12). |
user_pass |
The user_pass column contains a string representing the user's password. The user_pass has a type of varchar(12). |
The second table in the users database is the roles table. The roles table contains all of the possible roles for the users defined in this database. The roles table contains a single column, role_name, that is a varchar(12) string representing each role name.
The last table in the users database is the user_roles table. The user_roles table is a mapping table between the roles and users defined in this database. Table 3 contains the table definition for the user_roles table.
Table 3. The user_roles Table Definition. | |
| Column | Description |
user_name |
The user_name column contains a string representing the username that will be used in the login form. The user_name has a type of varchar(12). |
role_name |
The role_name column contains a string representing the user's role. The role_name has a type of varchar(12). |
The contents of each of the users database's tables are listed in Tables 4, 5, and 6.
Table 4. The Contents of the users Table | |
user_name |
user_pass |
| robert | password |
| bob | password |
| tomcat | password |
| joe | $joe$ |
Table 5. The Contents of the roles Table | |
user_name |
|
| onjava | |
| manager | |
| tomcat | |
Table 6. The Contents of the user_roles Table | |
user_name |
user_pass |
| bob | onjavauser |
| joe | onjavauser |
| joe | manager |
| tomcat | tomcat |
| robert | onjavauser |
Creating and Configuring a MySQL Users Database
Now that you have defined the users database, you can actually create the physical database. Before you can create the users database, you will need to download and install the MySQL server, which can be found at http://www.mysql.com. You should also download the latest JDBC driver for MySQL, which can also be found at the previously mentioned Web site.
Note: For this example we are using MySQL. You can use any JDBC-compliant database server of your choosing.
After you have MySQL installed, you need to complete the following steps to create and configure a MySQL Users database:
Start the
mysqlclient found in the<mysql_home>/bin/directory.Create the Users database, which will be explicitly named
tomcatusers, by executing the following command:create database tomcatusers;Create the
userstable using the following command:create table users
(
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);Create the
rolestable using the following command:create table roles
(
role_name varchar(15) not null primary key
);Create the
user_rolestable using the following command:create table users
(
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key(user_name, role_name)
);Insert the user data into the
userstable, by executing the following commands:insert into users values("bob", "password");
insert into users values("joe", "$joe$");
insert into users values("robert", "password");
insert into users values("tomcat", "password");Insert the roles data into the
rolestable with the following commands:insert into roles values("onjavauser");
insert into roles values("manager");
insert into roles values("tomcat");Insert the user roles data into the
user_rolestable with the following commands:insert into user_roles values("bob", "onjavauser");
insert into user_roles values("joe", "onjavauser");
insert into user_roles values("joe", "manager");
insert into user_roles values("robert", "onjavauser");
insert into user_roles values("tomcat", "tomcat");
Configuring Tomcat to Use a JDBC Realm
Now that you have a container of users, let's configure Tomcat to use the JDBC container instead of the previously configured MemoryRealm. The steps involved in configuring a JDBCRealm are described in the following list.
Open
<tomcat_home>/conf/server.xmland place comment tags around the previously uncommented<realm>element.<!-- <Realm className="org.apache.catalina.realm.MemoryRealm" /> -->Place the following code snippet directly below the previously referenced
<realm>element:<realm classname="org.apache.catalina.realm.JDBCRealm" debug="99"
drivername="org.gjt.mm.mysql.Driver"
connectionurl="jdbc:mysql://localhost/tomcatusers?user=test;password=test"
usertable="users" usernamecol="user_name" usercredcol="user_pass"
userroletable="user_roles" rolenamecol="role_name"/>Make sure that the JAR file containing the JDBC driver referenced by the
driverNameattribute is placed in Tomcat'sCLASSPATH. If you are using the JDBC-ODBC bridge, the driver will already be in Tomcat'sCLASSPATH. You will also need to replace the user and password values with the appropriate values for your database installation. This new<realm>entry defines aJDBCRealmthat leverages our database as its container of users. The attributes used in the<realm>element, with additional optional attributes, are described in Table 7.Table 7. The Attributes of the <Realm>ElementAttribute Description classnameThe fully qualified class name of the Realmimplementation.driverNameThe name of the driver used to connect to the database containing the users. connectionURLThe URL referencing the database containing the users. connectionNameThe usernameto use when connecting to the database. If you are using MySQL, you can encode the username directly on theconnectionURL.connectionPasswordThe passwordto use when connecting to the database. Again, if you are using MySQL, you can encode the password directly on theconnectionURL.userTableThe database table containing the user's information. userNameColThe column in the userTablethat references the user'susername.userCredColThe column in the userTablethat references the user'spassword.userRoleTableThe database table containing the mapping between the userTableand the table containing the possible user roles.roleNameColThe column in the userRoleTablethat contains a roles given to a user
To complete this configuration change, stop and restart the Tomcat server.
That is all there is to it; your Web application is now protected by a JDBCRealm. To test this change, try logging in to the /onjava Web application, entering a username from the users table that has a role of onjavauser. You should see a dialog similar to Figure 1 above.
James Goodwill is the co-Founder of Virtuas Solutions, LLC, a Colorado-based software consultancy.
Read more Using Tomcat columns.
Return to ONJava.com.
-
JDBC Realm Authentication with Tomcat 5 and Eclipse 3
2006-08-21 16:04:50 jCoder1973 [View]
-
Problem with Login
2006-03-09 12:09:20 gesuino [View]
-
How to protect a directory in my application
2006-03-07 05:44:54 singhgurpreet [View]
-
Disable Userdir
2006-03-01 07:31:25 kdsimms [View]
-
how do you know the DB conn is working?
2004-12-01 07:18:42 beam [View]
-
suggestion for more robust database schema
2004-04-27 12:51:40 pmocek [View]
-
error in article
2004-04-27 11:48:50 pmocek [View]
-
need clarification
2004-04-07 23:52:46 msreedhar2k [View]
-
Problem in accessing HTML pages
2004-03-02 23:03:49 bdsai [View]
-
Very good Article
2004-02-23 22:01:27 sripathi [View]
-
Realm accessed through DataSource
2004-02-03 23:44:30 sbrbot [View]
-
Basic authentification error
2004-01-15 01:55:19 joepizza [View]
-
Basic authentification error
2004-01-15 01:53:19 joepizza [View]
-
Connection Realm Wrong in example
2003-08-14 13:24:28 anonymous2 [View]
-
can also be done by a security filter
2003-08-07 06:19:54 anonymous2 [View]
-
Using Realm from within a context.xml file
2003-06-19 14:06:17 anonymous2 [View]
-
HTTP 403 - Wrong password and then?
2003-06-08 10:19:42 anonymous2 [View]
-
JDBCRealm: Protecting all webapps
2003-05-05 06:12:49 skladov [View]
-
Error when entering username and password
2003-03-26 13:09:53 anonymous2 [View]
-
Sybase & Tomcat
2003-03-25 12:37:12 cvilsack [View]
-
Well explained
2003-03-07 02:33:39 anonymous2 [View]
-
Embedded Tomcat 4.0 and Realms
2003-02-25 10:57:54 anonymous2 [View]
-
Stayed clear of jndi
2003-02-13 16:54:11 anonymous2 [View]
-
Separate realms for different applications
2002-11-08 14:19:51 roymcbrayer [View]
-
Tomcat JDBC ODBC error
2002-10-15 11:31:03 anonymous2 [View]
-
Table confusion
2002-10-03 07:48:40 petit [View]
-
Jndi - LDAP
2002-09-24 02:24:31 anonymous2 [View]
-
Part4 "Web Applications and the ServletContext"
2002-08-20 11:40:19 taylormaai [View]
-
404 Error
2002-07-22 05:54:48 der011 [View]
-
tomcat jdbc realm
2002-05-13 06:57:59 dwbrown [View]
-
tomcat security
2002-04-12 07:50:56 reza1001 [View]
-
Tomcat Secure Realms for LDAP directory
2002-04-12 01:05:24 schen01 [View]
-
Oracle Driver URL cannot take username/password
2002-02-27 12:21:21 c_novak@yahoo.com [View]
-
CLIENT-CERT auth-method not covered!
2001-12-28 14:24:59 chaitresh [View]
-
Transactions are not enabled
2001-12-07 16:11:18 mbober [View]
-
Error in connection string
2001-10-23 09:48:48 sonofseven [View]
