gpt4 book ai didi

java - 不能使用 AuthenticationException 的子类

转载 作者:行者123 更新时间:2023-11-29 09:10:27 25 4
gpt4 key购买 nike

我正在尝试创建一个 AuthenticationException 的异常子类,如下所示:

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

在我的 UserDetailsS​​ervice 类的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com