Я не могу получить эту работу по обработке исключений.Я смотрю похожие вопросы / ответы, но ни один из них не сработал.У меня есть этот класс с @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]