【问题标题】:Spring boot 2.1.4 ControllerAdvice not working [duplicate]Spring Boot 2.1.4 ControllerAdvice 不起作用[重复]
【发布时间】: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


【解决方案1】:

@ControllerAdvice 只会在@Controller 抛出异常时被命中,而不是@Component

由于您正在用危险/闪光消息“装饰”您的登录页面,您可能需要将request 转发到新的“页面”(例如user/loginerror)?

【讨论】:

  • 在我的应用程序中,组件中引发的异常通常会触发 ControllerAdvice。你说的是哪个版本?
【解决方案2】:

尝试将此添加到您的主 Spring Boot 应用程序以关闭 Error Mvc 的自动配置 配置

@SpringBootApplication
@EnableAutoConfiguration(exclude = {ErrorMvcAutoConfiguration.class})
public class SpringBootDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootDemoApplication.class, args);
    }
}

【讨论】:

  • 这没有帮助。在我点击“登录”按钮之前,我被重定向到登录页面并在控制台中看到异常,但在此之前,我在前端得到“HTTP 状态 500 - 内部服务器错误”和堆栈跟踪。跨度>
  • onAuthenticationFailure 是 Spring 在 AuthenticationFailureHandler 中定义的方法,您在此方法中抛出异常,但只有从控制器方法 impl 抛出的异常才能到达 ControllerAdvice
  • 好的,那么在这里使用 AuthenticationFailureHandler 对我没有帮助。我必须使用.failureUrl("/user-login?error=true") 之类的东西进行登录,并为应用程序的其余部分使用异常处理。
猜你喜欢
  • 2021-05-22
  • 2016-06-23
  • 1970-01-01
  • 2020-06-05
  • 2017-12-17
  • 2018-04-07
  • 2021-06-05
  • 2017-01-23
  • 1970-01-01
相关资源
最近更新 更多