title: Catching exception thrown in AuthenticationProvider
body: <p>I am implementing custom 'AuthenticationProvider'. If not authenticated I am throwing exception inside 'authenticate' function as shown below.</p>
  
  <pre><code>public class DelegatingLdapAuthenticationProvider implements AuthenticationProvider {
  
      private ActiveDirectoryLdapAuthenticationProvider primaryProvider;
      private List&lt;ActiveDirectoryLdapAuthenticationProvider&gt; secondaryProviders = new ArrayList&lt;&gt;();
  
      public DelegatingLdapAuthenticationProvider() {
  
      }
  
      @Override
      public Authentication authenticate(Authentication authentication) throws AuthenticationException {
          Authentication result = null;
          AuthenticationException exception = null;
          try {
              result = primaryProvider.authenticate(authentication);
          } catch (AuthenticationException e) {
              exception = e;
              for (ActiveDirectoryLdapAuthenticationProvider secondaryProvider : secondaryProviders) {
                  try {
                      result = secondaryProvider.authenticate(authentication);
                      if (result.isAuthenticated()) {
                              break;
                      }
                  } catch (AuthenticationException e1) {
                              exception = e;
                  }
              }
          }
          if (result == null || !result.isAuthenticated()) {
              throw exception;
      }
  
      return result;
  }
  </code></pre>
  
  <p>I have global exception handler as shown below.</p>
  
  <pre><code>@ControllerAdvice
  public class GlobalExceptionHandler {
  
      @ExceptionHandler({NoPermissionException.class})
      @ResponseBody
      @ResponseStatus(value = HttpStatus.FORBIDDEN)
      public Map&lt;String, String&gt; noPermission(NoPermissionException e) {
          return createErrorResponse(e, "Don't have permissions");
      }
  
      @ExceptionHandler({Exception.class})
      @ResponseBody
      @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
      public Map&lt;String, String&gt; exceptionInProcessing(Exception e) {
          return createErrorResponse(e, "Unable to process. Unknown error occurred: " + e.getMessage());
      }
  
      private Map&lt;String, String&gt; createErrorResponse(Exception e, String errorMessage) {
          Map&lt;String, String&gt; errorResponse = new HashMap&lt;&gt;();
          errorResponse.put("message", errorMessage);
          errorResponse.put("reason", e.toString());
          return errorResponse;
      }
  }
  </code></pre>
  
  <p>When exception is thrown inside the 'authenticate' function, global exception handler is not being called. For all the other exceptions it is being called. I want to catch the exception inside global exception handler and return custom error message. How can I do that? Any help appreciated. Thanks in advance.</p>