A while back I started working on a web application with, at the time, all new java technologies. Once the web application needed an authentication framework I turned to acegi (now part of Spring Security API). Acegi security provides much of the authentication features a developer requires in a web application including: remember me, failed login handling, public content access and so on. Other technologies I used where Struts2, Spring and Hibernate.
Since I was using hibernate and spring daos I thought it best that I store my user names and passwords in the database via the same mechanism. That is, I needed to use Acegi for authentication and Hibernate and Spring for managing the user detail persistence layer. After searching a few forums it turned out that many people wanted to do the same, but no one was providing a solution. Proceeding with a brief brainstorm session and research into the acegi API I came up with my own UserDetailsService implementation backed by hibernate and spring. Its simple but it provides me with what I need and I hope it will be a helpful reference for others as well.
The code used for the implementation is packaged as auth.jar with class and source files for your reference. Please do with it as you like (BSD license). The contents of the archive are described below:
- Authority.java - a userw authority (i.e. USER_ROLE) Implements UserDetails
- User.java - a user, having user name and password. Implements GrantedAuthority
- UserDetailProvider.java - Uses hibernate to provide the user details service. Implements UserDetailsService
- model.hbm.xml - Hibernate model description for User and Authority
Together these small classes provide the groundwork for our authentication layer. Next, the hard part is dealing with all of the Acegi spring configuration.
Acegi is loaded via two spring application context xml files. This first one is pretty basic, first it initialises my hibernate authentication implementation. Next it initialises the authentication provider.
The second application context config is applicationContext-acegi-security.xml. This is mostly copied directly out of the acegi example and simplified as much as possible.
The main beans here are:
- authenticationManager - uses the above defined daoAuthenticationProvider
- filterInvocationInterceptor - specifies which roles have access to what
- authenticationProcessingFilter - specifies which pages are used for authentication
All together, these resources will probably not work for you as they require a web application to be deployed. However, the pieces I provide should make integration into your application as simple as possible. If there are any issues or suggestions please let me know.