title: How to handle and custom error 401 from Spring boot ldap?
body: <p>I'm working in an application that uses Spring secutiry LDAP to authenticate its users, the authentication is working fine, my problem is that if someone try to log in with unauthorized credentials, the application sends immediatly a error 401, and I don't want that, I want to custom some friendly message to show this user, but even in debug I can't find where the application executes de authentication, it even seems that my backend isn't called, how can I custom this exception ?</p>
  
  <p>Here's my configuration on my Security configuration class:</p>
  
  <pre><code>@Configuration
  @Order(2147483640)
  public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
  
  protected void configure(HttpSecurity http) throws Exception {
      http.httpBasic().and()
              .cors().and()
              .csrf().disable()
              .authorizeRequests()
              .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
              .antMatchers(HttpMethod.GET, "/**").permitAll()
              .antMatchers(HttpMethod.POST, "/**").permitAll()
              .antMatchers(HttpMethod.PUT, "/**").permitAll()
              .antMatchers(HttpMethod.DELETE, "/**").permitAll()
              .antMatchers("/**").permitAll();
  }
  
  @Configuration
  protected static class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter {
  
      @Autowired
      private UsrPessoaService usrPessoaService;
  
      @Override
      public void init(AuthenticationManagerBuilder auth) throws Exception {
          auth.ldapAuthentication().userDetailsContextMapper(usrPessoaService)
                  .userDnPatterns("secret")
                  .groupSearchBase("secret")
                  .contextSource()
                  .root("secret")
                  .url("secret").port(000);
  
          System.out.println(auth.toString());
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Thanks in advance for any help !</p>