我正在尝试创建一个 AuthenticationException
的异常子类,如下所示:
public class RegistrationNotCompleteException extends AuthenticationException {
public RegistrationNotCompleteException(String msg) {
super(msg);
}
}
在我的 UserDetailsService
类的 loadUserByUsername
中,我进行了以下检查:
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()
来解决
关于java - 不能使用 AuthenticationException 的子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12763324/