【发布时间】:2019-05-03 11:47:02
【问题描述】:
我无法完成此异常处理工作。我看起来类似的问题/答案,但没有一个有效。我有这个带有@ControllerAdvice 的类来处理应用程序级错误处理。这不是 REST 应用程序。这是 Spring Boot 和 Thymeleaf
@EnableWebMvc
@ControllerAdvice
public class ErrorController {
@ExceptionHandler(LoginFailureException.class)
public ModelAndView handleLoginFailureException(LoginFailureException exception) {
log.info("Handle LoginFailureException");
return getModelAndView(exception.getMessage(), "user/login");
}
// other exception handling methods
private ModelAndView getModelAndView(String message, String viewName) {
ModelAndView mav = new ModelAndView();
FlashMessage flashMessage = new FlashMessage();
flashMessage.setType("danger");
flashMessage.setMessage(message);
mav.getModel().put("flashMessage", flashMessage);
mav.setViewName(viewName);
return mav;
}
}
在我的WebSecurityConfigurerAdapter 扩展类中,我有这个
.failureHandler(loginFailureHandler)
LoginFailureHandler 类看起来像这样:
@Component
public class LoginFailureHandler implements AuthenticationFailureHandler {
private MessageSource messageSource;
@Autowired
public LoginFailureHandler(MessageSource messageSource) {
this.messageSource = messageSource;
}
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
AuthenticationException exception) throws IOException, ServletException {
throw new LoginFailureException(
messageSource.getMessage("email.password.not.match", null, Locale.getDefault()));
}
}
当我尝试使用无效的用户名登录时,我的应用程序崩溃并抛出此异常,并且永远无法访问 ExceptionHandler 方法。
com.company.application.controller.exception.LoginFailureException: Email or Username is not valid
at com.company.application.security.LoginFailureHandler.onAuthenticationFailure(LoginFailureHandler.java:34) ~[classes/:na]
【问题讨论】:
-
我想知道这个
getModelAndView()函数在做什么。 -
@NavjotSingh 那部分没有达到,但我在我的问题中添加了那部分,以防你想看。
标签: java spring spring-boot exception