Results 1 to 4 of 4

Thread: ProviderManager logic

  1. #1
    Join Date
    Oct 2010
    Location
    Nice, France
    Posts
    2

    Default ProviderManager logic

    hello,

    I have a question about the default behaviour of org.springframework.security.authentication.Provid erManager.

    From reading the Spring Security 3 book (see e.g. the flow diagram at page 40, introduced by "Let's get into a little more detail and look specifically at the classes involved in the processing of a web-based username and password authentication request:"), it seems to me that supposedly all the configured AuthenticationProviders are tried that support the current authentication method, and as soon as one of them does support it, it should either succesfully authenticate or fail the authentication by throwing an AuthenticationException. In either case, then the loop (of trying AuthenticationProviders) is stopped.

    However when looking at the code (3.0.3 RELEASE), it does not seem to me this works like that. If I read the code correctly, in case of a succesful authentication it will indeed break out of the loop, but in case of an AuthenticationException (i.e. authentication failed), it will just happily loop on to try the next.

    The relevant code is (slightly simplified)

    Code:
    AuthenticationException lastException = null;
    Authentication result = null;
    
    for (AuthenticationProvider provider : getProviders()) {
     if (!provider.supports(toTest)) {
      continue;
     }
    
     try {
      result = provider.authenticate(authentication);
      if (result != null) {
       copyDetails(authentication, result);
       break;
      }
     }
     catch (AuthenticationException e) {
      lastException = e;
     }
    }

    Could anyone please enlighten me how this works ?

    Kind regards
    Heikki Doeleman

  2. #2
    Join Date
    Jan 2008
    Posts
    1,826

    Default

    I can only speak on behalf of what actually occurs since I do not own the book. So long as it is not an AccountStatusException that is thrown, the ProviderManager will continue to try the other AuthenticationProviders until one succeeds.
    Rob Winch - @rob_winch
    Spring Security Lead
    Pivotal

  3. #3
    Join Date
    Sep 2004
    Location
    Manchester, NH
    Posts
    1,236

    Default

    Hello Heikki,

    You are correct in your analysis of the code. Unfortunately, I had to cut down the complexity of that diagram, as it was one of the first ones, and I didn't want to overwhelm people early on

    It is important to note that the exception reported is the last exception from any supporting provider, so (I have had to answer this question before, IIRC), if you have multiple providers failing with exception, you will typically only see a report of the last one.

    Hope that helps!

    Peter
    Peter Mularien | Blog
    Author, Spring Security 3 (Book) - Packt Publishing, Available in print and eBook form
    SCJP 5, Oracle DBA
    Any postings are my own opinion, and should not be attributed to my employer or clients.


  4. #4
    Join Date
    Oct 2010
    Location
    Nice, France
    Posts
    2

    Default

    hello,

    thanks, it's completely clear. I would still suggest if there comes a new edition of the book, maybe you could change the flow chart or else describe the behaviour as it actually is in a few additional lines? As it is now, it makes people think it is different from what it really is ..

    many thanks for your fast responses
    Kind regards
    Heikki Doeleman

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •