Results 1 to 4 of 4

Thread: UserDetailsService - best practice question

Hybrid View

  1. #1

    Default UserDetailsService - best practice question

    Hi,

    The UserDetailsService's loadUserByUsername method defines 2 run-time Exceptions that it should throw, UsernameNotFoundException and DataAccessException.

    I'm currently tidying up the code for an implementation of this interface where the previous developer ignored these Exceptions and instead chose to throw our own custom Exception.

    I'm wondering though if either of these 2 defined Exceptions are being caught and handled further up the chain?
    What is recommended practice in these scenarios?
    From looking at the code I don't see explicit handling of this custom Exception so would have thought using the defined Exceptions would be best.

    Thanks.

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

    Default

    I would probably follow the java doc on when to use them. Specifically if the repository is working ok...use the UsernameNotFoundException, if the repository isn't working throw the DataAccessException. In the end both of the Exceptions are handled by the ExceptionTranslationFilter (the DataAccessException is converted to an AuthenticationException).

    HTH,
    Rob Winch

  3. #3

    Default

    Thanks. As a matter of interest where did you find out that this conversion to an AuthenticationException occurs?

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

    Default

    I found the try/catch for the DataAccessException in the DaoAuthenticationProvider. However, remember these are implementations details that can change (in fact it looks like trunk is catching Exception and throwing an AuthenticationServiceException now...still of type AuthenticationException though), so you should just base it off what the interfaces define.

Posting Permissions

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