I am trying to make an exception subclass of AuthenticationException as follows:
public class RegistrationNotCompleteException extends AuthenticationException {
public RegistrationNotCompleteException(String msg) {
super(msg);
}
}
and in the loadUserByUsername of my UserDetailsService class I make the following check:
if (!user.getHasRegistered()) {
System.out.println("######## User: " + username
+ " has not completed the registration");
throw new RegistrationNotCompleteException(
"User has not completed registration");
}
so the user is forwarded to the AuthenticationFailureHandler class as expected.
But when trying to get the exception class in the onAuthenticationFailure method as follows:
public void onAuthenticationFailure(HttpServletRequest request,
HttpServletResponse response, AuthenticationException exception)
throws IOException, ServletException {
UsernamePasswordAuthenticationToken token =
(UsernamePasswordAuthenticationToken) exception.getAuthentication();
String username = token.getName();
String credentials = token.getCredentials().toString();
System.out.println("Exception class: " + exception.getClass());
it prints that the exception class is AuthenticationException not RegistrationNotCompleteException.
And I want to verify that the exception class is RegistrationNotCompleteException.
Please advise how to do that.
solved by changing the check to
exception.getCause().getClass()instead ofexception.getClass()