Saturday, January 16, 2010

Jaas, part 2

So now that we have JAAS configured at our JBoss instance, we are free to build an application that uses this configuration. In this post I will build a minimal login and the login strategy that will be used in all future posts. I use Eclipse as my IDE, so I will follow the steps needed for that IDE more than a generic way to do things.
First, add the JBoss server to the IDE, this is from the "Servers" tab (in Java EE perspective) New->Server->JBoss 5.0 and the location where you installed JBoss. Use the "default" server as that is the one configured with the policy we defined before.
Then create a new Enterprise Application Project, use whatever name you like, I will refer to this as "Profile1" as it is the one that I am using. Then create one Dynamic Web Project, in the configuration combo, select "JavaServer faces v1.2" and be sure that this added into the enterprise application project previously created (in my case Profile1). We will use JavaServer Faces as the framework as it is nicely bundled to the JEE stack, the version we will use is JSF 1.2 that leaves a lot to be desired. I wish JBoss had an implementation of the JEE 6 stack where JSF 2.0 is implemented, but this is not the case :-(
Eclipse saves you a lot of time by creating the JSF configuration you and a minimal faces-config.xml I recommend you to try not to use the "graphical" editor of the faces-config.xml file as it is painfully slow. Anyhow, if you never implemented a JSF application, the graphical editor will assist you a lot, but whenever you do a change, check what changed in the real .xml file. Doing this will save you a lot of time, as making the modifications to the text editor is a lot faster.
In this post, we will follow the following steps
  • Create and declare a Login managed bean
  • Create login, success and failure pages
  • Configure the flow of the pages
  • Configure the message bundle
  • Configure the login filter
  • Add some test credentials to the database and test
First, create a Login class that will be a managed bean for JSF, to save you time, you can use this template: and the LPCallback and also create the class SessionUtil with the following content: You may notice a few things here
  • We are storing the login/password in the user context.
  • After we perform the login, we logout the user
This is something that will be explained in the next part of the Jaas setup. There are different approaches the problem of changing a user authorizations without killing the user session or forcing a new login. The implementation shown here is a simple implementation, but is one that resolves the largest part of the undesired side-effects associated with this issue.
Now we have to declare this class as a managed bean to be used later. This is very simple, just edit the file faces-config.xml and add the following section: Then we build a simple login page. I will create a simple .jspx page, I cannot stress more the huge benefits of using .jspx versus .jsp, they are superior in every single thing that really counts. I do know that under special conditions they can be more verbose, but that is a small price to pay for the number of benefits associated with them.
I created a simple login.jspx page like the following You can see that we are referencing the object #{msg.login_user}, these are phrases that will be configured later in this post.
Now, we create a successLogin.jspx and failureLogin.jspx page that will be the result of the login page, here is a sample failureLogin.jspx page (successLogin.jspx can be exactly the same with some labels changed) Not, it is time to configure the flow from the login.jspx to the successLogin.jspx or to the failureLogin.jspx. This is configured at the faces-config.xml by adding navigation rules (just like you would do with Struts) Now, it is time to configure a the resource bundle that we are using in all .jspx pages, again, this is done in the faces-config.xml. You first create the messages.properties file at com.conecat.jee and then reference it at bundle, in our case messages.properties looks like this and adding the reference to the faces-config.xml looks like this The last step is adding a login filter that will add the credentials to the session to JBoss (or nay other web application container) to use. Here is a somehow simplified version of this login filter, I had to remove some code not to make this post too long (like it is right now) but does work as expected and then we reference it by modifying the good old web.xml by adding The last part, is adding a test credential and role to the database, you should use the same database that we setup in the previous post, deploy the web application and test!

I know there is a lot to digest here, so has fun and good luck with the minimal Jaas integration for JBoss.

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.

Doing the JEE dance

The Java Enterprise Edition standard is as big as it gets. It tries to solve every possible issue you may see whenever developing a web application, and just like every solution that tries to do the same, it has it's strengths and weaknesses. This blog is about the frustrations and successes while trying to build a web application using nothing but standard components and "standard" JEE implementations.
The first thing to define is the specific technologies to use in the web application and, of course, the application container :-) I will start using JBoss 5.1 because it is the most popular one and the the first choice for most developers and not because I have any special attachment to it (that I do not). Regarding what to use out of the JEE stack, I will try to use as many parts of the stack as possible and I am aware of.