At work we’re building a grails application which needs to authenticate users against our internal user directory using LDAP (Lightweight Directory something-or-other Protocol). Grails has two fantastic plugins to help here:
- Spring security plugin
- Spring security LDAP plugin
Now, getting the LDAP plugin to work has proved somewhat problematic, partially because of me rushing ahead, partially because documentation isn’t quite as good as it could be. So to get the application to authenticate users with LDAP, there are two main ways of doing it.
- Password comparison (where you have to do your own password encoding)
- Bind authentication (where you pass on the username and password and let the LDAP directory authenticate)
for some daft reason, I started off down the Password comparison route, which was made more problematic when I found out that our internal LDAP directory uses an archaic form of password encryption. Once I’d stopped and re-read the documentation, a little light came on in my head and I started trying to get it to work with Bind authentication - then it doesn’t matter how the password is encrypted, you’re delegating that task.
The other part of the requirement is that the roles which will be retrieved following authentication will be from the application database (initially anyway) rather than the LDAP directory using a userDetailsService. So, to get everything working, the various Spring components need configuring manually, rather than relying on the plug in to do it.
So - how did I get it working against a real LDAP directory once the Spring Security & LDAP plugins are installed?
Well, first, in the application configuration, set the ldap context:
1
|
|
Then, in the resources.groovy spring configuration file, set up the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Now, when the application is started, spring security knows it needs to use LDAP to do it’s business and once it’s authenticated, it needs to use our user details service to return the list of roles. Hurrah!
PS - nearly forgot - to use the userDetailsService for retrieving the roles, it has to implement the org.springframework.security.ldap.userdetails.LdapAuthoritiesPopulator interface, returning a collection of org.springframework.security.core.GrantedAuthority