Thursday, 31 July 2008

... migrating from ACEGI to Spring Security

this piece of work is done by now. Here are my experiences so far. In most cases one can take Spring Security and use it as given. There are plenty of providers and mechanisms, that are supported (like LDAP, JAAS, CAS, digest, form authentication etc.) and would fulfil one's needs in 90% of cases. When it gets tough? Like in our case, when you e.g. would like to use some custom security providers or load access definitions from a file, describing your access rules in some other way.
Back again to a normal case... The new feature presented in Spring Security is a new configuration syntax, which allows you to skip defining all the beans in a Spring manner. A minimal configuration looks like:
Code...

Looks fine doesn't it? Than you have to specify an authentication provider to map a user with a valid password to the roles. And that's it. So what is when you'd like to override some filter in the filter chain? Or to define a custom ports mapping for HTTP and HTTPS? Or would like to use a custom JSF login page? I'll tell you. You end very soon by good old ACEGI Security configuration syntax with beans.

There was one issues that posed special degree of difficulty: how do I configure a custom JSF login page using Spring Security authentication mechanism without having to reimplement the whole authentication logic? The solution is to use Spring Security j_spring_security_check.jsp as a filter process URL and correctly map the username and password fields to be forwarded as request parameters. It looks something like this:
Code...

It will only work in connection with the login JSF designed as follows:
Code...

and not to forget the navigation rule in the faces config:
Code...

and the last point - the JSF backing bean to map username and password input from the login page, which is defined as a managed bean in faces config:
Code...

The java class LoginView contains only getter and setter for username and password and an exception handler to forward a failed login message from JSF to Spring:
Code...


Another interesting topic is localization. How to get localized login error messages displayed if locale is managed in JSF. I'll write on it some other time...

2 comments:

Anonymous said...

thx! that really helped me a lot!

Andriy said...

my pleasure