title: Spring Ldap user not found exception
body: <p>If a user entered his user name incorrectly, server internal error 500 appears but it should appear as errors on the login page.</p>
  
  <p><a href="https://i.stack.imgur.com/jMHu2.png" rel="nofollow noreferrer">Server error 500</a></p>
  
  <pre><code>@EnableWebSecurity
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http.authorizeRequests().antMatchers("/resources/**").permitAll()
  
                  .antMatchers("/admin/**").hasRole("ADMIN")
  
                  .antMatchers("/dozent/**").hasRole("USER")
  
                  .anyRequest().authenticated().and()
  
                  .formLogin().loginPage("/login").permitAll().and().logout().permitAll().and();
      }
  
      @Autowired
      DataSource dataSource;
  
      @Autowired
      public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception, UsernameNotFoundException {
          auth.jdbcAuthentication().
              dataSource(dataSource)
              .usersByUsernameQuery("select username,password, "
                      + "enabled from users where username=?")
              .authoritiesByUsernameQuery("select username, "
                      + "role from user_roles where username=?");
  
          auth.ldapAuthentication().
          ldapAuthoritiesPopulator
          (new CustomAuthoritiesPopulator())
              .userSearchFilter("(uid={0})").
              contextSource(contextSource());
  
  
      }
  
  
      @Bean // for LDAP users
      public LdapContextSource contextSource() {
          System.out.println("Ldap contextsource");
          LdapContextSource contextSource = new LdapContextSource();
          contextSource.setUrl("ldap://ldapc.xxx.com:389");
          contextSource.setUrl("ldap://ldapm.yyy.com:389");
          contextSource.setBase("ou=people,dc=example,dc=com");
          contextSource.afterPropertiesSet(); // needed otherwise you will have a
                                              // NullPointerException in spring
          return contextSource;
      }
  
      @Bean // für Admin
      public DriverManagerDataSource dataSource() {
          DriverManagerDataSource ds = new DriverManagerDataSource();
          ds.setDriverClassName("org.postgresql.Driver");
          ds.setUsername("xy");
          ds.setPassword("12345");
          ds.setUrl("jdbc:postgresql://xxx.de:5432/xyDB");
          System.out.println(ds);
          return ds;
      }
  </code></pre>
  
  <hr>
  
  <pre><code>public class CustomAuthoritiesPopulator implements LdapAuthoritiesPopulator {
  
      @Override
      public Collection&lt;? extends GrantedAuthority&gt; getGrantedAuthorities(DirContextOperations userData,
              String username) {
          System.out.println("UserRolle aus DB: " + PostgreSQLLassy.getInstance().getRolle(username));
          Collection&lt;GrantedAuthority&gt; gas = new HashSet&lt;GrantedAuthority&gt;();
  
          if ("User".equals(PostgreSQLLassy.getInstance().getRolle(username))) {
              gas.add(new SimpleGrantedAuthority("ROLE_USER"));
  
          } else if ("AnotherUserRole".equals(PostgreSQLLassy.getInstance().getRolle(username))) {
              gas.add(new SimpleGrantedAuthority("ROLE_USERR"));
          }
  
          return gas;
      }
  }
  </code></pre>