Consider allowing to hide UserNotFoundException in PreAuthenticatedAuthenticationProvider #15450
Description
I'd like to hide UsernameNotFoundException
in PreAuthenticatedAuthenticationProvider
as like DaoAuthenticationProvider
Since I don't want to imply that the "username" doesn't exist through the class name itself, let alone the error message.
--
hideUserNotFoundExceptions
is allowed in AbstractUserDetailsAuthenticationProvider
which is inherited by DaoAuthenticationProvider
.
--
PreAuthenticatedAuthenticationProvider
, which I'd like to use in my case, indicates UsernameNotFoundException
might be thrown depends on its usage. In actual, AuthenticationUserDetailsService#loadUserDetails is able to throw UsernameNotFoundException
. But the class does not support any sort of hideUserNotFoundExceptions
.
--
My current alternatives/workarounds:
Currently it seems the class can handle UsernameNotFoundException
as AuthenticationException
,
So I create and use new Exception class which inherits AuthenticationException
to avoid exposure of "Username"NotFoundException in case the exception is thrown.
public class SampleUserDetailsService
implements AuthenticationUserDetailsService<PreAuthenticatedAuthenticationToken> {
@Override
public UserDetails loadUserDetails(PreAuthenticatedAuthenticationToken token) {
return Optional.ofNullable(token)
.map( /* normal case */ )
}).orElseThrow(() -> new SampleAuthenticationException());
}
private static class SampleAuthenticationException extends AuthenticationException {
private SampleAuthenticationException() {
super( /* any safe message */ );
}
}
}
This works, but I'm curious why PreAuthenticatedAuthenticationProvider
does not support hideUserNotFoundExceptions
as like AbstractUserDetailsAuthenticationProvider
.
Thanks.