I recommend taking one of the JBoss login modules and extending it. There are some nice abstract base classes that will save you a lot of code. I had
trivial example of doing this in my blog. The JBoss documentation covers this and there are other examples on the wiki.