我正在尝试使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()
来解决