title: Spring Security LDAP Authentication and gather user details from local database
body: <p>In summary, user is being authenticated, but I do appear to actually have logged into the users account.</p>
  
  <p>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</p>
  
  <p>This will display the proper username:</p>
  
  <pre><code>&lt;sec:authorize access="isAuthenticated()"&gt;
     &lt;h2&gt;&lt;sec:authentication property="name"/&gt;&lt;/h2&gt;
  &lt;/sec:authorize&gt;
  </code></pre>
  
  <p>This does not (it did work before LDAP)</p>
  
  <pre><code>&lt;sec:authorize access="isAuthenticated()"&gt;
     &lt;h2&gt;&lt;sec:authentication property="principal.firstName"/&gt;&lt;/h2&gt;
  &lt;/sec:authorize&gt;
  </code></pre>
  
  <p>Relevant Code
  SecurityConfig.java</p>
  
  <pre><code>import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  import org.springframework.security.config.annotation.web.builders.WebSecurity;
  import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  import org.springframework.security.ldap.authentication.UserDetailsServiceLdapAuthoritiesPopulator;
  import org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler;
  import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
  import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
  
  @Configuration
  @EnableWebSecurity
  public class SecurityConfig extends WebSecurityConfigurerAdapter{
      @Autowired
      private CustomUserDetailsService userDetailsService;
  
      @Bean
      public CustomSaltSource customSaltSource(){ return new CustomSaltSource();}
  
      @Bean
      public AuthenticationSuccessHandler myAuthenticationSuccessHandler(){
          return new AuthenticationSuccessHandler();
      }
  
  
  
      @Autowired
      void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
          auth.ldapAuthentication().contextSource()
                  .url("ldap://bar.foo.com")
                  .port(####)
                  .and()
                  .userDnPatterns("cn={0},cn=users,dc=ms,dc=ds,dc=foo,dc=com")
                  .ldapAuthoritiesPopulator(new UserDetailsServiceLdapAuthoritiesPopulator(userDetailsService));
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
  
          http.authorizeRequests()
                  .antMatchers("/skins/**", "/css/**", "/**/laggingComponents", "/assets/**").permitAll().and()
                  .formLogin().loginPage("/login").permitAll().defaultSuccessUrl("/", true).successHandler(myAuthenticationSuccessHandler())
                  .and().logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).deleteCookies("JSESSIONID").permitAll()
                  .and().authorizeRequests().antMatchers("/api/**").anonymous()
                  .and().authorizeRequests().anyRequest().authenticated().and().rememberMe().key("KEY").userDetailsService(userDetailsService);
      }
  
      @Override
      public void configure(WebSecurity web) throws Exception {
          DefaultWebSecurityExpressionHandler handler = new DefaultWebSecurityExpressionHandler();
          handler.setPermissionEvaluator(new PermissionEvaluator());
          web.expressionHandler(handler);
          web.ignoring().antMatchers( "/skins/**", "/css/**", "/api/**", "/assets/**", "/health"); //"/**/test/**"
      }
  }
  </code></pre>
  
  <p>CustomUserDetaulsService.java</p>
  
  <pre><code>import org.hibernate.Session;
  import org.springframework.security.core.userdetails.UserDetailsService;
  import org.springframework.security.core.userdetails.UsernameNotFoundException;
  import org.springframework.stereotype.Service;
  
  import java.util.Set;
  
  @Service
  public class CustomUserDetailsService implements UserDetailsService{
  
      @Override
      public CustomUserDetails loadUserByUsername(String username) throws UsernameNotFoundException{
          Session session = DBFactory.factory.openSession();
          User user = (User) session.createQuery("from User where userName =:userName")
                  .setParameter("userName", username).uniqueResult();
          if(user == null){
              throw new UsernameNotFoundException("User Not Found");
          }
          //Needed to initialize permissions
          Set&lt;Role&gt; roles = user.getRoles();
          int i = roles.size();
          for(Role role: roles){
              int j = role.getPermissions().size();
          }
          CustomUserDetails userDetails = new CustomUserDetails(user);
  
          session.close();
          return userDetails;
      }
  
  
  }
  </code></pre>