Spring Security – The new and improved ACEGI

Ah! Good ol ACEGI. Or should I say the good old new ACEGI aka Spring Security. In one of my previous posts I blogged about configuring good ol ACEGI. Since I last looked at it ACEGI is now Spring Security. Configuration nightmare has been reduced greatly.

For those attempting Spring Security for the first time, a piece of advice. Ignore all comparisons to ACEGI and move on. You will find your brain in better shape at the end of the excercise.

First of all to make my life easier I created a Dynamic Web Project in Eclipse 3.3.2. Next I have Tomcat configured to run within my IDE for quick testing. My goal is to simply protect a bunch of web pages using Spring Security. Its easy to extend this to larger web apps.

First lets see the web.xml below.

Only thing of interest is the listener org.springframework.web.filter.DelegatingFilterProxy. I would love to compare this to ACEGI configuration but I will resist the urge. Just forget any old stuff. Suffice it to say that all URLS with the configured pattern pass through this filter and Spring Security is “performed” on them.

Next let us see the login.jsp page:

Refer to the login form and the way the parameters are named. For Spring Security to pick up the attributes you must name it the same as above.

Most important … here is the spring context file application-security.xml

  • Spring namespace is used to configure security.
  • security:authentication-manager need not be listed. If not a default will be created. List it if you need to refer it from some other configuration. In my case I just did to make the point.
  • security:http is very self explanatory. We configure form login page names and the roles-to-URL patterns to protect. You can choose to not have this mapping here and instead implement your own object definition source class and provide the info from there.
  • Finally security:authentication-provider is used to configure a password encoder and in this case a in-memory user store. The password is ‘password’. I have listed the MD5 values above to show the use of the encoder.

Once logged in the user is sent to the index.jsp which shows some common content and some admin specific content (if user has admin role).

  • Note the use of the taglib authz to show/hide admin related content.

Following screen shots show you how this all works…

The bold text above is only displayed to users with admin role.

Now for a few tips. Obviously you will not be hardcoding the user name and password into the configuration. Right my friend! Now for this you can implement your own class that gets the credentials from wherever you choose. This class then hooks into Spring Security. Here is a sample class CustomUserService.java where I have mocked the credentials in code.

In the application-security.xml file you need to make the following change.

The rest of the code should work same. Finally one more useful feature that I have used with the good ol ACEGI. That is to provide security access protection to the service layer. Add the following to your configuration…

Next add the annotations @Secured( {“ROLE_SECRET_AGENT”} ) to your service methods.