Showing posts with label spring-security. Show all posts
Showing posts with label spring-security. Show all posts

Thursday, March 25, 2010

Integrating failover strategy for Spring Security NTLM

During implementation of the NTLM authentication into our application, I wanted to achieve failover to standard login page (html form) if NTLM authentication fail.

In order to obtain the wanted behavior I extended some Spring Security classes and hack them a little bit, so I created:
  • An AuthenticationEntryPoint strategy to switch between NTLM or standard login
  • A custom AuthenticationProvider to prevent password checking when user authenticate with NTLM
  • A custom NtlmProcessingFilter to disable NTLM authentication if a remember-me cookie is present
The thing I was unable to do is detecting if the user client is NTLM compliant before starting the NTLM challenge. Due to the way the protocol is build (and certainly for security reasons) it's impossible to know if the client is NTLM compliant before launching the challenge.

So let's took the scenario of a Google Chrome user:
  1. A popup (HTTP realm) authentication window is shown to the user
  2. He hit enter with invalid credentials (i.e. empty username/password)
  3. The browser is redirected to the standard html login form
By using 'remember me' the NTLM authentication won't be shown next time the user access the site due to the presence of the cookie.

If the user client is Internet Explorer no authentication window is shown because the navigator use directly the information of the logged user. If the authentication fail, then the standard html login form is displayed letting him login with other credentials.

This implementation was made using Spring Security 2.0.5, but can be easly converted to Spring Security 3.0 using the Spring Security NTLM 3 module I recently created. Don't hesitate to ask me if you want that I publish an updated version.

Download
You can download a sample application including hacked class here:
http://github.com/downloads/aloiscochard/spring-security-ntlm-samples/spring-security-ntlm-samples-2.0.5.SNAPSHOT.zip

Sunday, March 21, 2010

Spring Security NTLM 3

As of version 3.X Spring Security doesn't include the NTLM extension anymore.

For the purpose of using NTLM authentication on Spring 3 projects, I migrated the 2.0.5 NTLM extension to the new version of Spring Security.

The full project sources are available here:
http://github.com/aloiscochard/spring-security-ntlm

I haven't took time fixing the following bugs found on version 2.0.5:
https://jira.springsource.org/browse/SEC-1003
https://jira.springsource.org/browse/SEC-1087

If your are interested to help correcting this defects don't hesitate to get in touch.

Download
You can download the spring-security-ntlm snapshot here:
http://github.com/downloads/aloiscochard/spring-security-ntlm/spring-security-ntlm-3.0.2.SNAPSHOT.jar

Usage
Due to the fact that NTLM isn't integrated in new version of spring-security-core, you need to add a custom filter.
Here is a snippet describing briefly how to do that:
Don't hesitate to ask if you need help, this extension work exactly as version 2.0.5. You can use an example from version 2.0.5 if you are starting from scratch.

Saturday, December 19, 2009

Integrating Spring Security with NTLM + Crowd

For the purpose of implementing a SSO (Single Sign On) system into our application, I investigated the use of spring-security and successfully implemented it using NTLM and Crowd.

Download Sample Project

Technologies
As most of our users are using Internet Explorer, NTLM seems to be the best solution. By the way, as developer we are using Firefox and fortunately Firefox can provide too NTLM credentials (see this to enableNTLM inside Firefox).
NTLM is used for authentication only (giving windows credentials to the web application) then Crowd is used to store and retrieve user's authorizations.
Atlassian Crowd provide full spring-security integration, and a bunch of connector to communicate with most of directory services available.
I first configured Crowd to use an internal directory to avoid being disrupted by communication with external directories. Then I added connection to our Microsoft Active Directory... but this go beyond the scope of this blog entry.

Prerequisites
  • Add spring-security and spring-security-ntlm and theirs dependency in your project
  • Add crowd-integration-client and his dependency in your project 
  • Integrate Crowd integration client by following this documentation from step 1 to 3.0 
Integration
First of all, create a new spring application context (applicationContext-security.xml) with the following content :

Then add it in your web application descriptor (web.xml) with the spring security filter, you must end with something like this :

And finally, create the custom UserDetailsAuthenticationProvider (unfortunally I was suprised to didn't find a provider that suit my needs...):

You're done !

Creating test pages
As you may have notified, the file applicationContext-security.xml defining three different level of security:

  • Anonymous access authorized by default to all page
  • Standard users can access the 'secure.jsp' page
  • Administrators can access the 'admin.jsp' page
So create the pages index.jsp, secure.jsp and admin.jsp with the content you want or change the security context to suit your needs.

Crowd configuration
Open the Crowd administrator console, and create user corresponding to your Active Directory account name, add it to the application you created when you integrated Crowd (more informations).
Create two groups (myapp-user and myapp-administrators, you can name it differently but you must then update your spring security context correspondingly) and add them to the user created before.
Don't hesitate to read the Crowd documentation for further information. Specially if you want add a connector for your directory, but I strongly recommend you to first test your application using a Crowd integrated directory.

Conclusion
Integrating spring-security into an existing web-application is relatively straight-forward, specially if you already using spring. Some specific configuration is needed to enable NTLM and Crowd support but documentation can be easily find online.
And best of all, if you want to change for Kerberos in future you can do it simply and without changing the way your application works... thanks to Spring IoC :-D
And thanks to Crowd you can change connection to different directory services without loosing user authorizations (of course user-name must be the same...).

References