AuthenticationException

AuthenticationException

我正在尝试使AuthenticationException的异常子类如下:

public class RegistrationNotCompleteException extends AuthenticationException {
    public RegistrationNotCompleteException(String msg) {
        super(msg);
    }
}


在我的loadUserByUsername类的UserDetailsService中,进行以下检查:

if (!user.getHasRegistered()) {
    System.out.println("######## User: " + username
        + " has not completed the registration");
    throw new RegistrationNotCompleteException(
        "User has not completed registration");
}


因此,按预期将用户转发到AuthenticationFailureHandler类。

但是,当尝试在onAuthenticationFailure方法中获取异常类时,如下所示:

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());


它显示异常类是AuthenticationException而不是RegistrationNotCompleteException

我想验证异常类是RegistrationNotCompleteException

请告知该如何做。

最佳答案

通过将支票更改为exception.getCause().getClass()而不是exception.getClass()来解决

08-16 17:39