JDBCRealm in GlassFish with MySQL
Monday Apr 23, 2007
In these few months, there were several discussions of using GlassFish JDBCRealm with MySQL. In this blog, I will share my experience about using GlassFish JDBCRealm with MySQL.
- Download the
MySQL Community Server.
I have downloaded the Solaris 10 (x86, 32 bit TAR package),
version 5.0.37, of the "MySQL Community Server".
- Expand the download file.
gunzip mysql-5.0.37-solaris10-i386.tar.gz
tar xf mysql-5.0.37-solaris10-i386.tar cd mysql-5.0.37-solaris10-i386and read INSTALL-BINARY.
- Set up the grant table.
scripts/mysql_install_db - Start the MySQL server.
bin/mysqld_safe - Set a password for the MySQL "root" user
bin/mysqladmin -u root password YOUR_PASSWORD - Create database and table. The following is a
sample command.
bin/mysql -u root --password=YOUR_PASSWORD create database database01; use database01; create table usertable(userid varchar(10) primary key, password varchar(32) not null); create table grouptable(userid varchar(10), groupid varchar(20) not null, primary key (userid, groupid)); alter table grouptable add constraint FK_USERID foreign key(userid) references usertable(userid); commit; grant all privileges on *.* to 'root'@'YOUR_HOST' identified by 'YOUR_PASSWORD' with grant option;
Note that you may like to replace YOUR_PASSWORD and YOUR_HOST in above. - Populate user, group and passwor data.
For the purpose of testing the database, you may try to use
clear text password first as follows:
insert into usertable values ('webuser', 'webuser'); insert into grouptable values ('webuser', 'employee');For MD5, please take a look at another blog on JDBCRealm. - Download
the JDBC driver from Connectors > Connector/J .
I have downloaded mysql-connector-java-5.0.5-bin.zip
- Unpack the package and copy the JDBC driver to $GLASS_HOME/lib.
unzip mysql-connector-java-5.0.5-bin.jar
cd mysql-connector-java-5.0.5
cp mysql-connector-java-5.0.5-bin.jar $GLASSFISH_HOME/lib - Restart the GlassFish server in order to pick up the JBDC driver.
- Create a Connector pool in Admin Console as follows:
Name MySQLPool Resource Type javax.sql.DataSource Database Vendor MySQL then click "Next" and add the following properties:
serverName YOUR_HOST port 3306 databaseName database01 user root password YOUR_PASSWORD Note that different versions of the JDBC driver may have different properties. You may need to check the readme file there. Furthermore, you may need to remove extra default properties from Admin Console.
- Create a DataSource
jdbc/mysqlassociated with the above pool. - Create a JDBCRealm, named
jdbcrealmwith the following properties:datasource-jndi jdbc/mysql user-table usertable user-name-column userid password-name-column password group-table grouptable group-name-column groupid jaas-context jdbcRealm digest-algorithm none Note that if you are using MD5 for password data, then you need to set value of digest-algorithm to MD5.
- Now a JDBCRealm is ready and it can be used by specifying
it in deployment descriptors.
If there is anything wrong and cannot authenticate,
then one can turn on security log to FINE level and
check if there is any exception in
server.log.











I can get the JDBC realm working ok but no luck in...
Muyiwa, I also receive ArrayIndexOutOfBoundsExcept...
I can see from the server.log that the logon is be...