Friday, January 15, 2010

Jaas setup, part 1

The first step will be defining the authentication and authorization mechanism, and just like one would expect from a web application that tries to follow the standards, I will use JAAS. JAAS is not really a part of the JEE stack it is part of the standard JRE, but it is very rare to see a developer use JAAS for non-web applications. To make a quick start, I will use a LoginModule that is already present in JBoss and if I have enough time will implement my a LoginModule later.
The LoginModule that I will use is org.jboss.security.auth.spi.DatabaseServerLoginModule as it has most of the things you would expect from a LoginModule that should implement authentication when the credentials are stored in a database. There are some things that I would like to see implemented like the use of a salt and iteration count to implement an algorithm like what is present at PKCS 5, but it is good enough for a start.

The first thing you have to do is deploying a database datasource. I will use PostgreSQL as it is very simple to deploy and in my opinion works better than all the other open source relational databases. To deploy an existing schema to JBoss, just create a new .xml file at ${JBossInstallDir}/server/default/deploy named postgresql-ds.xml with the following content
where you will have to replace ${database-host}, ${databasename}, ${username} and ${password} to match your configuration. Note: The default port for PostgreSQL is 5432.
For the first try, we will use a simple authorization and authentication mechanism and will make it better as we move forward. The simple mechanism is one table with login/password for all users and another with username/role, so create the following tables with the client of your choice You can find a lot of this information here, even when the content there is a little bit disorganized and not all of what is there applies anymore, it is a good source of reference for JBoss security.
The next thing is setting up the policy that will use this LoginModule at JBoss. To do this, you have to edit ${JBossInstallDir}/server/default/conf/login-config.xml and add a new <application-poilicy> with the LoginModule configured. It is very important to note two things, first that the configuration option dsJndiName must match the jndi name os the database datasource configured before, and second to configure a final LoginModule with the class org.jboss.security.ClientLoginModule at the end of the policy, as this LoginModule will be the one that will store the Subject information for JBoss to use.

This is all that you need to get the authentication ready. In part 2 I will build a simple test web app that uses this policy to login users and let them execute code based on the principals they have.

No comments:

Post a Comment