Wednesday, December 8, 2010

Spring security LDAP authentication

I was experimenting on spring security and found that with spring 2.5 LDAP authentication was a pain. This is what I did to get around the issue.

First my maven POM had these dependencies




Your web.xml should have the contextConfigLocation which refers to the location of the spring security config xml file, the spring web context loader and the spring security filter.

<!-- Spring context loader servlet and listener class need to spring managed beans-->
<listener-class> org.springframework.web.context.request.RequestContextListener </listener-class>

<servlet-class>org.springframework.web.context.ContextLoaderServlet </servlet-class>

<!--spring security filter chain -->



The contents of context-security.xml is as follows

<!-- first configure the LDAP url -->

<ldap-server id="appLdapServer" url="ldap://,o=ME" />

<!-- configure the LDAP authentication provider here you can specify search filters
which will start the LDAP search from a given node in the tree. you can also specify
users with certain specific attributes so that only those users can have access to your application web page

<ldap-authentication-provider server-ref="appLdapServer"
user-search-filter="(cn={0})" group-role-attribute="ou=Engineering"
user-search-base="ou=Software Eng"/>

Now the http intercept url xml elements need to be written for this we shall have a simple rule which shows pages in folder inbox if the user has been authenticated otherwise the user gets redirected to login or error page in folder auth so make sure you have only unsecure pages in the auth folder.

<http access-denied-page="/auth/denied.html">
access="ROLE_ANONYMOUS" />
authentication-failure-url="/auth/login.html" />
<logout logout-url="/auth/logout.html"
logout-success-url="/" />
<anonymous username="guest" granted-authority="ROLE_ANONYMOUS"/>

The IS_AUTHENTICATED_FULLY is a spring security access constant which does the trick to all pages inside inbox to be visible if the LDAP authentication is successful

No comments: