By ByronNevins on Oct 03, 2007
Here is another good blog on this subject.
In my last blog I showed you how to make a jdbc-resource that you can use for accessing a database.
Now I'll explain how to use that jdbc-resource to setup JDBC Realm Authentication and Authorization.
The job of a realm is to maintain a repository of user information. Each user has one password and 0 or more groups that he belongs to.
At runtime a Realm is initially given a username and password and the realm then figures out if the user is authentic by checking the password. It checks for authorization by seeing if the user's list of groups is authorized to use a resource (more on this later).
How to Create The Realm for userauth
Using Admin GUI navigate to Configuration/Security/Realms. Press the "new" button
Pick the classname that ends in JDBCRealm. Use the jndi name of your jdbc-resource -- if it isn't userauth.
I'm assuming that you are starting from scratch with no user database. If you already have such a database, you need to make sure your database table and column names are entered correctly and that your tables correspond to what JDBCRealm needs. Such details are coming up...
Tip: Use the names in the screen-shot for the 2 table names and 3 column names. These names are as good as the next. If you standardize on these names you can use some tools I've developed for help administering and testing the realm.
Database user and password -- I'm not so sure about this. The connection you get from the jndi name already has the username and password for that database. I put the username and password from my database here and everything worked fine...
You have to decide whether to save passwords as clear-text or encrypted. If you want clear-text, leave the edit field blank. If you want your passwords encrypted set this field to MD5.
My recommendation is to use MD5 for the passwords. I have some tools with the code you need to convert a plain text password to an encrypted string in the right format for JDBCRealm.
On the other hand you can set it to no encryption -- and it will be much easier to seed the tables with test user info. You can always change it to MD5 later when you have everything working.
All of the GlassFish administration work is now done.
Now you need to add some tables to your database. You can run this sql script and it will do all the work for you. The script also adds one user with username=admin and password=admin.
The easy way to do this is to run "ij" -- a javaDB sql console -- there is a copy here: <gf>/javadb/bin
ij> connect 'jdbc:derby://localhost:1527/userauth;user=APP;password=APP;create=true';
ij> run 'createdb.sql';
Now it's time to try out a protected web module. What we want here is the World's Simplest Web Module.
In NetBeans, create a new web module. The web module consists of one hello world jsp file. Perfect. If you don't use NetBeans then create some sort of ultra simple web module.
We have to add quite a bit of stuff to web.xml. Netbeans makes fast work of it:
Here are the additions to web.xml (generated by NetBeans)
I have 2 roles named "ADMINISTRATORS" and "USERS". I use these same names as the groupid values in the database table. This makes the role-mapping a bit easier because you use the exact same name on both ends.
You must setup the roll-mapping in sun-web.xml The easiest way to do it so directly edit the file and add these lines:
Build and deploy this web module and then run it. It will ask you for authentication info. If you enter "admin" "admin" you should be allowed in to see the jsp page.
If you enter the name and password for someone that does not belong to the group "ADMINISTRATORS" then you will get an access violation error.
If there is interest, I'll follow up with some tools you can use to add users dynamically and to manage the database itself.