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.

No comments:

Post a Comment