Custom authentication - Active Directory credentials using spring security 2.0
• Maniezhilan shanmugam
Last week IncorrectResultSizeDataAccessException showed up during login process of one of our application, which uses spring security authentication approach(discussed earlier in this blog posting) .
The error is result of more than one sAMAccountName returned by active directory.
This is possible because of the same sAMAccountName present in different domain.
For example hsimpson is unique sAMAccountName under domain SALES , sAMAccountName by hsimpson can also be under different domain called IT. In this case, when one of these hsimpson login , “yoink” the IncorrectResultSizeDataAccessException shows up.
Easy solution to this problem is, make the user login with domainName\sAMAccountName (example SALES\hsimpson or IT\hsimpson).
In order to make the above mentioned work, i have to write a custom LdapAuthenticationProvider class , thanks to this post.
Step 1.
I removed the <property name=”principalPrefix” value=”SALES" /> from applicationContext-security.xml. Since i want both hsimpson from SALES and IT able to login. If i had the above it defaults to domain “SALES” and no need of adding SALES\ while logging in.
Step 2.
Since my use case allows both hsimpson from SALES and IT able to login as SALES\hsimpson or IT\hsimpson, i had to tweak the LdapAuthenticatorImpl class using string tokenizer as shown below