title: Spring Boot LDAP Authentication: getting user data at Authentication
body: <p>I have developed a Spring Boot application, and I want to perform authentication using a LDAP server.
  In production it will be an Active Directory server, but during development I am just using <a href="http://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/" rel="nofollow noreferrer">this public LDAP testing server</a></p>
  
  <p>I got it to work using the following classes:</p>
  
  <p>A simple LDAP configuration class. 
  It reads from my <code>application.properties</code> file the properties related to LDAP stuff.</p>
  
  <pre><code>@Configuration
  public class LdapConfiguration {
  
      //Getting values from properties file
      @Value("${ldap.urls}")
      private String ldapUrls;
      @Value("${ldap.base.dn}")
      private String ldapBaseDn;
      @Value("${ldap.username}")
      private String ldapSecurityPrincipal;
      @Value("${ldap.password}")
      private String ldapPrincipalPassword;
      @Value("${ldap.user.dn.pattern}")
      private String ldapUserDnPattern;
      @Value("${ldap.enabled}")
      private String ldapEnabled;
  
      public LdapConfiguration() {
      }
  
      ... getters and setters ...
  
  }
  </code></pre>
  
  <p>And a WebSecurityConfiguration, that actually reads from the LdapConfiguration class to use it for Authentication:</p>
  
  <pre><code>@Configuration
  public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
  
      @Autowired
      private LdapConfiguration ldapConfiguration;
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
                  .authorizeRequests()
                  .anyRequest().fullyAuthenticated()
                  .and()
                  .formLogin();
      }
  
      // Update configure method for the online test server
      @Override
      protected void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth
                  .ldapAuthentication()
                  .contextSource()
                  .url(ldapConfiguration.getLdapUrls() +ldapConfiguration.getLdapBaseDn())
                  .managerDn(ldapConfiguration.getLdapSecurityPrincipal())
                  .managerPassword(ldapConfiguration.getLdapPrincipalPassword())
                  .and()
                  .userDnPatterns(ldapConfiguration.getLdapUserDnPattern());
      }  
  </code></pre>
  
  <p>With this configurations, the LDAP authentication works: I get a nice HTML page with the Login Form and using the proper credentials I can use the website.<a href="https://i.stack.imgur.com/bxEC7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bxEC7.png" alt="login form"></a></p>
  
  <p>And here is my issue: after the login phase, if I run something like </p>
  
  <pre><code>    UserDetails ud = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
      System.out.println(ud);
  </code></pre>
  
  <p>this is what I will get:</p>
  
  <pre><code>LdapUserDetailsImpl@fd8cece1:
      Dn: uid=tesla,dc=example,dc=com; 
      Username: tesla; 
      Password: [PROTECTED]; 
      Enabled: true; 
      AccountNonExpired: true; 
      CredentialsNonExpired: true; 
      AccountNonLocked: true; 
      Granted Authorities: ROLE_SCIENTISTS
  </code></pre>
  
  <p>I get nothing about, say, Tesla's full name (which is there in the LDAP server, as field <code>CN</code>).
  It would be nice to get all data about the authenticated user.</p>
  
  <p>Of course I can run a separate query to the LDAP server, asking the details about the user with username <code>tesla</code>. However, that means making an additional request to the LDAP server, so it does not seem a good solution.</p>
  
  <p>Is there a way to specify which fields I want to obtain from the LDAP server when I perform authentication?
  (And does it change much when working with an Active Directory server?)</p>
  
  <p>Thanks in advance.</p>