9
A question that often comes up in when looking through JSF forums or idling on IRC is, “How do I secure my JSF app?” to which, of course, there are a myriad of options. At IEC, we use Acegi Security. That answers only part of the “how,” though, as Acegi is not the easiest thing to learn. In this blog entry, I’ll detail how we have Acegi implemented at IEC. While it’s not perfect, it works well for us, and should be enough to get someone moving in the right direction.
The first step, of course, is to download Acegi, and integrate with the web application. Once the jars have been installed in WEB-INF/lib, web.xml needs to be edited:
That’s the easy part. The Acegi configuration, applicationContext-acegi.xml in this example, is where the difficulty comes in:
I’m not Acegi expert, and I make no claims to understand what all is going on here, but I have included the whole config file as I found it difficult (at the time, at least) to find a complete example that uses the Acegi 1.x package and class names. I must also note that I’ve done my best to back out IEC-specific changes, so there may still remain same changes that need to be made to get this to work in a “clean” environment (read as: this should work, but it may not. If you have to make changes, please let me know and I’ll fix my example).
Once Acegi is setup and configured, we can start protecting resources. The configuration above protects all URIs that start with /foo, but it is also sometimes desirable to protect only certain parts of a page. Acegi ships with some JSP tags that make that possible, but these work outside the JSF lifecycle. To solve that problem, Cagatay Civici has written some JSF tags that do live inside that cycle.
Here’s an example from an app we have in poroduction. In this particular snippet, if the user has the correct permissions, we allow him to approve a request or resubmit the order:
And that’s all there is to it. Once you get it setup, it’s really not too difficult to work with.
I have seen some balk at using Acegi, given its dependence on Spring, but, while it’s true that you must have Spring in your classpath for Acegi to work, by no means does that require that the application itself be Spring-based. In fact, we’re using this very approach in an application that uses no Spring at all, but, rather, some EJB3 session beans (and Ajax on the front end). So, if you can live with the extra few jars to solve the dependencies of Acegi, it plays well JSF, even in a non-Spring app.
What are your thoughts? Do you see ways to improve this approach, or do you have a better one altogether? I’d love to hear your feedback.
Popularity: 18% [?]

Thanks for all the effort you took to write this nice piece! Although I’m not using JSF I am very interested in using Acegi. The pieces of code are really excellent. As a true developer I always have to see some code to see what the description with it means.
January 22nd, 2007 | #
You’re more than welcome. I’m glad it helped!
January 22nd, 2007 | #
The problem I find is that when JSF is used with both Acegi and Facelets, the security has a glitch. Since JSF no longer uses a request dispatcher forward() call when heading to the view, but rather allows Facelets to do its thing, it is possible to secure an initial JSF request url, but not the resulting rendered page. I have seen talk, but no code, regarding an Acegi phase listener that would check the view id against the access manager. Seam offers a nice solution with its page actions. It seems to me like more people would be interested in this problem…and one that cannot be that difficult to solve.
March 4th, 2007 | #
Hi,
thanks for the effort.
Could please post the code of the login.jsp page?
Thanks in advance
March 5th, 2007 | #
This may can also help to get Acegi and JSF running:
http://blog.rainer.eschen.name/2006/11/14/howto-secure-jsf-via-springacegi/
BTW: For me using Spring is a big add-on, because it allows to create more comfortable backing beans:
http://blog.rainer.eschen.name/2006/11/16/the-observer-pattern-in-mixed-dependency-injection-contexts-spring-jsf/
March 9th, 2007 | #