What is JDBCRealm?
Is an implementation of a tomcat 3.X Realm that use a set of configurable tables inside a RDMS to store user's data, this tables are accessed by means of standard JDBC drivers.
The passwords can be stored as digested ( using standard Java's MessageDigest ) or in plain form.
All the parameters, drivers, tables, and columns are user configurable.
Example Config for JDBCRealm
This is an example of how to set up a JDBC Realm. For this example I used the MySQL JDBC driver.1. Create a database.
I made the database named "authority"
2. Create needed tables.
1. The user table.
This table needs the user's name and a password field. In the example I use "users" for the table name, "user_name" for the column that holds the user's name, and "user_pass" for the user's password.
2. The role table.
This table needs the role's set up that will be in any deployment descriptor that is managed under the container this Realm is in. In the example I use "roles" as the table name and "role_name" as the role's name. NB: This table doesn't get used at all by tomcat.
3. The role to user table.
This table joins a set of roles to a single user. In the example the table name is "user_roles", the role's name is "role_name" , and the user's name is assumed to have the same column name as in the user's table ("user_name" in this example.
Here is the SQL I used to create the tables:
create table users |
Here is sample output from the tables:
mysql> select * from users; |
3. Configure Tomcat
Add the information to the server.xml file. For this example I used this entry inside:
<JDBCRealm"
debug="99" driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=test;password=test" userTable="users"
userNameCol="user_name"
userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name" />
The meaning of the attributes is as follow:
| attribute | Meaning |
|---|---|
| driverName | The name of the driver needed to connect to the database |
| connectionURL | The connection URL used to connect to the database |
| userTable | The user's tables |
| userNameCol | The column in the user's table that contains the name |
| userCredCol | The column in the user's table that contains the password |
| userRoleTable | The user's roles table |
| roleNameCol | The column in the user's table that contains a role given to a user |
| connectionName | The name to use when connecting to the database. (Optional) |
| connectionPassword | The password to use when connecting to the database. (Optional) |
| digest | The algorithm used for digest passwords or "No" for plain passwords, the values can be "MD5", "MD2", "SHA", etc... (Optional) |
Done!!