Skip to content
Advertisement

Spring Security LDAP Authentication and gather user details from local database

In summary, user is being authenticated, but I do appear to actually have logged into the users account.

I’m currently working on implementing LDAP authentication on a project. It appears that the authentication portion of things are working in the sense that my application does accept the correct credentials. The issue I’m having is that I cant seem to access ‘principal’ in my jsp views. (I was able to access all of this before making the switch to LDAP). When running a trace my CustomUserDetails service is querying and pulling the correct account information. Any assistance is appreciated

This will display the proper username:

JavaScript

This does not (it did work before LDAP)

JavaScript

Relevant Code SecurityConfig.java

JavaScript

CustomUserDetaulsService.java

JavaScript

Advertisement

Answer

If I’m not wrong, You switched to Ldap Authorization, set url and DN patterns but still provide userDetailsService which search user in database. You need to set UserDetailsContextMapper by implementing the interface and creating your custom one. This will map data from ldap directory context to your custom UserDetails and return it through mapUserFromContext method.

Here is an example CustomUserDetailsContextMapper:

JavaScript

My custom LdapUser:

JavaScript

Then set CustomUserDetailsContextMapper in auth configuration. This is how you will be able to get your user from authentication.getPrincipal(). I hope I correctly understand your problem and answered.

Advertisement