2

我正在尝试创建一个异常子类,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.

请告知如何做到这一点。

4

1 回答 1

0

通过将检查更改为exception.getCause().getClass()而不是解决exception.getClass()

于 2012-10-06T20:25:27.927 回答