Welcome!

Java Authors: Maureen O'Gara, Patrick Curran, Carmen Gonzalez, Michael Sheehan, Jonny Defh

Related Topics: Java

Java: Article

Introduction to Acegi

Mastering the security framework

The filterSecurityInterceptor bean checks that the authenticated user has the right roles (or permissions) to access a particular objectDefinitionSource. Here we are using AffirmativeBased voting, which means the user just has to have one of the roles specified in the objectDefinitionSource. This is most likely what you will use, but Acegi does have a unanimous voter that ensures that a person has every role specified in the objectDefinitionSource before granting access. By now you may have realized that objectDefinitionSource determines who can access what.

The objectDefinitionSource starts off with the same two configuration instructions that filterChainProxy did, namely converting all URLs to lowercase and using the Apache ANT style for regular expressions. Next, we define which roles are allowed to access a particular URL. In our example, we give anonymous access to the /acegilogin.jsp page so that unauthenticated users can arrive at this page to log in. The next line in the objectDefinitionSource provides access to everything below the /secured directory for any user with the ADMIN role. Finally, we add a line that starts with /** to match on every URL. The filter will stop once the URL matches on a URL, so make sure you put specific regular expressions toward the top and broad regular expressions toward the bottom to ensure you get the desired behavior. If you were working with Struts, you could either set up your struts in modules http://struts.apache.org/struts-core/userGuide/ configuration.html#5_4_1_Configure_the_ActionServlet_Instance or simply specify the StrutAction (e.g., /CustomerAdd.do) in the objectDefinitionSource.

At this point, we are done with applicationContext.xml file. To complete our demonstration application, all we need to do now is create a login form and put something in the /secured directory to see that our Acegi authentication and authorization configuration is working. (See the acegi-demo.zip for /acegilogin.jsp and /secured/index.jsp.)

The login form is very simple; it has input fields for the username and password, j_username and j_password, respectively, and a form action pointing to j_acegi_security_check since that is what the authenticationProcessingFilter filter listens for to capture every login form submission.

Test your configuration and inspect the Tomcat logs and the Log4J log file that we configured for this application if you run into problems.

Now That I'm Over the Initial Learning Curve, What's Next?
Once you have this simple Acegi demonstration application running, you will undoubtedly want to increase its sophistication. The first thing I would want to do is to add a JDBC profile in addition to the simple in-memory profile.

I can understand the excitement after getting the initial application up and running, but you still have some reading to do in order to eclipse the initial learning curve. Read through the articles posted in the External Web Articles section of the Acegi Web site http://acegisecurity.sourceforge.net. Read through the Reference Documentation provided by Ben Alex, the creator of Acegi. Ben does a good job of providing help through the support forum too. Also, read the well-kept JavaDocs as your main source of information once you get familiar with Acegi. Of course, you can opt to read the source code - it's open source!

Since this is your first time using Acegi, test after each change to the applicationContext.xml file. The process of "one change, then test" will help you understand exactly what change to the applicationContext.xml file caused an error if one should occur. If you make four changes to that file, restart the application and get an error, then you won't know which one of the four changes caused the error.

Note that I kept this application very simple. As you add in features such as Acegi's caching, you will need to add the appropriate libraries (or JARs). Look at the Acegi example application available on the Acegi Web site to get access to all the various libraries. The example application on the Acegi Web site is complex, so it is not the best place to start to get over the initial learning curve, unfortunately, hence my attempt to make it easier with the article!

No Groups in Acegi?
Acegi will let you work with the notion of groups. When you put a person in a group, you are just grouping the permissions (or roles) that the group does or does not have. So, when you set up your LDAP or JDBC profile, you need to make sure that the query returns the roles that the users' groups should have access to.

Conclusion
Acegi is a very configurable, open source security framework that will finally let you reuse and port your security layer components. It can be daunting at first, but this article should easily remove the stress in getting over the learning curve. Remember, you need to get this simple application running, test after each change, and read the recommended readings to fully surmount the initial learning curve. After you follow these steps, you will be well on your way to mastering Acegi.

I welcome all feedback and/or suggestions for further aspects of Acegi to cover in future articles.

More Stories By David Hardwick

David Hardwick has 9 years of IT industry experience in B2B, non-profit, and public sector industries. He is manager of technology at Sapient Corporation where he is involved in application development and technology consulting.

Comments (13) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Aymens 08/25/08 09:46:11 AM EDT

I'd like to think you for this article, good and simple!

While reading, I was wondering how could we manage 'Roles'? Can we add meaning to roles ? And build them based on rights (MANAGER_ROLE=READ, WRITE, DELETE ...

Vitaly 09/01/06 01:58:15 PM EDT

I'm looking for a way to integrate Struts, Acegi and Spring in a web application. It turns out that you have to either load context files with context listener registered in web.xml (Acegi+Spring integration) or with a struts plugin (Struts+Spring integration). But I don't want to separate my context into 2 parts: one for security and the other for the whole application. I might need security beans inside of the application. Currently at least one bean - dataSource is shared by acegi and struts (Hibernate).

Mahmood Khan 08/25/06 01:27:31 AM EDT

Will it work for Desktop based applications using J2EE architecture. Client, Application Servers (EJB's) and Database.

JDJ News Desk 08/02/06 08:41:35 AM EDT

I recently evaluated the use of Acegi as the security framework for a Web development project. In the end, we decided to move forward with Acegi but in the beginning it took a couple days to come to that decision. The amazing thing is: once you get over the initial learning curve, it's smooth sailing. Hence, I wanted to share my experiences with it because first, I wanted to expose the Acegi security framework to JDJ readers and, second, I wanted to make it easier for JDJ readers to get over the initial learning curve. Once you're over that, you should be well on your way with Acegi.

srihari konakanchi 08/02/06 07:34:55 AM EDT

excellent article for quick start with acegi.
It would be nice if there more details about pros & cons with JAAS and acegi without spring
--srihari

Balagopal 04/25/06 06:26:12 AM EDT

David

Can u please suggest some steps to migrate from InMemoryDaoImpl to JdbcDaoImpl.

Bala

karthik N 03/07/06 06:03:46 AM EST

nice article, how about instance based security demo?

Vikas Hazrati 03/03/06 07:38:06 AM EST

I made the following change

CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
PATTERN_TYPE_APACHE_ANT
/acegilogin.jsp*=ROLE_ANONYMOUS,ROLE_USER,ROLE_ADMIN
/secured/*=ROLE_ADMIN
/**=ROLE_USER

and it seems to be working now.

Any comments?

Further you might also want to look at the welcome file, change it to something as there is no index.jsp present if I hit

http://localhost:8080/acegi-demo

Vikas Hazrati 03/03/06 07:22:13 AM EST

I deployed the application and tried playing around with it on tomcat.

The question is regarding
.....
CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
PATTERN_TYPE_APACHE_ANT
/acegilogin.jsp*=ROLE_ANONYMOUS,ROLE_USER,ROLE_ADMIN
/secured*=ROLE_ADMIN
/**=ROLE_USER

I assume that this means that anything under the secured folder would be accessible by anyone having a ROLE_ADMIN. Correct?

I created a user with the ROLE_USER as

.....

sapient=password,ROLE_ADMIN,ROLE_USER
vikas=password,ROLE_USER

....

Now even if i login with Vikas I am able to access the secured area.

Am I missing something?

Vikas Hazrati 03/03/06 07:22:03 AM EST

I deployed the application and tried playing around with it on tomcat.

The question is regarding
.....
CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
PATTERN_TYPE_APACHE_ANT
/acegilogin.jsp*=ROLE_ANONYMOUS,ROLE_USER,ROLE_ADMIN
/secured*=ROLE_ADMIN
/**=ROLE_USER

I assume that this means that anything under the secured folder would be accessible by anyone having a ROLE_ADMIN. Correct?

I created a user with the ROLE_USER as

.....

sapient=password,ROLE_ADMIN,ROLE_USER
vikas=password,ROLE_USER

....

Now even if i login with Vikas I am able to access the secured area.

Am I missing something?

rakesh patel 02/19/06 05:05:55 AM EST

hi,

i could not get your demo to deploy to my app server Sun Application Server 8.1.

I changed your original web.xml file to be Servlet 2.4 compatible by changing the first few lines. The error i recieve on deployment is:

[#|2006-02-19T08:35:17.881+0000|SEVERE|sun-appserver-pe8.1_01|javax.enterprise.system.tools.deployment|_ThreadID=14;|Exception occured in J2EEC Phase
com.sun.enterprise.deployment.backend.IASDeploymentException: Deployment Error -- java.net.URISyntaxException: Illegal character in path at index 19: file:///acegi-demo_\acegi-demo

Any ideas???

Thanks

Rakesh

Roko Roic 02/17/06 01:15:41 PM EST

This is exactly what the world needed, a simple and "for the dumb" ACEGI walkthru. Thank you.

SYS-CON Italy News Desk 02/02/06 03:19:35 PM EST

I recently evaluated the use of Acegi as the security framework for a Web development project. In the end, we decided to move forward with Acegi but in the beginning it took a couple days to come to that decision. The amazing thing is: once you get over the initial learning curve, it's smooth sailing. Hence, I wanted to share my experiences with it because first, I wanted to expose the Acegi security framework to JDJ readers and, second, I wanted to make it easier for JDJ readers to get over the initial learning curve. Once you're over that, you should be well on your way with Acegi.