Tuesday, May 29, 2007

JDBCRealm


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
(
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);


create table roles
(
role_name varchar(15) not null primary key
);

create table user_roles
(
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key( user_name, role_name )
);



Here is sample output from the tables:

mysql> select * from users;
+-----------+-----------+
| user_name | user_pass |
+-----------+-----------+
| tomcat | tomcat |
| user1 | tomcat |
| user2 | tomcat |
| user3 | tomcat |
+-----------+-----------+
4 rows in set (0.00 sec)

mysql>
mysql> select * from roles;
+------------+
| role_name |
+------------+
| tomcat |
| role1 |
+------------+
2 rows in set (0.02 sec)

mysql>


mysql> select * from user_roles;
+------------+-----------+
| role_name | user_name |
+------------+-----------+
| tomcat | user1 |
| role1 | user2 |
| tomcat | tomcat |
| role1 | tomcat |
+------------+-----------+
4 rows in set (0.00 sec)

mysql>

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!!

Saturday, May 12, 2007

log4j



The log4j is a very good logging tool for Java programming language. It is very easy to use and  configure.

The stable version of the JAR file can be downloaded from here. Extract the archived file and include the JAR file in your CLASSPATH.

log4j is configured using properties file or XML file. It is a 2 step process to configure log4j. First we set the APPENDER that tells log4j where to output the log messages and second LAYOUTS that sets the format of the output.

Appender

Appender tells log4j where to output the log messages. Following are the appenders available for log4j.

ConsoleAppender appends log events to System.out or System.err. The default target is System.out.
FileAppender appends log events to a file.
DailyRollingFileAppender extends FileAppender so that the underlying file is rolled over at a user chosen frequency.
RollingFileAppender extends FileAppender to backup the log files when they reach a certain size.
WriterAppender appends log events to a Writer or an OutputStream depending on the user's choice.
SMTPAppender sends an e-mail when a specific logging event occurs, typically on errors or fatal errors.
SocketAppender sends LoggingEvent objects to a remote a log server, usually a SocketNode.
SocketHubAppender sends LoggingEvent objects to a set of remote log servers, usually a SocketNodes
SyslogAppender sends messages to a remote syslog daemon.
TelnetAppender is a log4j appender that specializes in writing to a read-only socket.